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!