gpt4 book ai didi

python - 将对象类型放入 Outliner Maya 中的组中

转载 作者:行者123 更新时间:2023-12-01 08:28:26 25 4
gpt4 key购买 nike

我尝试将每个元素的所有对象类型放入大纲 View 中的组中。

这是我的代码。

from maya import cmds

objects = cmds.ls(selection=True, dag=True)

objects.sort(key=len, reverse=True)

# Now we loop through all the objects we have
for obj in objects:
# We get the shortname again by splitting at the last |
shortName = obj.split('|')[-1]

children = cmds.listRelatives(obj, children=True) or []

if len(children) > 0:
for current in children:
objType = cmds.objectType(current)
print(objType)

我收到此错误:

Error: RuntimeError: file /Users/jhgonzalez/Library/Preferences/Autodesk/maya/2018/scripts/AssigMaterialForEachMesh.py line 26: No object matches name: SafetyHandle_019_allFromGun:pCylinderShape21 Object 'SafetyHandle_019_allFromGun:pCylinderShape21' not found.

我正在用这个测试这段代码

enter image description here

最佳答案

问题是您没有使用长名称,因此如果存在名称重复的对象,脚本将会崩溃,因为 Maya 不知道如何解决它。

例如,假设您有一个包含 3 个节点的层次结构:

|a
|b
|c

另一个具有 2 个节点的层次结构:

|d
|a

由于您使用的是短名称,因此当您尝试从 a 查询 objectType 时,它不知道您想要从哪个层次结构中获取它,因此只需使用长名称:

from maya import cmds

objects = cmds.ls(selection=True, dag=True, l=True) # Use long parameter.

objects.sort(key=len, reverse=True)

# Now we loop through all the objects we have
for obj in objects:
children = cmds.listRelatives(obj, f=True, children=True) or [] # Use full parameter.

if len(children) > 0:
for current in children:
objType = cmds.objectType(current)
print(objType)

现在,尽管场景中有重复的名称,它仍然可以按预期工作。

关于python - 将对象类型放入 Outliner Maya 中的组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54067626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com