Search code examples
drake

How to edit alpha transparency for new GLTF models programatically


I want to change the transparency of a model's visual geometries (iiwa 7).Before the switch to GLTF files, I was able to do it using the following logic:

for geometry_id in visual_geometry_ids:
            old_props = scene_graph.model_inspector().GetIllustrationProperties(
                geometry_id
            )
            new_props = IllustrationProperties(old_props)
            old_rgba = old_props.GetProperty("phong", "diffuse")
            new_props.UpdateProperty(
                group_name="phong",
                name="diffuse",
                value=Rgba(
                    r=old_rgba.r(),
                    g=old_rgba.g(),
                    b=old_rgba.b(),
                    a=new_alpha,
                ),
            )
            scene_graph.AssignRole(
                source_id=plant.get_source_id(),
                geometry_id=geometry_id,
                properties=new_props,
                assign=RoleAssign.kReplace,
            )

However, the illustration properties of the new GLTF models only contain a "__default__" group with no properties.


Solution

  • The good news is that, as long as you can identify the path of the object you want to change, changing only its opacity (while leaving its original color -- or complex textures -- intact), is super easy.

    meshcat->SetProperty("path/to/my/thing/<object>", "opacity", alpha);
    meshcat->SetProperty("path/to/my/thing/<object>", "modulated_opacity", alpha);
    

    The first line sets the target to being a uniform opacity (the alpha value). The second lines modulates the original opacity -- the new opacity is the original opacity times alpha. You can choose to set either value interchangably -- meshcat remembers what the original opacity was and updates the current opacity based on the last flavor of opacity that got set.

    Do not forget to append /<object> to the end of your path. The opacity you want to set belongs to the underlying 3D object, so you must call that out specifically.