Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Position value as target of automatized movements

  • Position value as target of automatized movements

  • Marco Tabilio

    November 15, 2021 at 3:04 am

    Hello everyone, I’m trying to make an object move to the position determined by the value of the current keyframes, in a sort of automatized way (with an ‘ease’ expression).

    In ‘position’ I’ve set the keyframes and the following expression… which doesn’t work :/

    The object just pops up in the position of the current keyframe. I can’t figure out what I’m doing wrong. Any ideas? Thanks!

     

    if (numKeys > 0) {

    ClosestKeyframe = nearestKey(time);

    if (ClosestKeyframe.time > time){

    CurrentKeyframe=key(ClosestKeyframe.index – 1);

    } else {

    CurrentKeyframe=key(ClosestKeyframe.index);

    }

    }

    if (CurrentKeyframe.index = 1) {

    PreviousKeyframe=key(1);

    } else {

    PreviousKeyframe=key(CurrentKeyframe.index-1);

    }

    dur=1;

    startTime=CurrentKeyframe.time-dur;

    endTime=CurrentKeyframe.time;

    startValue=valueAtTime(PreviousKeyframe.time);

    endValue= CurrentKeyframe.value;

    ease(time,startTime,endTime,startValue,endValue)

  • Dan Ebberts

    November 15, 2021 at 2:12 pm

    I see a couple of things. This line:

    if (CurrentKeyframe.index = 1) {

    needs to be like this:

    if (CurrentKeyframe.index == 1) {

    Also, your code doesn’t quite work if there are no keyframes (or only one keyframe) or if the first keyframe might be later than time = 0. I think something like this should work for what you’re trying to do:

    val = value;

    if (numKeys > 1){

    n = nearestKey(time).index;

    if (time < key(n).time) n--;

    if (n == 1){

    t1 = key(1).time;

    t2 = key(2).time;

    v1 = key(1).value;

    v2 = key(2).value;

    val = ease(time,t1,t2,v1,v2);

    }

    }

    val

  • Marco Tabilio

    November 16, 2021 at 2:14 pm

    Thank you very much for your answer, it is an honour to have a conversation with you.

    However I think I didn’t explain myself clearly.

    The idea is that my object stands still until the time reaches a keyframe’s time minus a certain duration.

    Let’s say, duration = 1 second.

    So at every keyframe’s time minus 1 second the movement begins; the object starts moving from his current position (which is the previous keyframe’s position OR the initial position, if there are no previous keyframes).

    The movement terminates when the time reaches the keyframe’s time and the object reaches its position value.

    Then the object stands in its new position until the time reaches the following keyframe’s time minus the duration… and so on.

    This should happen at every keyframe.

    Basically I’d like to determine only the final positions of the movements of an object, because all the movements I want are equal (same duration, same easing). In other words, I’d like to move my object setting only one keyframe (the end position), not two (beginning and end position).

    Thanks again for your help.

    Marco

  • Dan Ebberts

    November 16, 2021 at 6:08 pm

    Something like this maybe:

    dur = 1.0;

    val = value;

    if (numKeys > 0){

    n = nearestKey(time).index;

    if (time > key(n).time) n++;

    if (n <= numKeys){

    v1 = n == 1 ? valueAtTime(0) : key(n-1).value;

    v2 = key(n).value;

    t1 = key(n).time-dur;

    t2 = key(n).time;

    val = ease(time,t1,t2,v1,v2);

    }else{

    val = key(numKeys).value;

    }

    }

    val

  • Marco Tabilio

    November 17, 2021 at 8:09 pm

    Thank you very much, this works perfectly! 🙂

Viewing 1 - 5 of 5 posts

Log in to reply.

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