Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Elegant “Current” Keyframe solution?

  • Elegant “Current” Keyframe solution?

    Posted by Jon Wooley on March 22, 2016 at 8:27 pm

    I love the nearestKey(time) expression and have been using it to trigger animations with (time – nearestKey(time).time). However, nearestKey looks both forward and backward when determining which Keyframe is the nearest. I would love it if it only looked backward. I have to wonder if anyone in this community has figured out an elegant solution to this problem. I’ve tried a few different things based on my relatively limited knowledge of AE Expressions/Javascript Here is what I’m currently doing. There has to be a better way, right?

    x = thisComp.layer("Test").effect("Slider Control")("Slider");
    y = x.nearestKey(time).index;
    d = (time - x.nearestKey(time).time);

    if (d>=0){
    currentKey = y; //if the time between the playhead and the nearest keyframe is positive, the current key is the nearest key
    }else{
    currentKey = y-1; //if the time between the playhead and the nearest keyframe is negative, the current key is the previous key
    }
    linear(currentKey,1,100,1,100) //keeps currentKey value above 0 since it will be used for index. Doesn't work if over 100 keyframes in effect.

    d = (time - x.key(currentKey).time);

    Jon Wooley replied 10 years, 1 month ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    March 22, 2016 at 8:37 pm

    Something like this maybe:


    x = thisComp.layer("Test").effect("Slider Control")("Slider");
    n = 0;
    if (x.numKeys > 0){
    n = x.nearestKey(time).index;
    if (x.key(n).time > time) n--;

    }
    if (n > 0)
    d = time - x.key(n).time
    else
    d = 0;

    Dan

  • Jon Wooley

    March 23, 2016 at 6:26 pm

    You are a wizard sir. I continue to learn so much from you.

    Thank you.

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