Home Technical Talk

Melscript Gurus (ParentConstraint)

StephenVyas
polycounter lvl 18
Offline / Send Message
StephenVyas polycounter lvl 18
Hi guys
There's a piece of code i'm not sure how to write. Here's a small example of what i'm trying to achieve:

I have two objects.
They are parent constraint to each other.
I'd like to write a piece of melscript to GET the name of the Parent object and load it into a Variable, if I have the child object selected.

I'm assuming it'd be similar to writing an string array like so:

string $array[] = `ls -sl`;
for ($item in $array) {
$Parentname = `getAttr ($item+ "Parent Constraint Name")`;
}

but I don't believe the 'getAttr' is the correct command to do this.

Any ideas?


Edit: TLDR; in simpler words.
How would I query the name of the "Parent object" if i have the child selected. (within a parentConstraint relationship)

Replies

  • Bartalon
    Options
    Offline / Send Message
    Bartalon polycounter lvl 12
    I'm far from being a guru but perhaps this can point you in the right direction until someone else responds...


    This will identify a physical parent-child relationship.
    string $parent[] = listRelatives("-p",`ls -sl`);
    
    //or more simply put:
    
    string $select[] = `ls -sl`;
    $parent = `listRelatives -p $select`;
    
    This will find connections between an actual constraint node (Parent, Scale, Aim, etc.)
    //store current selection, for later use
    string $select[] = `ls -sl`;
    
    //find related constraint nodes
    string $constraintList[] = `listConnections -type "constraint"`;
    
    //remove duplicates from array
    $constraintList = `stringArrayRemoveDuplicates($constraintList)`;
    
    //finds all objects the constraint is connected to (parent and child)
    string $relatedObjects[] = `listConnections $constraintList`;
    
    //remove duplicates from array
    $relatedObjects = `stringArrayRemoveDuplicates($relatedObjects)`;
    
    //removes your original selection from the list
    $relatedObjects = stringArrayRemoveExact($select, $relatedObjects); 
    
    //this is your parent or your child, depending on which one you selected first
    print $relatedObjects;
    
    
  • JohnnyRaptor
    Options
    Offline / Send Message
    JohnnyRaptor polycounter lvl 15
    string $Sel[] = `ls -sl`;
    string $targetAttr = $Sel[0] +"_parentConstraint1.target";
    string $connections[] = `listConnections $targetAttr`;
    string $connectionsCleaned[] = stringArrayRemoveDuplicates($connections);
    for ( $f in $connectionsCleaned ) {
    if ( $f != $con ) {
    print ( $f+ " is a Parent of " + $Sel[0] + "\n" );
    }
    }

    run this to get the name of the parentConstraint parent of your selection. its not perfect and will break if your selection has no parentConstraint applied or if you have renamed the parentconstraint, so will need some error handling in there..
  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
    Thanks Bartalon, it gave me a few ideas to go on.. but I couldn't have pulled this off myself.

    Johnny, I ran into a small issue
    The if statement didn't work due to $con not being declared. -I'm not sure what the intention to compare $f to $con is.. but if I print off $connectionsCleaned .. the first object in $connectionsCleaned[0] listed, is the name of the object i'm after. You sir are awesome!

    If the $f != $con argument is really important, let me know...
    Thanks Jonny, I owe ya a pint !
  • JohnnyRaptor
    Options
    Offline / Send Message
    JohnnyRaptor polycounter lvl 15
    Oh yeah, sorry about that, was gonna write it differently(little cleaner) to run through your selection if you have multiple objects selected, but didnt finish it and forgot to clean out the line.

    this is what i meant to post,

    string $Sel[] = `ls -sl`;
    string $targetAttr = $Sel[0] +"_parentConstraint1.target";
    string $connections[] = `listConnections $targetAttr`;
    string $connectionsCleaned[] = stringArrayRemoveDuplicates($connections);
    print ( $connectionsCleaned[0] + " is a Parent of " + $Sel[0] + "\n" );

    it checks your selection, adds the default naming convention for a parent constraint, then lists its connections, cleans out all the dupes, and lists the first item in the array, which will be the parent object of your selection.

    hope it helps :)
  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
    ahh no problem :)
    Thanks for thinking about adding the functionality of having multiple objects selected.

    What you have here returns exactly what I needed. It's compact & easy to follow
  • JohnnyRaptor
    Options
    Offline / Send Message
    JohnnyRaptor polycounter lvl 15
    small update:)

    cleaned it up a bit, automated it so it should work with any type of constraint, and added a bit of error handling,
    string $mySel[] = `ls -sl`;
    if ($mySel[0] != "") {
        //list all constraints in the scene.
        string $Cons[] = `ls -type "constraint"`;
        string $Buffer[];
        int $posArray;
    
        //find where in the array $Cons our selections constraint is and put that number in $posArray.
        for ($f1=0; $f1<size($Cons); $f1++){
            tokenize $Cons[$f1] "_" $Buffer;
            if ($Buffer[0] == $mySel[0]) {
                $posArray = $f1;
            }
        }
        
        //list all the connections of our target constraint, remove duplicates, and present data.
        string $targetAttr = $Cons[$posArray] +".target";
        string $connections[] = `listConnections $targetAttr`;
        string $connectionsCleaned[] = stringArrayRemoveDuplicates($connections);
        print ( $connectionsCleaned[0] + " is a Parent of " + $mySel[0] + "\n" );
    } else {
        print "Please make a selection";
    }
    
  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
    aaaannnd.. you just took it to a whole new level haha

    I think I understand almost everything your doing in there, except for the tokenize cmd.
    I have something new to learn today :) Thanks again!
  • passerby
    Options
    Offline / Send Message
    passerby polycounter lvl 12
    tokenize is used for spilting strings into multiple values. useful when you got a string with multiple bits of data in it and you only want part of it.
  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
    oh thanks passerby, i think I'm starting to get it...

    So he uses $con{$f1] as a way to cycle through the multiple objects selected.
    Then he tokenizes( or separates the string names) using the "_" flag and throws all those names into this $buffer
    Then takes the position of the first name in the buffer[0] , then compares it to the object that's currently selected then cycles , etc, and repeats through the cycle till it reaches the maximum number of objects selected..

    That's how it's making sense to me this morning.
    man, those For statements always scramble my brain up...
Sign In or Register to comment.