Created
April 12, 2024 23:26
-
-
Save nepfaff/2653fbc7632c965a5c29ad83ebc3b14e to your computer and use it in GitHub Desktop.
Minimal example: Meshcat opacity with GLTF files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
directives: | |
- add_model: | |
name: iiwa | |
file: package://drake_models/iiwa_description/sdf/iiwa7_no_collision.sdf # Drake 1.28.0 | |
# file: package://drake/manipulation/models/iiwa_description/iiwa7/iiwa7_no_collision.sdf # Drake 1.27.0 | |
- add_weld: | |
parent: world | |
child: iiwa::iiwa_link_0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from pydrake.all import ( | |
AddMultibodyPlantSceneGraph, | |
BodyIndex, | |
DiagramBuilder, | |
MeshcatVisualizer, | |
MultibodyPlant, | |
Simulator, | |
StartMeshcat, | |
Parser, | |
) | |
def main(): | |
model_path = "./iiwa.dmd.yaml" | |
alpha = 0.5 | |
# Create plant | |
builder = DiagramBuilder() | |
plant: MultibodyPlant | |
plant, scene_graph = AddMultibodyPlantSceneGraph(builder, time_step=0.0) | |
parser = Parser(plant) | |
parser.AddModels(model_path) | |
plant.Finalize() | |
# Create meshcat | |
meshcat = StartMeshcat() | |
MeshcatVisualizer.AddToBuilder(builder, scene_graph, meshcat) | |
diagram = builder.Build() | |
simulator = Simulator(diagram) | |
simulator.set_target_realtime_rate(1.0) | |
simulator.Initialize() | |
# Make existing visual geometries transparent | |
for i in range(plant.num_bodies()): | |
body = plant.get_body(BodyIndex(i)) | |
if body.name() == "world": | |
continue | |
meshcat_body_path = os.path.join( | |
"visualizer", *body.scoped_name().to_string().split("::") | |
) | |
for idx in plant.GetVisualGeometriesForBody(body): | |
meshcat_path = os.path.join( | |
meshcat_body_path, str(idx.get_value()), "<object>" | |
) | |
meshcat.SetProperty(meshcat_path, "opacity", alpha) | |
# Simulate | |
while True: | |
simulator.AdvanceTo(simulator.get_context().get_time() + 1.0) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment