Skip to main content
Participating Frequently
November 1, 2024
Question

Persistent state between frames

  • November 1, 2024
  • 2 replies
  • 267 views

I have a layer with a car, and I have another Null layer with a speed controller that has manually placed keyframes.

 

I want to control the car position according to those speed keyframes, i.e. accumulate traveled distance. For example, if the spped values on the first 5 frames are 1, 2, 3, 4, 5 then positions should be 1, 3, 6, 10, 15.

 

I wrote this script and it works as intended:

 

sourceLayer = thisComp.layer("Null 1");
currentFrame = timeToFrames();

sum = 0;
for(i = 0; i <= currentFrame; i++) {
sum += sourceLayer.effect("speed")("Slider").valueAtTime(framesToTime(i));
}
sum

 

This becomes very inefficient as currentFrame increases. In reality I need some kind of persistent across frames storage, so I could just add one value to it every frame. Is that possible to do in Ae scripting system?


This topic has been closed for replies.

2 replies

Mylenium
Legend
November 1, 2024
Is that possible to do in Ae scripting system?

 

Long and short: No.

 

Mylenium

Dan Ebberts
Community Expert
Community Expert
November 1, 2024

If your keyframes are linear and they don't happen every frame, you can probably gain some efficiancy by using a linear keyframe integrator, as discussed here:

https://www.motionscript.com/articles/speed-control.html#linear

but otherwise, theres no reliable way do persistent variables with expressions.