Mel Version:
// **********************************************************
// Return Top Parent Of Any Node
global proc string jgReturnTopParent (string $obj) {
string $a[] = {} ;
string $b ;
$a = `listRelatives -p $obj` ;
while(size($a) > 0) {
$b = $a[0] ;
$a = `listRelatives -p $a[0]` ;
}
return $b ;
}
PyMel Version:
##################################################
## Return Top Parent Of Any Node
def jgReturnTopParent(obj):
# Imports
import pymel.core as pm
# Get First Parent
varA = pm.listRelatives(obj,p=True)
# Loop All Parents
while(len(varA) > 0):
varB = varA[0]
varA = pm.listRelatives(varA[0],p=True)
return varB
Update: Thanks to Scott Englert for pointing out in the comments that MEL and PyMel both have built in functions for this already.
MEL command that comes with Maya:
rootOf($obj);
PyMEL method of dag nodes:
obj.root()
Cheers Scott!
There are already shortcuts written into Maya and PyMEL for these so you do not need to write a new function.
ReplyDeleteMEL command that comes with Maya:
rootOf($obj);
PyMEL method of dag nodes:
obj.root()
Simple and easy, one line and you don't have to carry around extra functions around.
Cheers!
Haha...I should have known. Thanks for pointing that out Scott, updated!
ReplyDeleteSorry to hit up an old post, but when I've tried using obj.root() on a joint, it returns an invalid result. To get the joints root I had to use obj[0].root().
ReplyDeletefrom pymel.core import *
obj = ls(sl=True)
obj[0].root()
Tried using obj="joint6" as well with errors as well. Just getting used to PyMel, so it may be me entirely. :)
This is due to the command "ls" returning a list, not a single object.
DeleteThis is due to the command "ls" returning a list, not a single object.
Delete