在 Outliner Maya 中将对象类型放入组中
Get Object Type into Group in Outliner Maya
我尝试将每个元素的所有对象类型放入 Outliner 的组中。
这是我的代码。
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.
我正在用这个
测试这段代码
问题是您没有使用长名称,因此如果存在名称重复的对象,脚本将崩溃,因为 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)
现在它继续按预期工作,尽管您的场景中有重复的名称。
我尝试将每个元素的所有对象类型放入 Outliner 的组中。
这是我的代码。
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.
我正在用这个
测试这段代码问题是您没有使用长名称,因此如果存在名称重复的对象,脚本将崩溃,因为 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)
现在它继续按预期工作,尽管您的场景中有重复的名称。