PDA

View Full Version : ScriptJon on attribute change but not auto key?


TechnicallyArtistic
10-26-2010, 06:22 PM
I've got a script I'm trying to run for an FK IK switch/match control. I've got it set up as a global proc in the expression editor, then in that expression I have a script job running that calls those global procs. The script job should be run if the arms FK/IK control gets switched, and then calls up a global proc that's an if statement, so which ever it's set to, it matches the correct rig to the other.

Anyway, what's going on is it seems to be running when key frames are set, and setting key frames in the wrong place too because of that some how. (I think it's running more than when I manually change the attribute, but when ever auto key hits it as well.

Here's the code I'm using for the script job trigger.
int $leftSnapSwitchOn = ` scriptJob -attributeChange "lf_Hand_CTRL.fkIkSwitch"
"leftFKIKMatch()" ` ;

Is there some better way to do this, so it doesn't run when it's getting keyed?

TechnicallyArtistic
10-26-2010, 06:52 PM
Part of the problem I'm encountering too is when I scrub the time line it's resetting it's self into all sorts of strange positions. Here's the whole code I have running.

Is it because it's an expression that it does this, and I should instead just run a script for this?

global proc leftFK2IK()
{
//Start of fk to ik matching code
// Get the XYZ Rotation values of the ik shoulder joint
float $L_ShoulderRX = `getAttr l_shoulder_a_ikRIG.rx`;
float $L_ShoulderRY = `getAttr l_shoulder_a_ikRIG.ry`;
float $L_ShoulderRZ = `getAttr l_shoulder_a_ikRIG.rz`;
// Get the Y Rotation value of the ik Elbow joint
float $L_ElbowRX = `getAttr l_elbow_a_ikRIG.rx`;
float $L_ElbowRY = `getAttr l_elbow_a_ikRIG.ry`;
float $L_ElbowRZ = `getAttr l_elbow_a_ikRIG.rz`;
// Get the XYZ Rotation values of the ik wrist joint
float $L_WristRX = `getAttr l_hand_a_ikRIG.rx`;
float $L_WristRY = `getAttr l_hand_a_ikRIG.ry`;
float $L_WristRZ = `getAttr l_hand_a_ikRIG.rz`;
//Apply those values to the fk bones and that will “snap” the fk arm to the ik arm
// Match the XYZ Rotation values for the fk shoulder joint
setAttr lf_shoulder_fkCTRL.rx $L_ShoulderRX;
setAttr lf_shoulder_fkCTRL.ry $L_ShoulderRY;
setAttr lf_shoulder_fkCTRL.rz $L_ShoulderRZ;
// Match the Y Rotation value for the fk elbow joint
setAttr lf_elbow_fkCTRL.rx $L_ElbowRX;
setAttr lf_elbow_fkCTRL.ry $L_ElbowRY;
setAttr lf_elbow_fkCTRL.rz $L_ElbowRZ;
// Match the XYZ Rotation values for the fk wrist joint
setAttr lf_Hand_CTRL.tx 0;
setAttr lf_Hand_CTRL.ty 0;
setAttr lf_Hand_CTRL.tz 0;
setAttr lf_Hand_CTRL.rx $L_WristRX;
setAttr lf_Hand_CTRL.ry $L_WristRY;
setAttr lf_Hand_CTRL.rz $L_WristRZ;
// End of fk to ik matching code


SetKey lf_shoulder_fkCTRL;
SetKey lf_elbow_fkCTRL;
SetKey lf_Hand_CTRL;
SetKey lf_Elbow_pv_CTRL;

setAttr -lock true "lf_Hand_CTRL.tx";
setAttr -lock true "lf_Hand_CTRL.ty";
setAttr -lock true "lf_Hand_CTRL.tz";


setAttr "lf_Hand_ctGRP_parentConstraint1.lf_hand_worldGRPW1" 0;
setAttr "lf_Hand_ctGRP_parentConstraint1.lf_elbow_fkCTRLW0" 1;

}

