【问题标题】:How to isolate group nodes in maya with python如何使用python隔离maya中的组节点
【发布时间】:2014-10-27 19:35:06
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: python maya pymel


    【解决方案1】:

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

    可以想到的最明显的区别是它的子节点必须完全由其他 transform 节点组成。 “组”下的形状节点将不再被视为“组”


    首先,您选择的transform 节点。我假设您已经掌握了这些方面的内容:

    selection = pymel.core.ls(selection=True, transforms=True)
    

    接下来,检查给定变换本身是否是“组”的函数。

    遍历给定节点的所有子节点,如果其中任何一个不是 transform,则返回 False。否则返回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
    

    现在您只需要过滤选择,在以下两种方式中的一种,具体取决于您发现最清晰的样式:

    selection = filter(is_group, selection)
    

    selection = [node for node in selection if is_group(node)]
    

    【讨论】:

    • 感谢您的帮助!这正是我所需要的。 :D
    【解决方案2】:

    我知道这是旧的,此处描述的方法仅与 maya.cmds 命令一起使用时将无法正常工作。这是我的解决方案:

    import maya.cmds as cmds
    
    def is_group(groupName):
        try:
        children = cmds.listRelatives(groupName , children=True)
        for child in children:
            if not cmds.ls(child, transforms=True):
                return False
            return True
        except:
            return False
    
    for item in cmds.ls():
        if is_group(item):
            print(item)
        else:
            pass
    

    【讨论】:

      【解决方案3】:

      mhlester 对关节的回答将返回 true,因为它们也符合该定义。 它也不考虑空组

      def isGroup(node):
          if mc.objectType(node, isType = 'joint'):
              return False
          kids = mc.listRelatives(node, c=1)
          if kids:
              for kid in kids:
                  if not mc.objectType(kid, isType = 'transform'):
                      return false
          return True
              
      print isGroup(mc.ls(sl=1))  

      【讨论】:

        猜你喜欢
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        • 2021-03-20
        • 2018-02-02
        • 2021-03-03
        • 2012-09-07
        • 1970-01-01
        相关资源
        最近更新 更多