[Maya MEL] Unexpected For Loop / Parent Behavior

mel
maya

#1

I have this script that retrieves the immediate parent of a selected object. It works well if I only have one selected but not on multiple selection.

For instance, if the hierarchy is as follows:

  • objectA_grp
  •  objectA_geo
    
  •     objectB_grp
    
  •         objectB_geo
    
  •           objectC_grp
    
  •              objectC_geo
    

Selects all the geo objects. I would expect only three result

objectC_grp
objectB_grp
objectA_grp

However, when I run the script, it returns redundant objects

objectC_grp
objectB_grp
objectA_grp
objectC_grp
objectB_grp
objectA_grp
objectC_grp
objectB_grp
objectA_grp

Is there a way around this? Thank you for looking at the problem

The script is as follows:
$sel= ls -sl;

for ($list in $sel){
$parent = listRelatives -p;
print $parent;
}


#2

Hey bentraje,

This seemed to work for me:

$sel= `ls -sl`;
for ($list in $sel){
    $parent = `listRelatives -p $list`;
    print $parent;
}

It looks like you may have just needed to pass in $list as a variable when checking for the listRelatives. Otherwise it will return the parents for all of the selected, each loop, but not iterate through them 1 by 1.


#3

Hi RiggerRyan! (Nice Name BTW)
Thanks for pointing out. It works as expected now.


#4

Haha thanks :smiley: Awesome news!