Maya | Get parent nodes in sequence

Go back from the selected node and get the parent node.

Maya Cmds version


import maya.cmds as cmds

def get_parent(node):
    parent = cmds.listRelatives(node, parent=True, path=True)
    if parent:
        yield parent
        for p in get_parent(parent):
            yield p

for node in get_parent(cmds.ls(selection=True)):
    print node

PyMel version


import pymel.core as pm

def get_parent(node):
    parent = pm.listRelatives(node, parent=True)
    if parent:
        yield parent
        for p in get_parent(parent):
            yield p

for node in get_parent(pm.selected()):
    print node

If you select null1 and run the script, you will get the following results: image

[nt.Transform(u'group1')]
[nt.Transform(u'group2')]
[nt.Transform(u'group3')]

Recommended Posts

Maya | Get parent nodes in sequence
[Maya] Write custom nodes in Open Maya 2.0
Get date in Python
Get and create nodes added and updated in the new version
Get last month in python
List of nodes in diagrams
Get clipboard from Maya settings
Get Terminal size in Python
Explicitly get EOF in python
Get Evernote notes in Python
Maya | Get the workspace path
Get Japanese synonyms in Python