pythonmayapymel

How to isolate group nodes in maya with python


I have a selection that can reasonably contain most any node type. In python I need to filter out everything except the group nodes. The problem is that group nodes are read by maya as just transform nodes so its proving difficult to filter them out from all of the other transform nodes in the scene. Is there a way to do this? Possibly in the API?

Thanks!


Solution

  • As you alluded to, "group" nodes really are just transform nodes, with no real distinction.

    The clearest distinction I can think of however would be that its children must be comprised entirely of other transform nodes. Parenting a shape node under a "group" will no longer be considered a "group"


    First, your selection of transform nodes. I assume you already have something along these lines:

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

    Next, a function to check if a given transform is itself a "group".

    Iterate over all the children of a given node, returning False if any of them aren't transform. Otherwise return 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
    

    Now you just need to filter the selection, in one of the following two ways, depending on which style you find most clear:

    selection = filter(is_group, selection)
    

    or

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