Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Expressions – Changing Position and Storing Previously Calculated Values

  • Expressions – Changing Position and Storing Previously Calculated Values

    Posted by Hannah Tran on November 1, 2024 at 5:49 pm

    I have this simple script :

    // Define the duration for the first movement
    var duration = 10; // Change position for the first 10 seconds

    // Get the starting position
    var startPosition = position; // The initial position of the layer

    // Variable to store the specified position
    var specifiedPosition;

    // Check the time to determine the behavior
    if (time < duration) {
    // Move linearly from the starting position to the new position
    var progress = time / duration; // Calculate progress (0 to 1)
    var newX = linear(progress, 0, 1, startPosition[0], startPosition[0] + 100); // Example target position
    var newY = linear(progress, 0, 1, startPosition[1], startPosition[1] + 100); // Example target position
    specifiedPosition = [newX, newY];
    } else if (time >= duration && time < 20) {
    // Hold the last specified position
    specifiedPosition = thisLayer.transform.position.valueAtTime(time – thisComp.frameDuration); // Get the last position
    } else {
    // After 20 seconds, you can set to hold or do something else
    specifiedPosition = thisLayer.transform.position.valueAtTime(time – thisComp.frameDuration); // Continue holding the last position
    }

    // Return the final position
    specifiedPosition;

    At 10 seconds and beyond, I want it to stop calculating a new position and I want it to stay at the last position it had calculated. For some reason though, the position is not sticking and its reverting to its original position that would come from value. It’s a text layer, and I used Source Text to debug and “print” the previous position, and I can see that it is indeed calculating the previous position, but it’s not using that as an actual position! I need help understanding why it’s not holding?? I even tested with another layer and I can referece the previous position of ANOTHER layer but not the current layer? So confused!

    Video example:

    https://www.youtube.com/watch?v=FbFG0dAuTS8

    Dan Ebberts
    replied 3 months, 2 weeks ago
    3 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    November 1, 2024 at 7:00 pm

    Expressions have no memory, so they don’t have access to previous values calculated by that expression. Anytime an expression refers to the property hosting the expression with value or valueAtTime(), it gets the pre-expression value (as if the expression didn’t exist). There’s no way for it to get the post-expression value except to re-calculate it.

    Strangely though, when an expression refers to any other property, it gets the post-expression (if there is one) value.

  • Yoan Boisjoli

    November 1, 2024 at 11:03 pm

    Dan is right—expressions don’t store previous values and will only access the pre-expression value when referencing their own property. To hold the position after 10 seconds, you can use valueAtTime(duration) to “freeze” the last calculated position:

    var duration = 10;

    if (time < duration) {

    var progress = time / duration;

    [

    linear(progress, 0, 1, value[0], value[0] + 100),

    linear(progress, 0, 1, value[1], value[1] + 100)

    ];

    } else {

    valueAtTime(duration);

    }

  • Dan Ebberts

    November 2, 2024 at 12:47 am

    Except that valueAtTime() won’t give you the last calculated value. I think you can do what you’re suggesting with just this:

    var dur = 10;
    linear (time,0,dur,value, value + [100,100]);

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy