首页 > 解决方案 > 如何获取在场景中创建的根父级列表 - Autodesk Maya / Python?

问题描述

我对 python 有点陌生,我正在尝试获取一个列表,其中包含类型场景中存在的所有根父级joint。例如,我的场景大纲是这样的:

组1>>组2>>关节1>>关节2>>关节3

group3>>joint4>>joint5

接头16>>接头17>>接头18

在我的示例中,我想要一个遍历大纲并返回列表的脚本:

[joint1, joint4, joint16]

任何提示将不胜感激。太感谢了。

标签: pythonmaya

解决方案


我不确定它是否有用,Haggi Krey 解决方案工作正常,但您也可以使用标志:来自 cmds.ls 的 -long

# list all the joints from the scene
mjoints = cmds.ls(type='joint', l=True)
# list of the top joints from chain
output = []
# list to optimise the loop counter
exclusion = []
# lets iterate joints
for jnt in mjoints:
    # convert all hierarchy into a list
    pars = jnt.split('|')[1:]
    # lets see if our hierarchy is in the exclusion list
    # we put [1:] because maya root is represented by ''
    if not set(pars) & set(exclusion):
        # we parse the hierarchy until we reach the top joint
        # then we add it to the output
        # we add everything else to the exclusion list to avoid 
        for p in pars:
            if cmds.nodeType(p) == 'joint':
                output.append(p)
                exclusion+=pars
                break
print(output)

我之所以这么说,是因为没有一条路可走。我希望这段代码的构建可以帮助你的 Python 技能。完全一样,只是找到父节点的方式不同而已!


推荐阅读