How can a script run automatically when I manually move an object?

I have 2 joints (joint1 and joint2) and when I manually move joint1, I want joint2 to move according to an expression that I have created.

I wrote the following in the expression editor:
################################
$ty = getAttr joint1.ty;

if($ty< 1){
setAttr joint2.ty $ty;
}
else if ($ty < 2){
setAttr joint2.ty (1+($ty-1)*0.5);
}
else{
setAttr joint2.ty ($ty-0.5);
}
################################

and every time I translate joint1 in Y and press the “Edit” button in the Expression Editor, it works.

What I want, however, is to be able to move joint1 and not have to press “Edit” in order for joint2 to move.

How can I achieve this?

Thank you,

Which DCC package are you using?

You could convert your math into utility nodes so there is a direct connection between the two joints or create a script job if you are in Maya.

Are you actually trying to run commands when you make a change, or do you want to simply link the attributes?

If the latter, you don’t need to use getAttr and setAttr
(I may have changed some of your values as I tinkered.)


if(joint1.translateY< 1){
joint2.translateY  = joint1.translateY;
}
else if(joint1.translateY< 2){
joint2.translateY  = (joint1.translateY*0.5);
}
else{
joint2.translateY = (joint1.translateY-0.5);
}

Ok…i just wanted to connect the attributes.

Thanks a lot!