首页 > 解决方案 > 如何在 Maya python 中获取父关节组

问题描述

我有这样的层次结构:

root_ctrl
 group2
   group3
     joint

我想获取组的名称。

输出应该是['group2', 'group3']. 我试过使用

parents = cmds.listRelatives('joint', allParents = True )
output = []
parents = (cmds.ls('joint', long=True)[0].split('|')[1:-1])
print parents 

但这会返回 [join,group3,group2,root_ctrl]。但我想 [group3, group2] 作为输出。

标签: pythonparentmaya

解决方案


如果我了解您的问题,这是一个解决方案。这是一个不是很漂亮的解决方案,但它仍然有效。

hierarchy = cmds.ls('joint', long=True)[0]

def get_groups(hierarchy=None):
    nodes = [node for node in hierarchy.split('|') if node]
    return [x for x in nodes if cmds.listRelatives(x, shapes=True) is None and cmds.nodeType(x) == 'transform']

print(get_groups(hierarchy))

推荐阅读