View Full Version : Getting base level selection

12-20-2010, 05:08 PM
Hi, just getting back into MEL after about 6yrs and I've been struggling with selections.

I want to be able to select any part of a hierarchy, drop to the lowest transform nodes (the ones giving transform info for the shape nodes) and unparent. My script calls for me to delete an existing hierarchy if it exists and build a new one from the parameters in the script.

How do I get this selection?


12-21-2010, 01:38 PM
I don't think I'm following completely...
But maybe you are looking for something of that:string $transforms[] = `ls -long -tr -sl`;
string $pathParts[] = stringToStringArray($transforms[0],"|");
print $pathParts[0];
string $parents[] = `listRelatives -p`;

12-21-2010, 01:56 PM
Cheers for the reply. Its not exactly what I mean though.

Take a cube object and group it twice, creating two transform nodes above it.
What I want to do is break all links to the new group nodes and just retain the cube as a selection regardless of which part of the hierarchy a user has selected.

If they always select the cube itself then its easy, 'parent -w' does it. I want to remove the possibility of user error in the selections though as it can change the outcome of the rest of the script.

12-22-2010, 10:14 AM
Cheers for the reply. Its not exactly what I meant though.

I'm trying to unlink all nodes in a hierarchy, leaving me with only the base transform node which is connected to the shape node of the object.

Create a box and group it twice. This creates two nodes above it in the hierarchy. I want to be able to select any part of this hierarchy and have the script end up with the box as the selection (but not the shape node).

I'm trying to remove user error from my script. It is simple if people only select the object as I can unlink using parent -w but I just wanted to make it more foolproof than that.

12-22-2010, 02:28 PM
OK, getting closer to a solution to this. If I'm only dealing with a single object in the hierarchy then I can look at the size of the selection array and pick the last object in the array then unparent.

//select any part of the hierarchy
// first I want to get all transform nodes in the current selection hierarchy.
string $selection[] = `ls -sl -dag -tr`;
//the object I am after is the size of the selection - 1 since the array begins at 0
int $num_selection = size($selection)-1;
select $selection[$num_selection];
//create a new selection array from the current selection.
string $selected[] =`ls -sl`;
for ($obj in $selected)
parent -w;

However, if I have multiple objects assigned to the hierarchy this doesn't work. Say I have two cubes and have grouped them, then added another transform node on top of this group.

Group 2

I tried this...

//select any part of the hierarchy
//get the shape nodes as these will be lowest level.
string $selection[] = `ls -sl -dag -s`;
//rebuild selection from array
select $selection;
//step up one level to get the transform nodes for the shape nodes.
pickWalk -d Up;
//make a new selection from this.
string $current_selection[] = `ls -sl`;
for ($obj in $current_selection)
parent -w;

Now this does actually work but it gives a warning and I can't figure out how to get rid of it.

// Warning: Object, 'pCube1', skipped. It is already a child of the parent, 'world'. // // Warning: Object, 'pCube2', skipped. It is already a child of the parent, 'world'. //

If I manually select each of the cubes in the viewport and unparent using parent -w in the script editor it works. I think it is trying to unparent the cube's transform and shape nodes inside the loop.

Anyone have any ideas how to sort this? I might be going about this the completely wrong way.

12-22-2010, 03:47 PM
Now this does actually work but it gives a warning and I can't figure out how to get rid of it.The reason you get a warning here, is that you don't define which object's to parent to the world in the for-loop.

This should work :)
//Get all the shapes in any level of the selection
string $shapes[] = `ls -sl -dag -s`;
//Get the transform nodes of the shapes
string $transforms[] = `pickWalk -d Up $shapes`;
//For each transform node
for ($obj in $transforms)
//If it's not already a parent of 'world', parent it to 'world'
if(`firstParentOf($obj)` != "") parent -w $obj;

12-22-2010, 04:03 PM
Aha. Works perfect. Thanks for that.

12-22-2010, 04:03 PM
Maybe this way to get the transforms:

string $shapes[] = `ls -sl -dag -leaf`;
string $transforms[] = `listRelatives -p $shapes`;

and unparent:

parent -w $transforms;

12-22-2010, 05:14 PM
Thanks. That's a cleaner solution than my effort :)

CGTalk Moderation
12-22-2010, 05:14 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.