List ALL parents of an object ? (MEL)


#1

Clearly the coffee isn’t working today or I’m having a brain fart but I can’t seem to get this to work.

I want to write a script that can take different objects that are at different levels in hierarchies (the depth will be unpredictable from rig to rig) and check if ANY of the parent objects have their visibility turned off.

So I might have a hierarchy like this :

-group1
----- group2
----------- group3
---------------- objectToCheck

and I want to give the script the ‘objectToCheck’ and have it check its visibility AND all of the group nodes’ visibility.

But I’m stuck on just listing all of those groups given the ‘objectToCheck’

It seems to me from reading the docs that this should work :

string $objectParents[]=listRelatives -allParents "objectToCheck";

but it only gives me one object (group3) so I’m puzzled as to why the -allParents flag exists if it just gives the same result as -parent.
I expected -allParents would give… ALL parents (!) - in this case - group1,group2,group3

How do I get it to populate the $objectParents array with all of the parent groups ?

Cheers,
Brian


#2

The nomenclature is a bit confusing. allParents really means return all immediate parents. group2 would be a “grandparent” and group1 a “great grandparent”.

If you want to get the full parent hierarchy, then you would want something like this.

global proc getParentHier( string $node, string $result[] )
{
    string $parents[] = `listRelatives -ap $node`;
    for($p in $parents)
    {
        getParentHier($p, $result);
        $result[size($result)] = $p;
    }
}

{
    string $result[];
    getParentHier( "pSphere1", $result );
    print $result;
}

For my test scene, where the hierarchy is: group3 > group2 > group1 > pSphere1, the above code returns:

group3
group2
group1

#3

Awesome many thanks! It does seem confusing but your example makes sense and works perfectly here.
Much appreciated.

Cheers,
Brian


#4

Another way to do it:


string $objects[] = `ls -l myobject`;
string $tokens[];
tokenize($objects[0], "|", $tokens);


#5

Hey thanks, that’s a useful one too. I’ve used tokenize to deal with Namespaces but didn’t think of it for this, nice idea.

Cheers,
Brian