You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drake最新版本中设置模型透明度失败的问题求助

Drake最新版本中设置模型透明度失败的问题求助

问题原因分析

你碰到的这个报错,是因为Drake在新版本里调整了几何属性的存储逻辑——原来用来存漫反射颜色的phong组被替换成了__default__组,所以旧代码里通过properties.GetProperty("phong", "diffuse")获取属性的方式就失效了,才会抛出“组不存在”的错误。

修复代码方案

你只需要修改helpers.py里的set_transparency_of_models函数,适配新的属性分组即可。以下是调整后的完整函数代码:

def set_transparency_of_models(plant, model_instances, alpha, scene_graph):
    inspector = scene_graph.model_inspector()
    for body_index in plant.GetBodyIndices(model_instances):
        body = plant.get_body(body_index)
        # 处理可视化几何的透明度
        for geometry_id in inspector.GetGeometries(
                body.index(), Role.kIllustration):
            properties = inspector.GetIllustrationProperties(geometry_id)
            
            # 优先适配新版Drake的__default__组,同时兼容旧版的phong组
            if properties.HasProperty("__default__", "diffuse"):
                diffuse_color = properties.GetProperty("__default__", "diffuse")
                target_group = "__default__"
            elif properties.HasProperty("phong", "diffuse"):
                diffuse_color = properties.GetProperty("phong", "diffuse")
                target_group = "phong"
            else:
                # 找不到对应属性时跳过,避免程序中断
                continue
            
            # 修改颜色的alpha通道值
            diffuse_color.set(diffuse_color.r(), diffuse_color.g(), diffuse_color.b(), alpha)
            # 将修改后的属性更新回对应分组
            properties.UpdateProperty(target_group, "diffuse", diffuse_color)

关键修改说明

  1. 分组兼容处理:代码里先检查新版的__default__组,再兼容旧版的phong组,这样你的代码可以同时在新旧版本的Drake中正常运行。
  2. 容错逻辑:如果某个几何没有漫反射属性,直接跳过它,不会因为个别异常导致整个可视化流程中断。
  3. 属性更新对应:获取属性时记录对应的分组,更新时也用同一个分组,确保修改能正确同步到场景图中。

把旧的set_transparency_of_models函数替换成上面的代码后,重新运行prm_comparison.ipynb,应该就能顺利设置模型透明度了。

备注:内容来源于stack exchange,提问作者LiHaochuan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:58:03