Skip to main content
Paul Cristo
Inspiring
October 5, 2023
Question

Expression to determine each value of next 100 frames

  • October 5, 2023
  • 3 replies
  • 2774 views

I'm trying to write an expression that gets the value of a property for each of the next 100 frames and determine if they're all below a certain value. If they are, change a boolean to true and vice versa. Is this possible in the After Effects paradigm? Thanks. 

This topic has been closed for replies.

3 replies

Dan Ebberts
Community Expert
October 5, 2023

Something like this, probably (this example is for the layer's rotation property):

prop = rotation;
threshold = 20;
val = true;
for (i = 0; i < 100; i++){
  if (prop.valueAtTime(time + framesToTime(i)) >= threshold){
    val = false;
    break;
  }
}
val

 

Community Expert
October 5, 2023

Dan, how would you use a property in an array like Scale or Position? When I try position[0] instead of rotation I get an error. The only way to not break the expression is to create an array for threshold, but I only want to look at the Y position. 

 

Paul, you have to add an argument that does something when the val changes from true to false. Something like this;

You'll then need to work out a time when the "val" changes from true to false and use that to drive an interpolation method like ease(tome, time of the change, time of the change + duration, start value, end value).

 

Dan, you can see the problem in the threshold line of the expression. I only want to look at the Y value.

 

 

ref = thisComp.layer(index + 1);
prop = ref.position;
threshold = [700, 540];
val = true;
for (i = 0; i &lt; 100; i++){
  if (prop.valueAtTime(time + framesToTime(i)) &gt;= threshold){
    val = false;
    break;
  }
}
val

if (val == true){
	a = "Do Something"
}
else{
	a = "Do something else"
}

 

 

 

Dan Ebberts
Community Expert
October 6, 2023

I'm looking at simplifying the problem. So, everything gets captured at current time instead of 100 frames ahead. Then after capturing the values and the keyframes are created, one can manually move these keyframes earlier in time or use valueAtTime. The result should be the same but a lot quicker to process the initial expressions if the created keyframes are manually moved 100 frames earlier in time. 


Hmmm... I'm not sure what you mean by "gets captured". I don't think there's a solution where the expression can just examine values at the current (or a single offset) time. It needs more info to calculate the current state because it starts each frame not knowing anything. But, in any case, once you have the expression set up, you could certainly convert it to keyframes to speed up subsequent processing, but I suspect that's not at all what you have in mind...

Community Expert
October 5, 2023

If you are trying to look forward from the current time to the next 100 frames, you are going to end up with a recursive expression that slows down the farther down the timeline you go. The slowdown can be exponential. You will have to take time and create a loop that looks forward 100 * thisComp.frameDuration and looks for your critical value. It can throw a switch or change a color if it finds it and returns a false value.

 

Add this expression to a text layer above a layer with an animated position property, and it will tell you whether the layer is above or below the center of the comp 100 frames ahead of the current time.

 

lookForward = 100 * thisComp.frameDuration;
v = thisComp.layer(index + 1).transform.position.valueAtTime(time + lookForward);
trgtVal = thisComp.height/2;
if (v[1] < trgtVal){
	p = ": Top"
}
else{
	p = ": Bottom"
}
"Position" + p

 

It won't compare all 100 frames, but it would look ahead to detect the change.

 

If I get some free time later on, I will take a look at adding a loop operator to check if any frame is above the target value.

Mylenium
Brainiac
October 5, 2023

Sure. A simple for() loop will do.

 

Mylenium