Pages

Wednesday, June 13, 2012

Tip Make Cut Geometry Follow Rig Yet Separate From Rig Joints


Hi,

I was trying to hide some joints of rig but because some geo was parented to the joints it made it a little difficult.  So here is a tool I wrote to help separate some simple polygon geometry pieces from the animation rig while still following the animation rig.

Some tips it has are creating a no child duplicate of a joint with identical orient and rotate order.  Making connections in MEL and a little on hierarchy. (note I commented out the error checking so it was easier to download).


Hope you find this helpful.

Cheers,
Nate


global proc
na_getGeoInWorldWhileFollowAnimRig_unitTest()
{
    select -d;
    joint -p 0 0 0 ;
    joint -p 1 0 0 ;
    joint -e -zso -oj xyz -sao yup joint1;
    joint -p 2 0 0 ;
    joint -e -zso -oj xyz -sao yup joint2;
    joint -p 3 0 0 ;
    joint -e -zso -oj xyz -sao yup joint3;
    polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1; 
    move -rpr 1 0 0 ;
    polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;
    move -rpr 2 0 0 ;
    polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;
    move -rpr 3 0 0 ;
    select -r pCube1 ;
    parent pCube1 joint1 ;
    select -r pCube2 ;
    parent pCube2 joint2 ;
    select -r pCube3 ;
    parent pCube3 joint3 ;
    
    select -r pCube1 ;
    move 0 0 0 pCube1.scalePivot pCube1.rotatePivot ;
    select -r pCube3 ;
    select -cl  ;
    select -r pCube2 ;
    move 1 0 0 pCube2.scalePivot pCube2.rotatePivot ;
    select -r pCube3 ;
    move 2 0 0 pCube3.scalePivot pCube3.rotatePivot ;
    
    na_getGeoInWorldWhileFollowAnimRig({"pCube1","pCube2","pCube3"});
    
}
/** Get geo in world hierarchy while following animation rig
@pre geo single instance shape and it is part of animation rig by being child of a joint or transform
*/
global proc string[]
na_getGeoInWorldWhileFollowAnimRig(string $geoArray[])
{
    //na_assertObjectExist($geoArray);
    //na_assertTypeInList( $geoArray, {"transform"} );
    
    string $geo[] = $geoArray;
    string $parent[] = {};
    
    //uses single geo instance assumption, and joint parent assumption
    $parent = `listRelatives -parent -f $geo`;
    //na_assertTypeInList( $parent, {"joint","transform"} );
    //na_assertSizeEqualArg( $parent, size($geo) );
    
    
    //parent $geo to the world so
    parent -w $geo;
    
    //goal is to have geometry scaling and moving along with rig in separate hierarchy
    //
    string $to = "";
    string $toArray[] = {};
    string $toTemp[] = {};
    
    for($i = 0; $i < size($geo); $i++)
    {
        $from = $parent[$i];
        
        //to preserve orient we make a duplicate
        $toTemp = `duplicate -rr $from`;
        //na_assertSizeEqualArg( $toTemp, 1 );
        $to =  $toTemp[0];
        delete `listRelatives -children -f $to`;

        
        //geometry almost moving with rig
        parentConstraint -mo -weight 1 $from $to;

        connectAttr -f ($from+".scaleX") ($to+".scaleX");
        connectAttr -f ($from+".scaleY") ($to+".scaleY");
        connectAttr -f ($from+".scaleZ") ($to+".scaleZ");
        
        //geometry moving with rig
        if( size(`listRelatives -parent -f $to`) != 0 ){
        parent -w $to;
        }
        parent $geo[$i] $to;
        
        $toArray[size($toArray)] = $to;
    }

    
    return $toArray;
}