如何在Maya中根据optionMenu选择项联动更新textScrollList?
你原代码里有两个关键问题需要修正:
- 最后一行操作错了控件:应该往
tsl(textScrollList)里加内容,不是om(optionMenu),而且添加项目要用append参数 - 需要确保
temp是对象类型到对应对象列表的字典,比如temp = {"mesh": ["pCube1", "pSphere1"], "nurbsCurve": ["nurbsCircle1"]}这种结构
修正后的筛选函数
def selection(*args): # 获取optionMenu选中的类型 selected_type = cmds.optionMenu(om, sl=True, q=True) # 清空textScrollList现有内容 cmds.textScrollList(tsl, e=True, removeAll=True) # 如果选中的类型在映射字典里,就把对应对象添加到textScrollList if selected_type in temp: for item in temp[selected_type]: cmds.textScrollList(tsl, e=True, append=item)
补充:如何构建类型-对象映射字典
如果还没生成temp字典,可以用这段代码批量获取场景中不同类型的对象,同时把类型选项添加到optionMenu:
# 初始化映射字典 temp = {} # 定义要筛选的对象类型,可根据需求增减 target_types = ["mesh", "nurbsCurve", "joint", "camera", "light"] for obj_type in target_types: # 获取场景中所有该类型的对象 obj_list = cmds.ls(type=obj_type) temp[obj_type] = obj_list # 把类型添加到optionMenu作为选项 cmds.menuItem(label=obj_type, parent=om)
最后别忘了绑定回调
创建optionMenu的时候,要把筛选函数设为变更回调,这样选类型时才会触发更新:
# 创建optionMenu并绑定回调 om = cmds.optionMenu(label="选择对象类型", changeCommand=selection) # 创建textScrollList tsl = cmds.textScrollList(width=200, height=300)
内容的提问来源于stack exchange,提问作者Kartikey Sinha
相关产品推荐
相关产品推荐

