Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to isolate group nodes in maya with python

Tags:

python

maya

pymel

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!

like image 223
TheBeardedBerry Avatar asked Oct 22 '25 05:10

TheBeardedBerry


1 Answers

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)]
like image 102
mhlester Avatar answered Oct 24 '25 16:10

mhlester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!