【问题标题】:Get Object Type into Group in Outliner Maya在 Outliner Maya 中将对象类型分组
【发布时间】:2019-06-01 17:15:00
【问题描述】:

我尝试将每个元素的所有对象类型放入 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)

我收到了这个错误:

错误:RuntimeError:文件 /Users/jhgonzalez/Library/Preferences/Autodesk/maya/2018/scripts/AssigMaterialForEachMesh.py 第 26 行:没有对象匹配名称:SafetyHandle_019_allFromGun:pCylinderShape21 找不到对象“SafetyHandle_019_allFromGun:pCylinderShape21”。

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

【问题讨论】:

  • 您的代码没有为我产生任何错误。这是完整的脚本吗?
  • 是的...但我知道问题出在哪里...问题是他们在这个场景中重复了一些网格。我解决了这些问题。

标签: python maya


【解决方案1】:

问题在于您没有使用长名称,因此如果存在具有重复名称的对象,脚本将会崩溃,因为 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)

现在它继续按预期工作,尽管您的场景中有重复的名称。

【讨论】:

    猜你喜欢
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2020-05-22
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多