global proc leftIK2FK()
{

setAttr -lock false "lf_Hand_CTRL.tx";
setAttr -lock false "lf_Hand_CTRL.ty";
setAttr -lock false "lf_Hand_CTRL.tz";

//Start of ik to fk matching code
//Left Arm ik controls--> fk controls snap
// Get the XYZ Translation values from the left arm pole vector "dummy" node
// We don’t care about the rotation from the “dummy” node
float $LelbowDummyTX = `getAttr lf_Elbow_pv_ikLOC.tx`;
float $LelbowDummyTY = `getAttr lf_Elbow_pv_ikLOC.ty`;
float $LelbowDummyTZ = `getAttr lf_Elbow_pv_ikLOC.tz`;
// Get the XYZ Translation values from the left Arm ik Control "dummy"
float $LwristDummyTX = `getAttr lf_Hand_pv_fkLOC.tx`;
float $LwristDummyTY = `getAttr lf_Hand_pv_fkLOC.ty`;
float $LwristDummyTZ = `getAttr lf_Hand_pv_fkLOC.tz`;
float $LwristDummyRX = `getAttr lf_Hand_pv_fkLOC.rx`;
float $LwristDummyRY = `getAttr lf_Hand_pv_fkLOC.ry`;
float $LwristDummyRZ = `getAttr lf_Hand_pv_fkLOC.rz`;


setAttr "lf_Hand_ctGRP_parentConstraint1.lf_hand_worldGRPW1" 1;
setAttr "lf_Hand_ctGRP_parentConstraint1.lf_elbow_fkCTRLW0" 0;

//Move the Left arm Pole Vector control to the "dummy" position
setAttr lf_Elbow_pv_CTRL.tx $LelbowDummyTX;
setAttr lf_Elbow_pv_CTRL.ty $LelbowDummyTY;
setAttr lf_Elbow_pv_CTRL.tz $LelbowDummyTZ;
// Move and rotate the left arm ik control to the "dummy" position
setAttr lf_Hand_CTRL.tx $LwristDummyTX;
setAttr lf_Hand_CTRL.ty $LwristDummyTY;
setAttr lf_Hand_CTRL.tz $LwristDummyTZ;
setAttr lf_Hand_CTRL.rx $LwristDummyRX;
setAttr lf_Hand_CTRL.ry $LwristDummyRY;
setAttr lf_Hand_CTRL.rz $LwristDummyRZ;

//End of ik to fk matching code


SetKey lf_shoulder_fkCTRL;
SetKey lf_elbow_fkCTRL;
SetKey lf_Hand_CTRL;
SetKey lf_Elbow_pv_CTRL;

setAttr "lf_Hand_ctGRP_parentConstraint1.lf_hand_worldGRPW1" 1;
setAttr "lf_Hand_ctGRP_parentConstraint1.lf_elbow_fkCTRLW0" 0;


}


global proc rightFK2IK()
{
//Start of fk to ik matching code
// Get the XYZ Rotation values of the ik shoulder joint
float $R_ShoulderRX = `getAttr r_shoulder_a_ikRIG.rx`;
float $R_ShoulderRY = `getAttr r_shoulder_a_ikRIG.ry`;
float $R_ShoulderRZ = `getAttr r_shoulder_a_ikRIG.rz`;
// Get the Y Rotation value of the ik Elbow joint
float $R_ElbowRX = `getAttr r_elbow_a_ikRIG.rx`;
float $R_ElbowRY = `getAttr r_elbow_a_ikRIG.ry`;
float $R_ElbowRZ = `getAttr r_elbow_a_ikRIG.rz`;
// Get the XYZ Rotation values of the ik wrist joint
float $R_WristRX = `getAttr r_hand_a_ikRIG.rx`;
float $R_WristRY = `getAttr r_hand_a_ikRIG.ry`;
float $R_WristRZ = `getAttr r_hand_a_ikRIG.rz`;
//Apply those values to the fk bones and that will “snap” the fk arm to the ik arm
// Match the XYZ Rotation values for the fk shoulder joint
setAttr rt_shoulder_fkCTRL.rx $R_ShoulderRX;
setAttr rt_shoulder_fkCTRL.ry $R_ShoulderRY;
setAttr rt_shoulder_fkCTRL.rz $R_ShoulderRZ;
// Match the Y Rotation value for the fk elbow joint
setAttr rt_elbow_fkCTRL.rx $R_ElbowRX;
setAttr rt_elbow_fkCTRL.ry $R_ElbowRY;
setAttr rt_elbow_fkCTRL.rz $R_ElbowRZ;
// Match the XYZ Rotation values for the fk wrist joint
setAttr rt_Hand_CTRL.tx 0;
setAttr rt_Hand_CTRL.ty 0;
setAttr rt_Hand_CTRL.tz 0;
setAttr rt_Hand_CTRL.rx $R_WristRX;
setAttr rt_Hand_CTRL.ry $R_WristRY;
setAttr rt_Hand_CTRL.rz $R_WristRZ;
// End of fk to ik matching code


SetKey rt_shoulder_fkCTRL;
SetKey rt_elbow_fkCTRL;
SetKey rt_Hand_CTRL;
SetKey rt_Elbow_pv_CTRL;

setAttr -lock true "rt_Hand_CTRL.tx";
setAttr -lock true "rt_Hand_CTRL.ty";
setAttr -lock true "rt_Hand_CTRL.tz";

setAttr "rt_Hand_ctGRP_parentConstraint1.rt_hand_worldGRPW1" 0;
setAttr "rt_Hand_ctGRP_parentConstraint1.rt_elbow_fkCTRLW0" 1;

}

