Maya, mel, question: solution for scriptJob


Hello! I faced a trouble while creating a rig system for my model…
I decided to create an IK spine system beside normal FK build, but the main requirement for such an IK spine is a
possibility to move root\hips without causing chest movement (For simpler realization of belly dance or something of its kind). That is why I created an ik spline chain (Joint chain) and oriented it in the opposit way to the deformation spine chain (the root of deforming chain is in the root of a model while the root of IK spine chain is in its chest), then I linked FK\IK joints to deformJoints and got quite a good result! For now movement of the spine begins from the chest and smoothly deforms to the root. As drivers of IKspline curve I used 2 joints to which a curve that controls the whole chain is binded. Therefore I can move controller of the root rather far from the model without making the spine stretch. However, this is a bit disorienting because you can move controller into position, where the root couldn’t possibly be and it will cause unpredictible animation.

Solution which I found:
To solve this mistake I decided to create a script, which should reposition the root controller to the closest acceptable point (Approximately), when you move it too far from the model. If distance beetwen the root controller and the last spine joint in the chain is more than 1/10 of arclenght, the root controller should be repositioned.
(I used Arclenght for calculating distance for posibility of painless scaling of the model)

To do this I wrote script using scriptJob feature:

// needed if scene with rig is referenced to scene hierarchy

proc string GetNameSpace(string $SceneName)

if (`file -q -shn -sn` != $SceneName)
string $NameSpace = `referenceQuery -namespace $SceneName`;
$NameSpace = `substitute ":" $NameSpace ""` + ":";
return $NameSpace; 
{return "";}    


// Translate Match procedure

proc TRmatch(string $target,string $control)

matchTransform -pos $control $target;


// Calculating distance beetwen points in space

proc float distance(string $target, string $control)

vector $TRtarget = `xform -q -t -ws $target`;
vector $TRcontrol = `xform -q -t -ws $control`;

float $dis, $dis_x2, $dis_y2, $dis_z2;
$dis_x2 = ($TRtarget.x - $TRcontrol.x)*($TRtarget.x - $TRcontrol.x);
$dis_y2 = ($TRtarget.y - $TRcontrol.y)*($TRtarget.y - $TRcontrol.y);
$dis_z2 = ($TRtarget.z - $TRcontrol.z)*($TRtarget.z - $TRcontrol.z);

$dis = sqrt($dis_x2 + $dis_y2 + $dis_z2);

return $dis;


//Initianting scroptJob and storing its ID in global value

proc SCRPT_Creator()

global int $scrptID;

$scrptID = `scriptJob -attributeChange (GetNameSpace("") + "Center_CTRL_Root.translate") run_spine_match`;


// killing script using global ID

proc SCRPT_Del()

global int $scrptID;

if($scrptID != 0)
scriptJob -k $scrptID;  


//Main, no coments =)

proc main()



//Procedure called by scriptJob on trigger

proc run_spine_match()

float $CurveLen = `arclen Center_IKSpline_Spine_curve`;
float $CompDis = $CurveLen/10;
string $target = GetNameSpace("") + "Center_IKSplineJoint_SpineINBTW_00";
string $control = GetNameSpace("") + "Center_CTRL_Root";
if ( (distance($target , $control)) > $CompDis)
        TRmatch($target , $control);



Trouble I faced:
Translate Match operation trigger scriptJob even if actual reposition not happaning, on other words, while this scripteJob alive maya cycling empty procedure nonstop and I can not select nothing or Undo because last do operation is runing empty procedure.

If some one faced something similar or know how to overcome this trouble – share you experience, please.