Search code examples
pythonmaya

How to get the list of root parents created in a scene - Autodesk Maya / Python?


I am a bit new to python and I am trying to get a list containing all root parent existing in a scene of type joint. for example, my scene outliner is something like that:

group1>>group2>>joint1>>joint2>>joint3

group3>>joint4>>joint5

joint16>>joint17>>joint18

I want a script that travels through the outliner and returns a list, in my example:

[joint1, joint4, joint16]

Any tips would be really appreciated. thank you so much.


Solution

  • Im not sure if it is any of use has Haggi Krey solution works fine but You can use also the flag : -long from cmds.ls

    # list all the joints from the scene
    mjoints = cmds.ls(type='joint', l=True)
    # list of the top joints from chain
    output = []
    # list to optimise the loop counter
    exclusion = []
    # lets iterate joints
    for jnt in mjoints:
        # convert all hierarchy into a list
        pars = jnt.split('|')[1:]
        # lets see if our hierarchy is in the exclusion list
        # we put [1:] because maya root is represented by ''
        if not set(pars) & set(exclusion):
            # we parse the hierarchy until we reach the top joint
            # then we add it to the output
            # we add everything else to the exclusion list to avoid 
            for p in pars:
                if cmds.nodeType(p) == 'joint':
                    output.append(p)
                    exclusion+=pars
                    break
    print(output)
    

    I just put this because there is not one way to go. I hope the construction of this code could help your python skills. It is exactly the same, just the way to find the parent nodes is different !