Set a timer in expressions?

Set a timer in expressions?

Anonymous
Not applicable
1,943 Views
8 Replies
Message 1 of 9

Set a timer in expressions?

Anonymous
Not applicable

Hello there my fellow Maya shamans, I've got an easy to explain, hard (for me) to pull out situation, that I hope you ladies & gentlemen can enlight me (and save learning time) : ) .


The situation is the following:

I want Set drive a rotating motion. I have an object X, so when the attribute value  (in a controller) is 1 it rotates (in a continuing loop), and when it's 0 it doesn't. So far so good, I can set that up, not as a driven key, but as an expressions with a condition: if attribute == 1 {rotation = time * X }, right?
Perfect, now... the problem is to retrieve that last rotating point. I thought I'd need a parallel time counter. A float value that keeps track of the seconds "attribute == 0", and substract that to "time" whenever is "attribute == 1" (let's called this parallel time as timeAlt), and then I'd be at the last point the rotating condition was true. Something like if attribute == 1 {rotation = timeAlt * X }

My problem right now is... I don't know how to set up a counter/timer in MEL. I've read some doc about.... "timer" and "timeX" and whatever, but I don't know how to implement them, andddd, as usual, there's so little info around : D.

Bottom line: I'M NOT A CODING Andy : ), that's why I come here and kneel for help.

Thanks in advance ppl.
Bye!

 

0 Likes
Accepted solutions (2)
1,944 Views
8 Replies
Replies (8)
Message 2 of 9

mcw0
Advisor
Advisor

Sounds like you want a scriptJob.  Have it triggered by your attribute to run a script.  Your script will start a timer if your attribute value is 1.  And stop and record your data when the attribute is 0.

0 Likes
Message 3 of 9

Anonymous
Not applicable

@mcw0thanks for reaching out!

 

Yep, I mean... my question is more about "is there an 'easy' way to do this in MEL". I'm pretty sure any coder Master Race would look at this and mention how easy and silly is xD, but since I'm nothing like that, and... I'm pretty close to the solution, I thought you (the community) might have the answer about how to declare that timer and add it to  the previous logic I laid out, since again, there's soooo little info around to "do it myself", as I'm used to  : /

 

Back to the point @mcw0 , yes that's what I had in mind. If I do it just with "time" as variable, when that attribute returns to 1, the last value logically isn't the one when your switched the attribute (you can't stop time they say) to 0 and made the object stop, so... you get a jump from the between the object's last rotation and the current.

 

ideas... ?

 

Message 4 of 9

mcw0
Advisor
Advisor

I think I understand what you want.  You want a way to start and stop the rotation at any point in time.  Then all you need is to record the switch frame and use that to calculate an offset the next time the switch is on.  This will prevent a jump in your rotations.

 

Another way to not have a jump is to not rely on time.  Use an increment instead.  Each time your switch is on, just add to your rotation by the increment value each frame.

 

rotation += increment

Message 5 of 9

Anonymous
Not applicable

mmm...

I get what you mean, but I don't know how to pull it off : (

 

I made three ints: one that alledgedly tracks the frame number when attribute is == 0, another when the attribute is == 0, and another that does the math and substracts that "Attr1" with "Attr0" = and gives an "Alt_time". Then I'd multiply that Alt_time by whatever number, and made that the rotation of the object.

Buuut, it doesn't work as I wrote it down xD. I read `currentTime -q` worked to get the "current frame", but there's problem cause, doesn't matter either the attribute is 1 or 0, it always gets stored, so... there's no "only when it's == 0 save the frame number".

Enlight me, pls : )

 

 

int $switch_record0;
int $switch_record1;
int $alt_time;

 

if (curve1.Switcher == 0)
{
$switch_record0 = `currentTime -q`;
}
else
{
$switch_record1 = `currentTime -q`;

$alt_time = $switch_record1 - $switch_record0;

pCube1.rotateY = $alt_time * 3;

}

 

 

Message 6 of 9

mcw0
Advisor
Advisor
Accepted solution

This is untested but try this.

 

float $increment = 1.0;
int $startFrame = 1001;
if(frame == $startFrame)
    rotation = 0;
else
{
    if( switch == 1 )
        rotation += $increment;
}
0 Likes
Message 7 of 9

Anonymous
Not applicable
Accepted solution

Works : )

 

this is my test:

 

float $increment = 1.0;
int $startFrame = 1;

if(frame == $startFrame)
{
pCube1.rotateX = 0;
}
else
{
if( curve1.asd == 1 )
{
pCube1.rotateX += $increment;
}}

 

 

*curve1.asd is the text attr xD

*I know too many brackets

 

I didn't know "+=" operator function, so... now I see it and makes sense

 

Question: Why the number you establish for $startFrame is 1001? I mean, cause if it's meant as a resetting piece, shouldn't it be 1 (or 0, depending on the beggining of your timeslider)?

 

@mcw0thanks for lending an ear. Much appriciated : )

 

0 Likes
Message 8 of 9

mcw0
Advisor
Advisor

I set startFrame = 1001 as an example that this approach will work for any animation frame range.  Just set the startFrame to any value you want.  But now I just realized a slight problem in my example in regards to startFrame.  The condition should not be if frame == startFrame.  To be bullet proof, it should be if frame <= startFrame.

 

I'm glad I was able to help.  Good luck.

Message 9 of 9

Anonymous
Not applicable

Yep, that makes even better sense.

 

@mcw0thanks again for your time and help : )