// ============================================================== // Turn arm IK on or off while preserving the arm orientation. // ============================================================== global proc IkFk_switch( string $nameSpace, string $side, string $limb ) { string $currentSelection[] = `ls -sl`; // Check to see if anything is keyed in the arm, and if so, set the flag. int $keyWhenSwitching = 0; float $keysOnShoulder[] = `keyframe -q ($nameSpace + $side + "shoulder_JNT")`; float $keysOnElbow[] = `keyframe -q ($nameSpace + $side + "elbow_JNT")`; float $keysOnArmIk[] = `keyframe -q ($nameSpace + $side + "armIk_CON")`; float $keysOnArmPole[] = `keyframe -q ($nameSpace + $side + "armPole_CON")`; if ( `size $keysOnShoulder` != 0 || `size $keysOnElbow` != 0 || `size $keysOnArmIk` != 0 || `size $keysOnArmPole` != 0 ) $keyWhenSwitching = 1; // ---------------- // Turn arm IK off. // ---------------- if ( $limb == "arm" ) { if ( `getAttr ( $nameSpace + $side + "armIk_CON.enableIk")` == 1 ) { // 1) Get current arm joints rotation info. vector $shoulderRot = `xform -q -r -ro ($nameSpace + $side + "shoulder_JNT")`; vector $elbowRot = `xform -q -r -ro ($nameSpace + $side + "elbow_JNT")`; // 2) Disable the IK solver (if there are keys on the joints, the arm will pop back to those). setAttr ($nameSpace + $side + "armIk_CON.enableIk") 0; // 3) Match the joints rotations. setAttr ($nameSpace + $side + "shoulder_JNT.rx") ($shoulderRot.x); setAttr ($nameSpace + $side + "shoulder_JNT.ry") ($shoulderRot.y); setAttr ($nameSpace + $side + "shoulder_JNT.rz") ($shoulderRot.z); setAttr ($nameSpace + $side + "elbow_JNT.rx") ($elbowRot.x); // 4) If there are keys anywhere on the arm, then key the shoulder and elbow, and then delete the constraint. if ( $keyWhenSwitching ) { setKeyframe -breakdown 0 -hierarchy none -controlPoints 0 -shape 1 ($nameSpace + $side + "shoulder_JNT"); setKeyframe -breakdown 0 -hierarchy none -controlPoints 0 -shape 1 ($nameSpace + $side + "elbow_JNT"); } } // --------------- // Turn arm IK on. // --------------- else { // 1) Move the IK control to the current position of the wrist. pointConstraint -w 1 -o 0 0 0 ($nameSpace + $side + "wrist_JNT") ($nameSpace + $side + "armIk_CON"); // 2) If there are keys anywhere on the arm, then key the armIk and delete the constraint. if ( $keyWhenSwitching ) setKeyframe -breakdown 0 -hierarchy none -controlPoints 0 -shape 1 ($nameSpace + $side + "armIk_CON"); delete ($nameSpace + $side + "armIk_CON_pointConstraint1"); // 3) Move the pole vector where it should be to match the arm angle. pointConstraint -w 1 -o 0 0 0 ($nameSpace + $side + "armPoleDefault_LOC") ($nameSpace + $side + "armPole_CON"); // 4) If there are keys anywhere on the arm, then key the armPole and delete the constraint. if ( $keyWhenSwitching ) setKeyframe -breakdown 0 -hierarchy none -controlPoints 0 -shape 1 ($nameSpace + $side + "armPole_CON"); delete ($nameSpace + $side + "armPole_CON_pointConstraint1"); // 5) Enable the IK solver. setAttr ($nameSpace + $side + "armIk_CON.enableIk") 1; select -replace ($nameSpace + $side + "armIk_CON"); } } }