Pages

Tuesday, July 16, 2013

Tips: Script Nodes, Getting Joints Of Chain in Order, Renaming by Suffix

Hi there,

Here are a couple tips and code I wrote today on script node, listRelative and replacing names. Hope you find them helpful.

1. script nodes
Basically I used script nodes to save info to the Maya file and then I made a script job that can react to user actions in Maya. This example reacts to when an attribute changes on the scene. This script job turns off joint display in perspective view which could be helpful for cleaning a rig scene. To make a script node is very similar to making an expression.

//sourced one time
global proc setVisJointInPanel(string $objAttr)
{
print("[setVisJointInPanel]  Setting Visibility of Joints ...\n");
string $type = `addAttr -q -at $objAttr`;
//proceed only if valid type and thing we need exists
if( strcmp($type,"bool") == 0 ){
int $mode = `getAttr $objAttr`;
//string $window = `getPanel -withFocus`;
string $window = "modelPanel4";// only supports perspective view
//0 means dont show
modelEditor -e -joints $mode $window;
print("[setVisJointInPanel]  Complete !!!\n");
}

}



//-------The thing that holds our setting
string $objAttr = ("CHARACTER_SETTING"+".isShowJoint");

//-------


//script node stuff
if(`objExists $objAttr`){ 

string $type = `addAttr -q -at $objAttr`;

//proceed only if valid type and thing we need exists
if( strcmp($type,"bool") == 0 ){


print("[CHARACTER_snd]  Making Visibility of Joints Setup\n");
//we do somthing repeatedly anytime something else changes
scriptJob -runOnce false -attributeChange $objAttr "setVisJointInPanel($objAttr)"; 

}


}



2. listRelatives
This is some code I wrote that goes over the listRelatives command.

/**give me all joints in this single hierarchy in order from root first. it is not select -hierarchy because
all joints need to be on same joint chain.
@param string $joint -- root joint
*/
global proc string[]
na_selectJointsInHierarchy(string $joint)
{
    string $sel[] = `ls -sl`;
    string $result[] = {};
    string $childJoint[] = {};
    string $arg = "";
    
    if( size( `ls -type joint {$joint}` ) == 1 ){
        $arg = $joint;
        $result[size($result)] = $arg;
    }
    else{ error("[na_selectJointsInHierarchy]"); }
    
    //---->go through joint hierarchy
    print("[na_selectJointsInHierarchy] Going through Joint Hierarchy...\n");
    //use max iterations as all hierarchy
    int $maxJointLength = size(`listRelatives -ad -type joint $arg`); 
    
    
    //so we can start looping.
    $childJoint = `listRelatives -type joint $arg`; //not all decendants is important
    
    for( $i = 0; $i < $maxJointLength; $i++){
        
        if( size($childJoint) == 0  )
        {
            print("[na_selectJointsInHierarchy] Found no more joints so exiting!\n");
            $childJoint = {};
            break;
        }
        else if( size($childJoint) > 1  )
        {
            print("[na_selectJointsInHierarchy] Found more than 1 child joint so exiting!\n");
            $childJoint = {};
            break;
        }
        else if( size($childJoint) == 1  )
        {
            $arg = $childJoint[0];
            $result[size($result)] = $arg;
            //so we can go to next joint in hierarchy
            $childJoint = {};
            $childJoint = `listRelatives -type joint $arg`;
        }
        else{
            $childJoint = {};
        }
        
    }
    
    select -r $sel;
    
    return $result;
}


3. renaming stuff
This type of code may come in handy for renaming stuff.
Also a couple Python tips if used to MEL alot is
//python
//--remove dollar sign
//--remove ;
//use mostly cmds.usualMelFunction
//use import over source
//use def over global proc

/*usage
for($arg in `ls -sl`){
string $newName = replaceSuffix($arg, "_joint", "_anim"); 
rename $arg $newName;
}
*/

//--- replace old suffix of argument with replacement 
global proc string
replaceSuffix(string $arg, string $oldSuffix, string $replace)
{
string $result = "";
string $base = substring($arg,1,(size($arg)-size($oldSuffix)) );
if( strcmp( ($base+$oldSuffix), $arg ) != 0 ){error("Verify the old suffix is used on argument !!!\n");};//check if old suffix is used

$result = $base+$replace;

return $result;

}


print( "replaceSuffix(\"adfsfs_joint\", \"_joint\", \"_anim\")"+"\n");