Sunday, March 10, 2013

Auto Rig Scrip with Ik Fk switch

This script was made for any Motion Capture data used on our different characters.

For the script to work it needs to have the controls in a separate Maya file where it can be imported to the scene.

The script attaches all the controls to the joints, bakes all the movements onto the controls, then reattaches all the controls to the joints for the animators to clean up the data. It also builds arm control joints that allow the rig to switch between Ik and Fk.

The script also makes it that when you switch between Ik and Fk the unused setting goes invisible.












 if (!objExists("BodyCTL"))
    {

    currentTime 0 ;
  
    select -r Hips ;
    SelectHierarchy;
  
    string $allJoints_array[];
    $allJoints_array = `ls -sl`;
  

  
    for($object in $allJoints_array)
    {
        setAttr ($object + ".rotateX") 0;
        setAttr ($object + ".rotateY") 0;
        setAttr ($object + ".rotateZ") 0;
    }
    setAttr "Hips.translateX" 0;
    setAttr "Hips.translateY" 0;
    setAttr "Hips.translateZ" 59.701;



//Constrain controles to Joints

file -import -type "mayaBinary" -ra true -mergeNamespacesOnClash false -namespace "medEnemie_Controles" -options "v=0;" -loadReferenceDepth "all" "C:/Users/rcampbell/Documents/Semester 2/animation/Enemies/MiddleEnemie/Skeleton/medEnemie_Controles.mb";

namespace -mv "medEnemie_Controles" ":" -force;

select -r Hips Spine Spine1 Head LeftShoulder LeftArm LeftForeArm RightShoulder RightArm RightForeArm LeftFoot RightFoot;

string $FKJoints_rot[];
$FKJoints_rot = `ls -sl`;

select -r BodyCTL SpineCTL Spine1CTL HeadCTL LShoulderCTL LArmFKCTL LForeArmFKCTL RShoulderCTL RArmFKCTL RForeArmFKCTL LFootCTL  RFootCTL;

string $FKCont_rot[];
$FKCont_rot = `ls -sl`;

for($i=0; size($FKJoints_rot)>$i; $i++){
    orientConstraint -weight 1 -mo $FKJoints_rot[$i] $FKCont_rot[$i] ;
}

select -r Hips LeftFoot RightFoot LeftBlade RightBlade LeftLeg RightLeg;

string $Joints_tran[];
$Joints_tran = `ls -sl`;

select -r BodyCTL LFootCTL RFootCTL LIkArmCTL RIkArmCTL LKneeCTL RKneeCTL;

string $Cont_tran[];
$Cont_tran = `ls -sl`;

for($i=0; size($Joints_tran)>$i; $i++){
    pointConstraint -weight 1 -mo $Joints_tran[$i] $Cont_tran[$i] ;
}

//Bake keys onto controles

select -r Hips ;
SelectHierarchy;
  
string $allJoints_array[];
$allJoints_array = `ls -sl`;

int $time_range_start = `findKeyframe -which first Hips`;
int $time_range_end = `findKeyframe -which last Hips`;
$time_range = $time_range_start+":"+$time_range_end;

bakeResults -simulation true -t $time_range -sampleBy 1 -disableImplicitControl true -preserveOutsideKeys true -sparseAnimCurveBake false -removeBakedAttributeFromLayer false -bakeOnOverrideLayer false -minimizeRotation true -controlPoints false -shape true {"BodyCTL", "SpineCTL", "Spine1CTL", "HeadCTL", "JawCTL", "LShoulderCTL", "LArmFKCTL", "LForeArmFKCTL", "LBladeFKCTL", "LIkArmCTL", "LBladeIKCTL", "LElbowCTL", "RShoulderCTL", "RArmFKCTL", "RForeArmFKCTL", "RBladeFKCTL", "RIkArmCTL", "RBladeIKCTL", "RElbowCTL", "LFootCTL", "LKneeCTL", "RFootCTL", "RKneeCTL" };

for($object in $allJoints_array)
{
    selectKey -add -k ($object + "_translateX") ;
    cutKey -animation keys -clear;
    selectKey -add -k ($object + "_translateY") ;
    cutKey -animation keys -clear;
    selectKey -add -k ($object + "_translateZ") ;
    cutKey -animation keys -clear;
    selectKey -add -k ($object + "_rotateX") ;
    cutKey -animation keys -clear;
    selectKey -add -k ($object + "_rotateY") ;
    cutKey -animation keys -clear;
    selectKey -add -k ($object + "_rotateZ") ;
   cutKey -animation keys -clear;
    selectKey -add -k ($object + "_scaleX") ;
    cutKey -animation keys -clear;
    selectKey -add -k ($object + "_scaleY") ;
    cutKey -animation keys -clear;
    selectKey -add -k ($object + "_scaleZ") ;
    cutKey -animation keys -clear;
    selectKey -add -k ($object + "_visibility") ;
    cutKey -animation keys -clear;
}

//Delete Controles

select -r BodyCTL_orientConstraint1 BodyCTL_pointConstraint1 LFootCTL_orientConstraint1 LFootCTL_pointConstraint1 RFootCTL_orientConstraint1 RFootCTL_pointConstraint1 SpineCTL_orientConstraint1 LKneeCTL_pointConstraint1 RKneeCTL_pointConstraint1 Spine1CTL_orientConstraint1 HeadCTL_orientConstraint1 LShoulderCTL_orientConstraint1 RShoulderCTL_orientConstraint1 LArmFKCTL_orientConstraint1 LIkArmCTL_pointConstraint1 RArmFKCTL_orientConstraint1 RIkArmCTL_pointConstraint1 LForeArmFKCTL_orientConstraint1 RForeArmFKCTL_orientConstraint1  ;
delete;

//Create Controle Joints

float $Opos[] = `xform -q -ws -translation LeftArm`;
float $POpos[] = `xform -q -ws -translation LeftForeArm`;
float $Ppos[] = `xform -q -ws -translation LeftBlade`;
joint -p $Opos[0] $Opos[1] $Opos[2] -name "LeftIkArmControle";
joint -p $POpos[0] $POpos[1] $POpos[2]-name "LeftIkForeArmControle";
joint -e -zso -oj xyz -sao yup LeftIkArmControle;
joint -p $Ppos[0] $Ppos[1] $Ppos[2] -name "LeftIkHandControle";
joint -e -zso -oj xyz -sao yup LeftIkForeArmControle;
select -cl  ;
joint -p $Opos[0] $Opos[1] $Opos[2] -name "LeftFkArmControle";
joint -p $POpos[0] $POpos[1] $POpos[2] -name "LeftFkForeArmControle";
joint -e -zso -oj xyz -sao yup LeftFkArmControle;
joint -p $Ppos[0] $Ppos[1] $Ppos[2] -name "LeftFkHandControle";
joint -e -zso -oj xyz -sao yup LeftFkForeArmControle;
select -cl  ;
float $Opos[] = `xform -q -ws -translation RightArm`;
float $POpos[] = `xform -q -ws -translation RightForeArm`;
float $Ppos[] = `xform -q -ws -translation RightBlade`;
joint -p $Opos[0] $Opos[1] $Opos[2] -name "RightIkArmControle";
joint -p $POpos[0] $POpos[1] $POpos[2] -name "RightIkForeArmControle" ;
joint -e -zso -oj xyz -sao yup RightIkArmControle;
joint -p $Ppos[0] $Ppos[1] $Ppos[2]  -name "RightIkHandControle";
joint -e -zso -oj xyz -sao yup RightIkForeArmControle;
select -cl  ;
joint -p$Opos[0] $Opos[1] $Opos[2]  -name "RightFkArmControle";
joint -p $POpos[0] $POpos[1] $POpos[2] -name "RightFkForeArmControle" ;
joint -e -zso -oj xyz -sao yup RightFkArmControle;
joint -p $Ppos[0] $Ppos[1] $Ppos[2] -name "RightFkHandControle";
joint -e -zso -oj xyz -sao yup RightFkForeArmControle;
select -cl  ;


//Create Ik Arm

select -r LeftIkArmControle.rotatePivot ;
select -add LeftIkHandControle.rotatePivot ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "LeftIkArmHandle";

parent LeftIkArmHandle LIkArmCTL ;

select -r RightIkArmControle.rotatePivot ;
select -add RightIkHandControle.rotatePivot ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "RightIkArmHandle";

parent RightIkArmHandle RIkArmCTL ;

//Parent Controle Joints to Shoulder

parent LeftIkArmControle LeftShoulder ;
parent LeftFkArmControle LeftShoulder ;
parent RightIkArmControle RightShoulder ;
parent RightFkArmControle RightShoulder ;

select -r LElbowCTL;
select -add LeftIkArmHandle ;
poleVectorConstraint -weight 1;

select -r RElbowCTL;
select -add RightIkArmHandle ;
poleVectorConstraint -weight 1;

//Arm

select -r LeftIkArmControle ;
select -add LeftArm ;
orientConstraint -mo -weight 1;

select -r LeftFkArmControle ;
select -add LeftArm ;
orientConstraint -mo -weight 1;

select -r RightIkArmControle ;
select -add RightArm ;
orientConstraint -mo -weight 1;

select -r RightFkArmControle ;
select -add RightArm ;
orientConstraint -mo -weight 1;

//ForeArm

select -r LeftIkForeArmControle ;
select -add LeftForeArm ;
orientConstraint -mo -weight 1;

select -r LeftFkForeArmControle ;
select -add LeftForeArm ;
orientConstraint -mo -weight 1;

select -r RightIkForeArmControle ;
select -add RightForeArm ;
orientConstraint -mo -weight 1;

select -r RightFkForeArmControle ;
select -add RightForeArm ;
orientConstraint -mo -weight 1;

//Hand

select -r LeftIkHandControle ;
select -add LeftBlade ;
orientConstraint -mo -weight 1;

select -r LeftFkHandControle ;
select -add LeftBlade ;
orientConstraint -mo -weight 1;

select -r RightIkHandControle ;
select -add RightBlade ;
orientConstraint -mo -weight 1;

select -r RightFkHandControle ;
select -add RightBlade ;
orientConstraint -mo -weight 1;

pointConstraint -weight 1 -mo BodyCTL  Hips;

//Orient constrain joint

select -r Hips Spine Spine1 Head Jaw LeftShoulder LeftFkArmControle LeftFkForeArmControle LeftFkHandControle RightShoulder RightFkArmControle RightFkForeArmControle RightFkHandControle LeftFoot  RightFoot;

string $FKJoints_rot2[];
$FKJoints_rot2 = `ls -sl`;

select -r BodyCTL SpineCTL Spine1CTL HeadCTL JawCTL LShoulderCTL LArmFKCTL LForeArmFKCTL LBladeFKCTL RShoulderCTL RArmFKCTL RForeArmFKCTL RBladeFKCTL LFootCTL  RFootCTL;

string $FKCont_rot2[];
$FKCont_rot2 = `ls -sl`;

for($i=0; size($FKJoints_rot2)>$i; $i++){
    orientConstraint -weight 1 -mo $FKCont_rot2[$i] $FKJoints_rot2[$i] ;
}

orientConstraint -weight 1 -mo LBladeIKCTL  LeftIkHandControle;

//Attach nodes

shadingNode -asUtility reverse;
connectAttr -f LIkFkSwitch.L_Ik2Fk LeftArm_orientConstraint1.LeftFkArmControleW1;
connectAttr -f LIkFkSwitch.L_Ik2Fk reverse1.inputX;
connectAttr -f reverse1.outputX LeftArm_orientConstraint1.LeftIkArmControleW0;

connectAttr -f LIkFkSwitch.L_Ik2Fk LeftForeArm_orientConstraint1.LeftFkForeArmControleW1;
connectAttr -f reverse1.outputX LeftForeArm_orientConstraint1.LeftIkForeArmControleW0;

connectAttr -f LIkFkSwitch.L_Ik2Fk LeftBlade_orientConstraint1.LeftFkHandControleW1;
connectAttr -f reverse1.outputX LeftBlade_orientConstraint1.LeftIkHandControleW0;

connectAttr -f RIkFkSwitch.R_Ik2Fk RightArm_orientConstraint1.RightFkArmControleW1;
connectAttr -f RIkFkSwitch.R_Ik2Fk reverse1.inputY;
connectAttr -f reverse1.outputY RightArm_orientConstraint1.RightIkArmControleW0;

connectAttr -f RIkFkSwitch.R_Ik2Fk RightForeArm_orientConstraint1.RightFkForeArmControleW1;
connectAttr -f reverse1.outputY RightForeArm_orientConstraint1.RightIkForeArmControleW0;

connectAttr -f RIkFkSwitch.R_Ik2Fk RightBlade_orientConstraint1.RightFkHandControleW1;
connectAttr -f reverse1.outputY RightBlade_orientConstraint1.RightIkHandControleW0;

//Visibility

connectAttr -f LIkFkSwitch.L_Ik2Fk LeftFkArmControle.visibility;
connectAttr -f LIkFkSwitch.L_Ik2Fk LeftFkForeArmControle.visibility;
connectAttr -f LIkFkSwitch.L_Ik2Fk LeftFkHandControle.visibility;

connectAttr -f LIkFkSwitch.L_Ik2Fk LArmFKCTL.visibility;
connectAttr -f LIkFkSwitch.L_Ik2Fk LForeArmFKCTL.visibility;
connectAttr -f LIkFkSwitch.L_Ik2Fk LBladeFKCTL.visibility;

connectAttr -f RIkFkSwitch.R_Ik2Fk RightFkArmControle.visibility;
connectAttr -f RIkFkSwitch.R_Ik2Fk RightFkForeArmControle.visibility;
connectAttr -f RIkFkSwitch.R_Ik2Fk RightFkHandControle.visibility;

connectAttr -f RIkFkSwitch.R_Ik2Fk RArmFKCTL.visibility;
connectAttr -f RIkFkSwitch.R_Ik2Fk RForeArmFKCTL.visibility;
connectAttr -f RIkFkSwitch.R_Ik2Fk RBladeFKCTL.visibility;

connectAttr -f reverse1.outputX LeftIkArmControle.visibility;
connectAttr -f reverse1.outputX LeftIkForeArmControle.visibility;
connectAttr -f reverse1.outputX LeftIkHandControle.visibility;

connectAttr -f reverse1.outputX LElbowCTL.visibility;
connectAttr -f reverse1.outputX LIkArmCTL.visibility;
connectAttr -f reverse1.outputX LBladeIKCTL.visibility;

connectAttr -f reverse1.outputY RightIkArmControle.visibility;
connectAttr -f reverse1.outputY RightIkForeArmControle.visibility;
connectAttr -f reverse1.outputY RightIkHandControle.visibility;

connectAttr -f reverse1.outputY RElbowCTL.visibility;
connectAttr -f reverse1.outputY RIkArmCTL.visibility;
connectAttr -f reverse1.outputY RBladeIKCTL.visibility;

//Create Ik Legs

select -r LeftUpLeg.rotatePivot ;
select -add LeftFoot.rotatePivot ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "LeftIkLegHandle";

select -r RightUpLeg.rotatePivot ;
select -add RightFoot.rotatePivot ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "RightIkLegHandle";

parent LeftIkLegHandle LLegCTL;

parent RightIkLegHandle RLegCTL;

select -r LKneeCTL;
select -add LeftIkLegHandle ;
poleVectorConstraint -weight 1;

select -r RKneeCTL;
select -add RightIkLegHandle ;
poleVectorConstraint -weight 1;

//Create Ik Toes

//First Toes

select -r LeftFirstToe ;
select -tgl LeftFirstToeEnd ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "LeftIkFirstToeHandle";

select -r RightFirstToe ;
select -tgl RightFirstToeEnd ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "RightIkFirstToeHandle";

parent LeftIkFirstToeHandle LFirstToeCTL;

parent RightIkFirstToeHandle RFirstToeCTL;

//Middle Toes

select -r LeftMiddleToe ;
select -tgl LeftMiddleToeEnd ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "LeftIkMiddleToeHandle";

select -r RightMiddleToe ;
select -tgl RightMiddleToeEnd ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "RightIkMiddleToeHandle";

parent LeftIkMiddleToeHandle LMiddleToeCTL;

parent RightIkMiddleToeHandle RMiddleToeCTL;

//Third Toe

select -r LeftThirdToe ;
select -tgl LeftThirdToeEnd ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "LeftIkThirdToeHandle";

select -r RightThirdToe ;
select -tgl RightThirdToeEnd ;
ikHandle -solver ikRPsolver;
rename "ikHandle1" "RightIkThirdToeHandle";

parent LeftIkThirdToeHandle LThirdToeCTL;

parent RightIkThirdToeHandle RThirdToeCTL;

//Heel Ik

select -r LeftHeel ;
select -tgl LeftHeelEnd ;
ikHandle -solver ikSCsolver;
rename "ikHandle1" "LeftIkHeelHandle";

select -r RightHeel ;
select -tgl RightHeelEnd ;
ikHandle -solver ikSCsolver;
rename "ikHandle1" "RightIkHeelHandle";

parent LeftIkHeelHandle LHeelCTL;

parent RightIkHeelHandle RHeelCTL;
}

else{
    error "BodyCTL exists!  Cannot zero it out.";
}

No comments:

Post a Comment