Skip to main content
Jake D - JDPS
Participating Frequently
September 15, 2022
Answered

Sample Time when true expression?

  • September 15, 2022
  • 1 reply
  • 445 views

I'm trying to save some animation time by triggering some animations dynamically.  It would be REALLY handy if I could figure out how to trigger a layer to start playing based on the time at which an if/then becomes true, and continue playing until NOT true.  Imagine a UI where a video plays as you hover a cursor over it, and then stops when the cursor moves away.  Right now, I'm trying to trigger it somehow using an "if" expression, but everything I come up with to try to offset the value either just gives me a constant 0 time, or just the current time because I don't know how to sample time only once at the moment that the "if" statement becomes true.  Make sense?

 

So, ideally something like:

if(transform.scale[0]>20){
	time-(something that gets me time value when it STARTED being true)
}else{
	0
}

I can certainly get the project done by just lining up my layers to start when my cursor interacts with them, or by triggering them with markers, or a dozen other non dynamic things... but it seems like a handy bit of knowledge if someone knows a trick to get the value I'm looking for.  Many thanks in advance!

This topic has been closed for replies.
Correct answer Dan Ebberts

Huh.  Nope, I thought I could understand what was going on, but apparently I do not.  But I was at least able to put the while loop inside an if statement so it would show the first frame of the layer when the conditions weren't met, which should be good enough for this project.

t = accum = 0;

if(transform.scale[0]>20){
	while (t <= time){ if (transform.scale.valueAtTime(t)[0] > 20) accum += thisComp.frameDuration; t += thisComp.frameDuration; } valueAtTime(accum) 
}else{
	0
}

 


Try this:

if (transform.scale[0] > 20){
  t = time;
  accum = 0;
  while (transform.scale.valueAtTime(t)[0] > 20 && t >= 0){
    t -= thisComp.frameDuration;
    accum += thisComp.frameDuration;
  }
  accum;
}else{
  0;
}

1 reply

Dan Ebberts
Community Expert
Community Expert
September 15, 2022

Value triggers are tricky and you end up having to loop through time, frame-by-frame to find the trigger time. There are different ways of doing it, depending on whether you need it to re-trigger, hold the last value, etc. Try this one and see if it gets you close:

 

t = accum = 0;
while (t <= time){
  if (transform.scale.valueAtTime(t)[0] > 20) accum += thisComp.frameDuration;
  t += thisComp.frameDuration;
}
valueAtTime(accum)

 

Jake D - JDPS
Participating Frequently
September 15, 2022

Thanks, Dan.  This is why I buy all of your scripts on AEScripts.  I wish my brain worked the same way yours does!  I don't think I need it to do so for this project, but for the sake of better understanding what is going on in this expression, I'll experiement with it to see if I can figure out how to get "t" to reset back to 0 to reset the "while" loop so I could have it potentially play multiple times.

Jake D - JDPS
Participating Frequently
September 15, 2022

Huh.  Nope, I thought I could understand what was going on, but apparently I do not.  But I was at least able to put the while loop inside an if statement so it would show the first frame of the layer when the conditions weren't met, which should be good enough for this project.

t = accum = 0;

if(transform.scale[0]>20){
	while (t <= time){ if (transform.scale.valueAtTime(t)[0] > 20) accum += thisComp.frameDuration; t += thisComp.frameDuration; } valueAtTime(accum) 
}else{
	0
}