Maya Mel/Script expression help with a Rig

Maya Mel/Script expression help with a Rig

Anonymous
Not applicable
1,699 Views
5 Replies
Message 1 of 6

Maya Mel/Script expression help with a Rig

Anonymous
Not applicable

I need help with a rig im working on in maya, i have no experience with scripting/coding (my background is in modelling, shading). 

 

i need to control one attribute of a certain node, depending on what direction another locator is moving(relative to itself). this is to automate the rig so I don't have to manually animate it.

 

i think it can be done with expressions (expression editor).  maybe someone can show me a test scene with 2 locators if they think they can help. 

 

 

__________________________________

 

i was thinking if it could work with condition if statements, but the tutorial i watched only showed how to do it with more than or less than fuction, which i dont think will work in this case

this is what i need: 

 

>so if locator 1 is moving in one direction (eg. +X) it will toggle locator 2 to become 0.2 in translateX

>then as soon as locator 1 starts moving in the opposite direction (eg. -X) it will toggle locator 2 to become -0.2 in translateX

 

so its not as simple as making an expression like this locator1.translateX = locator2.translateX

 

 

Accepted solutions (1)
1,700 Views
5 Replies
Replies (5)
Message 2 of 6

mcw0
Advisor
Advisor

Condition nodes will work perfectly for what you want to do.  You will need 2 condition nodes.  One for greater than zero and the other for less than zero.  Connect locator1.tx to the firstTerm of each condition.  Set the colorIfTrueR value to .2 for the greater than condition.  Set the colorIfTrueR to -.2 for the less than condition.  And now connect the outColorR of the less than condition to the colorIfFalseR of your greater than condition.  Lastly, connect the outColorR of the greater than condition to locator2.tx

Message 3 of 6

Anonymous
Not applicable

this is a good starting point however its not exactly what i need. i need locator2 to become -0.02 as soon a locator 1 starts moving in the opposite direction, so if i move locator one to +1X then when i move it to +0.90X "as its moving in the -X direction" it will make locator 2 snap to -0.02. 

 

and then the same in the opposite direction

 

hope you understand

0 Likes
Message 4 of 6

mcw0
Advisor
Advisor
Accepted solution

Ah, so what you want is for locator2.tx to be -.02 anytime locator1 moves in the negative direction, irrespective of where it is in the positive X direction.  And vice versa.  Then you will need either an expression or a frameCache node.

 

For both of these solutions, you will need to keyframe locator1.  It won't work well interactively...maybe with a scriptJob.  But let's keep it simple for now.  For the expression, you will need to query the both the current and previous position of locator1.  Then get the direction vector to determine if it is traveling in the positive or negative direction in X.  Will locator1 only move along the X axis?  Let's assume that is the case.

 

//  GET LOCATOR1 POSITIONS

float $currentX = `getAttr locator1.tx`;

int $currentFrame = `currentTime -q`;

float $previousX = `getAttr -time ($currentFrame-1) locator1.tx`;

float $dx = $currentX - $previousX;

$dx < 0 ? locator2.tx = -0.02 : locator2.tx = 0.02;

 

For the frameCache solution, connect locator1.tx to the frameCache.stream. 

Create a plusMinusAverage node and connect locator1.tx to input1D[0].

Set the plusMinusAverage operation to "subtract".

Then connect frameCache.past[1] t input1D[1].

Now create condition nodes as I outlined previously and connect the plusMinusAverage.output1D to the firstTerm of the condition nodes.  

 

Hope one of those works for you.

Set

Message 5 of 6

Anonymous
Not applicable

Thanks! this is great, very clear instruction 🙂 

0 Likes
Message 6 of 6

mcw0
Advisor
Advisor

Sorry, I just realized my expression isn't correct.  It doesn't account for when locator1 isn't moving.  When locator1 isn't moving, locator2 will be at 0.02.  Sorry about missing that.  But I'm sure you have enough to figure it out.