如何使用python隔离maya中的组节点

The*_*rry 2 python maya pymel

我有一个可以合理地包含大多数节点类型的选择.在python中,我需要过滤掉除组节点之外的所有内容.问题是maya将组节点读取为变换节点,因此很难将它们从场景中的所有其他变换节点中过滤出来.有没有办法做到这一点?可能在API中?

谢谢!

mhl*_*ter 6

正如您所提到的,"组"节点实际上只是transform节点,没有真正的区别.

然而,我想到的最明显的区别是它的孩子必须完全由其他transform节点组成.将"组"下的形状节点设为父母将不再被视为"组"


首先,选择transform节点.我假设你已经有了以下几点:

selection = pymel.core.ls(selection=True, transforms=True)
Run Code Online (Sandbox Code Playgroud)

接下来,检查给定变换本身是否为"组"的函数.

迭代给定节点的所有子节点,False如果其中任何节点没有,则返回transform.否则返回True.

def is_group(node):
    children = node.getChildren()
    for child in children:
        if type(child) is not pymel.core.nodetypes.Transform:
            return False
    return True
Run Code Online (Sandbox Code Playgroud)

现在您只需要使用以下两种方法之一过滤选择,具体取决于您最清楚的样式:

selection = filter(is_group, selection)
Run Code Online (Sandbox Code Playgroud)

要么

selection = [node for node in selection if is_group(node)]
Run Code Online (Sandbox Code Playgroud)