2

I have come across an existing piece of code that does this, but I assume there must be a better way. So, I need to get a highest level in mesh node in Autodesk Maya.

// List all mesh objects
string $nodess[] = `ls -type mesh` ;

// Replace existing items on active list with this
select -r $nodess[0] ;   

int $i = 1 ;

while ($i < 30) {
    // Pick up the tree 30 times
    pickWalk -d up ;
    $i++ ;
}

// List all selected objects
string $rootNode[] = `ls -sl` ;

// Clear selection
select -cl ;

string $myroot = $rootNode[0] ;
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
panofish
  • 7,578
  • 13
  • 55
  • 96

3 Answers3

2

to get the root, just string-split the long name:

global proc string get_root(string $node)
{
    string $longname[] = ls("-l", $node);
    string $tokens[];
    tokenize($longname[0], "|", $tokens);
    return $tokens[0];
}

Of course it's much more elegant in Python:

root = cmds.ls(node, l=True)[0].split("|")[0]

You could also re-write the orignal function by calling listRelatives -p until it returns nothing; but the string method is easier

theodox
  • 12,028
  • 3
  • 23
  • 36
2

I think this is probably the most direct:

string $roots[] = `ls -assemblies`;

From the Docs:

    -assemblies(-as)
        List top level transform Dag objects
agrant3d
  • 53
  • 4
1

I understand this topic is a little out of date... However...

Theodox's answer helped me, but for some reason...

root = cmds.ls(node, l=True)[0].split("|")[0]

...didn't give me the exact answer I needed. Instead...

root = cmds.ls(node, l=True)[0].split("|")[1] #<-- Notice this [1]

gave me me the exact answer I needed. Apparently, [0] provides a blank space in my scene, because the array is listed as...

 [u'',u'the_Item_I_Need',u'etc1',u'etc2']

Just a helpful hint if someone is having an issue!!

SirJames
  • 387
  • 8
  • 27