global proc rightIK2FK()
{

setAttr -lock false "rt_Hand_CTRL.tx";
setAttr -lock false "rt_Hand_CTRL.ty";
setAttr -lock false "rt_Hand_CTRL.tz";


//Start of ik to fk matching code
//right Arm ik controls--> fk controls snap
// Get the XYZ Translation values from the right arm pole vector "dummy" node
// We don’t care about the rotation from the “dummy” node
float $RelbowDummyTX = `getAttr rt_Elbow_pv_ikLOC.tx`;
float $RelbowDummyTY = `getAttr rt_Elbow_pv_ikLOC.ty`;
float $RelbowDummyTZ = `getAttr rt_Elbow_pv_ikLOC.tz`;
// Get the XYZ Translation values from the right Arm ik Control "dummy"
float $RwristDummyTX = `getAttr rt_Hand_pv_fkLOC.tx`;
float $RwristDummyTY = `getAttr rt_Hand_pv_fkLOC.ty`;
float $RwristDummyTZ = `getAttr rt_Hand_pv_fkLOC.tz`;
float $RwristDummyRX = `getAttr rt_Hand_pv_fkLOC.rx`;
float $RwristDummyRY = `getAttr rt_Hand_pv_fkLOC.ry`;
float $RwristDummyRZ = `getAttr rt_Hand_pv_fkLOC.rz`;


setAttr "rt_Hand_ctGRP_parentConstraint1.rt_hand_worldGRPW1" 1;
setAttr "rt_Hand_ctGRP_parentConstraint1.rt_elbow_fkCTRLW0" 0;

//Move the right arm Pole Vector control to the "dummy" position
setAttr rt_Elbow_pv_CTRL.tx $RelbowDummyTX;
setAttr rt_Elbow_pv_CTRL.ty $RelbowDummyTY;
setAttr rt_Elbow_pv_CTRL.tz $RelbowDummyTZ;
// Move and rotate the right arm ik control to the "dummy" position
setAttr rt_Hand_CTRL.tx $RwristDummyTX;
setAttr rt_Hand_CTRL.ty $RwristDummyTY;
setAttr rt_Hand_CTRL.tz $RwristDummyTZ;
setAttr rt_Hand_CTRL.rx $RwristDummyRX;
setAttr rt_Hand_CTRL.ry $RwristDummyRY;
setAttr rt_Hand_CTRL.rz $RwristDummyRZ;

//End of ik to fk matching code


SetKey rt_shoulder_fkCTRL;
SetKey rt_elbow_fkCTRL;
SetKey rt_Hand_CTRL;
SetKey rt_Elbow_pv_CTRL;

setAttr "rt_Hand_ctGRP_parentConstraint1.rt_hand_worldGRPW1" 1;
setAttr "rt_Hand_ctGRP_parentConstraint1.rt_elbow_fkCTRLW0" 0;


}




// Depending what the FK IK Switch is set to, run one of the two global procs above.
global proc leftFKIKMatch()
{
float $lhandSetting = `getAttr lf_Hand_CTRL.fkIkSwitch`;


if ($lhandSetting == 0) leftFK2IK();

if ($lhandSetting == 1) leftIK2FK();


}


global proc rightFKIKMatch()
{

float $rhandSetting = `getAttr rt_Hand_CTRL.fkIkSwitch`;

if ($rhandSetting == 0) rightFK2IK();

if ($rhandSetting == 1) rightIK2FK();

}


//Run this script job when the attribute changes on the fk ik switch.
int $leftSnapSwitchOn = ` scriptJob -attributeChange "lf_Hand_CTRL.fkIkSwitch"
"leftFKIKMatch()" ` ;

int $rightSnapSwitchOn = ` scriptJob -attributeChange "rt_Hand_CTRL.fkIkSwitch"
"rightFKIKMatch()" ` ;

TechnicallyArtistic
10-26-2010, 11:47 PM
I realized the big problem with this script is that the FK IK switch control is transitioning during animation. When it makes that flip from FK to IK on the control, the script job is going to run, which isn't a good time for it to run.

What I would need is something that could match the two rigs only at a button press rather than during animation.

CGTalk Moderation
10-26-2010, 11:47 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.