// ==================================== // Reposition joint // ==================================== // // Purpose: // To be able to tweak any joint's position while maintaining their correct orientation // and without messing their parent's or children's axis. And keeping their channel values clean. // // Also, you get to see a visual indication of where the joint was previously while you tweak its position! // // Usage: // repositionJoint // // --------------------------------------------------------------------------------------------------- // =================================== // Do reposition joint // =================================== global proc doRepositionJoint() { // ----------------------------------------------- // Get the joint, its parent and children. // ----------------------------------------------- string $sel[] = `ls -sn -sl`; //print ("$sel[0] = " + $sel[0] ); string $joint = $sel[0]; //print $joint; string $children[] = `listRelatives -c $joint`; //print $children; string $parents[] = `listRelatives -p $joint`; //print $parents; string $checkTmp[] = `ls -type "joint" $joint`; string $jointBuffer[]; string $childrenBuffer[]; int $numTokens = `tokenize $joint "|" $jointBuffer`; //print $jointBuffer; if ( `size $jointBuffer` > 1 ) error ("repositionJoint: The name of the joint \"" + $jointBuffer[`size $jointBuffer` - 1] + "\" is not unique!"); for ($i=0;$i<`size $parents`;$i++) { string $parentBuffer[]; int $numTokens = `tokenize $parents[$i] "|" $parentBuffer`; //print $parentBuffer; if ( (`size $parentBuffer` > 1) || (eval("catch(`select " + $parents[$i] + "`)")) ) error ("repositionJoint: The name of the joint's parent \"" + $parentBuffer[`size $parentBuffer` - 1] + "\" is not unique!"); } for ($i=0;$i<`size $children`;$i++) { string $childrenBuffer[]; int $numTokens = `tokenize $children[$i] "|" $childrenBuffer`; //print $childrenBuffer; if ( (`size $childrenBuffer` > 1) || (eval("catch(`select " + $children[$i] + "`)")) ) error ("repositionJoint: The name of the joint's child \"" + $childrenBuffer[`size $childrenBuffer` - 1] + "\" is not unique!"); } if ( endsWith($joint, "_repos") ) error "repositionJoint: This joint is already being repositioned!"; if ( $checkTmp[0] == "" ) error "repositionJoint: This script only works with joints!"; // --------------------------------------------------------------------- // Create a templated version of the joints, for visual reference. // --------------------------------------------------------------------- select -cl; // Create templated joint for the parent. string $whatType[] = `ls -type joint -showType $parents[0]`; //print ("$whatType[0] = " + $whatType[0] ); if ( `checkBox -q -v reorientParentCB` ) { if ( $parents[0] != "" && $whatType[0] != "" ) { joint -p 0 0 0 -n ($parents[0] + "_tempRef");//print ($parents[0] + "_tempRef"); toggle -state on -template ($parents[0] + "_tempRef"); delete (`pointConstraint -w 1 -o 0 0 0 $parents[0] ($parents[0] + "_tempRef")`); setAttr ($parents[0] + "_tempRef.radius") `getAttr ($parents[0] + ".radius")`; //print ("radius = " + (`getAttr ($parents[0] + ".radius")`)); } } // Create templated joint for the selected joint. joint -p 0 0 0 -n ($joint + "_tempRef"); delete (`pointConstraint -w 1 -o 0 0 0 $joint ($joint + "_tempRef")`); setAttr ($joint + "_tempRef.radius") `getAttr ($joint + ".radius")`; // Create templated joints for the children. for ($i=0;$i<`size $children`;$i++) { select -cl; joint -p 0 0 0 -n ($children[$i] + "_tempRef"); delete (`pointConstraint -w 1 -o 0 0 0 $children[$i] ($children[$i] + "_tempRef")`); parent ($children[$i] + "_tempRef") ($joint + "_tempRef"); setAttr ($children[$i] + "_tempRef.radius") `getAttr ($children[$i] + ".radius")`; //print ("radius = " + (`getAttr ($parents[0] + ".radius")`)); setAttr ($children[$i] + "_tempRef.visibility") 0; } toggle -state on -template ($joint + "_tempRef"); select -cl; // ------------------------------------- // Create the repositioning object. // ------------------------------------- string $reposJointMain = `curve -d 1 -p 0 0 0 -p 0.75 0 0 -p 1 0.05 0 -p 1.05 0 0 -p 1 -0.05 0 -p 0.75 0 0 -p 1 0 0.05 -p 1.05 0 0 -p 1 0 -0.05 -p 1 0.05 0 -p 1 0 0.05 -p 1 -0.05 0 -p 1 0 -0.05 -p 0.75 0 0 -p 0 0 0 -p -0.75 0 0 -p -1 0.05 0 -p -1.05 0 0 -p -1 -0.05 0 -p -0.75 0 0 -p -1 0 0.05 -p -1.05 0 0 -p -1 0 -0.05 -p -1 0.05 0 -p -1 0 0.05 -p -1 -0.05 0 -p -1 0 -0.05 -p -0.75 0 0 -p 0 0 0 -p 0 0.75 0 -p 0 1 -0.05 -p 0 1.05 0 -p 0 1 0.05 -p 0 0.75 0 -p -0.05 1 0 -p 0 1.05 0 -p 0.05 1 0 -p 0 1 0.05 -p -0.05 1 0 -p 0 1 -0.05 -p 0.05 1 0 -p 0 0.75 0 -p 0 0 0 -p 0 -0.75 0 -p 0 -1 -0.05 -p 0 -1.05 0 -p 0 -1 0.05 -p 0 -0.75 0 -p -0.05 -1 0 -p 0 -1.05 0 -p 0.05 -1 0 -p 0 -1 -0.05 -p -0.05 -1 0 -p 0 -1 0.05 -p 0.05 -1 0 -p 0 -0.75 0 -p 0 0 0 -p 0 0 -0.75 -p 0 0.05 -1 -p 0 0 -1.05 -p 0 -0.05 -1 -p 0 0 -0.75 -p -0.05 0 -1 -p 0 0 -1.05 -p 0.05 0 -1 -p 0 0.05 -1 -p -0.05 0 -1 -p 0 -0.05 -1 -p 0.05 0 -1 -p 0 0 -0.75 -p 0 0 0 -p 0 0 0.75 -p 0 0.05 1 -p 0 0 1.05 -p 0 -0.05 1 -p 0 0 0.75 -p -0.05 0 1 -p 0 0 1.05 -p 0.05 0 1 -p 0 0.05 1 -p -0.05 0 1 -p 0 -0.05 1 -p 0.05 0 1 -p 0 0 0.75 -k 0 -k 1 -k 2 -k 3 -k 4 -k 5 -k 6 -k 7 -k 8 -k 9 -k 10 -k 11 -k 12 -k 13 -k 14 -k 15 -k 16 -k 17 -k 18 -k 19 -k 20 -k 21 -k 22 -k 23 -k 24 -k 25 -k 26 -k 27 -k 28 -k 29 -k 30 -k 31 -k 32 -k 33 -k 34 -k 35 -k 36 -k 37 -k 38 -k 39 -k 40 -k 41 -k 42 -k 43 -k 44 -k 45 -k 46 -k 47 -k 48 -k 49 -k 50 -k 51 -k 52 -k 53 -k 54 -k 55 -k 56 -k 57 -k 58 -k 59 -k 60 -k 61 -k 62 -k 63 -k 64 -k 65 -k 66 -k 67 -k 68 -k 69 -k 70 -k 71 -k 72 -k 73 -k 74 -k 75 -k 76 -k 77 -k 78 -k 79 -k 80 -k 81 -k 82 -k 83`; $reposJointMain = `rename ($joint + "_repos")`; setAttr ($reposJointMain + ".scale") 2 2 2; delete (`orientConstraint -w 1 -o 0 0 0 $joint $reposJointMain`); color -ud 6; group -n ($reposJointMain + "_ZERO"); parent ($reposJointMain + "_ZERO") $joint; setAttr ($reposJointMain + "_ZERO.translate") 0 0 0; setAttr ($reposJointMain + "_ZERO.rotate") 0 0 0; setAttr ($reposJointMain + ".rotate") 0 0 0; parent -world ($reposJointMain + "_ZERO"); pointConstraint -w 1 -o 0 0 0 $reposJointMain $joint; select -cl; if ( `checkBox -q -v reorientParentCB` ) if ( $parents[0] != "" && $whatType[0] != "" ) parent ($parents[0] + "_tempRef") ($reposJointMain + "_ZERO"); else parent ($joint + "_tempRef") ($reposJointMain + "_ZERO"); // ------------------------------------- // Create children locators. // ------------------------------------- for ($i=0;$i<`size $children`;$i++) { if ( $children[0] != "" ) { spaceLocator -p 0 0 0 -n ($children[$i] + "_Child_LOC"); delete (`pointConstraint -w 1 -o 0 0 0 $children[$i] ($children[$i] + "_Child_LOC")`); setAttr ($children[$i] + "_Child_LOC.visibility") 0; parent ($children[$i] + "_Child_LOC") ($reposJointMain + "_ZERO"); pointConstraint -w 1 -o 0 0 0 ($children[$i] + "_Child_LOC") $children[$i]; orientConstraint -w 1 -mo ($children[$i] + "_Child_LOC") $children[$i]; } } // ------------------------------------- // Define the aim axis based on the UI. // ------------------------------------- int $aimAxisX, $aimAxisY, $aimAxisZ = 0; if ( `radioCollection -q -sl aimChoiceRB` == "aimAxisX" ) $aimAxisX = 1; else if ( `radioCollection -q -sl aimChoiceRB` == "aimAxisY" ) $aimAxisY = 1; else if ( `radioCollection -q -sl aimChoiceRB` == "aimAxisZ" ) $aimAxisZ = 1; else if ( `radioCollection -q -sl aimChoiceRB` == "aimAxisMinusX" ) $aimAxisX = -1; else if ( `radioCollection -q -sl aimChoiceRB` == "aimAxisMinusY" ) $aimAxisY = -1; else if ( `radioCollection -q -sl aimChoiceRB` == "aimAxisMinusZ" ) $aimAxisZ = -1; // ------------------------------------- // Create parent locator. // ------------------------------------- if ( `checkBox -q -v reorientParentCB` ) { if ( $parents[0] != "" && $whatType[0] != "" ) { spaceLocator -p 0 0 0 -n ($reposJointMain + "_Parent_LOC"); parent ($reposJointMain + "_Parent_LOC") $parents[0]; setAttr ($reposJointMain + "_Parent_LOC.translate") 3 0 0; setAttr ($reposJointMain + "_Parent_LOC.rotate") 0 0 0; parent -world ($reposJointMain + "_Parent_LOC"); aimConstraint -offset 0 0 0 -weight 1 -aimVector $aimAxisX $aimAxisY $aimAxisZ -upVector 1 0 0 -worldUpType "object" -worldUpObject ($reposJointMain + "_Parent_LOC") $reposJointMain $parents[0]; setAttr ($reposJointMain + "_Parent_LOC.visibility") 0; parent ($joint + "_repos_Parent_LOC") ($reposJointMain + "_ZERO"); } } // ------------------------------------- // Create selected joint UP locator. // ------------------------------------- if ( $children[0] != "" ) { spaceLocator -p 0 0 0 -n ($reposJointMain + "_Up_LOC"); parent ($reposJointMain + "_Up_LOC") $joint; setAttr ($reposJointMain + "_Up_LOC.translate") 3 0 0; setAttr ($reposJointMain + "_Up_LOC.rotate") 0 0 0; parent ($reposJointMain + "_Up_LOC") $reposJointMain; aimConstraint -offset 0 0 0 -weight 1 -aimVector $aimAxisX $aimAxisY $aimAxisZ -upVector 1 0 0 -worldUpType "object" -worldUpObject ($reposJointMain + "_Up_LOC") ($children[0] + "_Child_LOC") $joint; setAttr ($reposJointMain + "_Up_LOC.visibility") 0; } select -r $reposJointMain; setFocus `getPanel -wf`; } // ==================================== // Freeze joint after repositioning // ==================================== global proc freezeJointAfterRepositioning() { // ---------------------------------------------------------------- // Get the selected joint and find its parent and its children. // ---------------------------------------------------------------- string $sel[] = `ls -sl`; string $checkTmp[] = `ls -type "joint" $sel[0]`; if ( $checkTmp[0] != "" ) error "repositionJoint: You have to select the control object, NOT the joint itself!"; string $buffer[]; int $numTokens = `tokenize $sel[0] "_" $buffer`; //print $buffer; string $joint = $buffer[0]; for ($i=1;$i<$numTokens-1;$i++) { $joint += "_" + $buffer[$i]; } string $children[] = `listRelatives -c $joint`; //print $child; string $parents[] = `listRelatives -p $joint`; //print $parent; int $isParentTweaked = 0; // ------------------------------------- // Clean-up after ourselves. // ------------------------------------- if ( `objExists ($parents[0] + "_tempRef")` ) delete ($parents[0] + "_tempRef"); else delete ($joint + "_tempRef"); if ( `objExists ($parents[0] + "_aimConstraint1")` ) { delete ($parents[0] + "_aimConstraint1"); $isParentTweaked = 1; } if ( `objExists ($joint + "_aimConstraint1")` ) delete ($joint + "_aimConstraint1"); for ($i=0;$i<`size $children`;$i++) { if ( `objExists ($children[$i] + "_pointConstraint1")` ) delete ($children[$i] + "_pointConstraint1") ($children[$i] + "_orientConstraint1"); } delete ($joint + "_pointConstraint1") ($joint + "_repos_ZERO"); // ------------------------------------- // Freeze the transforms on the joint. // ------------------------------------- if ( $isParentTweaked ) makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 $parents[0]; else makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 $joint; select -r $joint; setFocus `getPanel -wf`; } // =============================== // repositionJoint UI // =============================== global proc repositionJoint() { // -------------------------- // build UI // -------------------------- if (`window -exists repositionJointWindow` ) deleteUI repositionJointWindow; int $w = 175; int $cw = 58; window -tlb off -rtf on -sizeable true -width 50 -height 40 -title ("Reposition Joint:") repositionJointWindow; columnLayout -adjustableColumn false -p repositionJointWindow mainColumn; frameLayout -mw 5 -bv on -lv off -collapsable false -borderStyle "etchedOut" -p mainColumn meshFrame; columnLayout -adjustableColumn false -p meshFrame firstColumn; text -en on -al "left" "Aim Axis:"; rowColumnLayout -nc 3 -cw 1 $cw -cw 2 $cw -cw 3 $cw; radioCollection aimChoiceRB; radioButton -label "X" aimAxisX; radioButton -label "Y" aimAxisY; radioButton -label "Z" aimAxisZ; radioButton -label "- X" aimAxisMinusX; radioButton -label "- Y" aimAxisMinusY; radioButton -label "- Z" aimAxisMinusZ; radioCollection -e -sl aimAxisZ aimChoiceRB; setParent ..; separator -h 4-st "in" -w $w; checkBox -h 20 -v 1 -ann "" -al "left" -vis 1 -label "Reorient Parent" reorientParentCB; button -label "Reposition Joint" -h 22 -w $w -ann "" -c doRepositionJoint; separator -st none -h 2; button -label "Freeze Joint!" -h 22 -w $w -ann "Freeze joints (after repositioning)" -c freezeJointAfterRepositioning; separator -st none -h 4; setParent ..; setParent ..; showWindow repositionJointWindow; }