Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions play a number of specified keyframes on a layer

  • play a number of specified keyframes on a layer

    Posted by Christopher Hesse on May 11, 2023 at 8:16 am

    Hello,

    I’ve dabbled in letting ChatGPT write an expression for me that does, what I’m looking for in this posts topic, but sadly, no amount of coaxing gets the AI to write code that does the job. Can anyone here help me out? What’s wrong with this code?

    // Set the range of keyframes to play

    var startKeyframe = 1; // Change this value to the index of the first keyframe you want to play

    var endKeyframe = 5; // Change this value to the index of the last keyframe you want to play


    // Calculate the total duration of the keyframe range

    var rangeDuration = key(endKeyframe).time – key(startKeyframe).time;

    // Calculate the time offset for each keyframe

    var timeOffset = rangeDuration / (endKeyframe – startKeyframe);

    // Calculate the time of the outpoint of the layer

    var outpoint = thisLayer.outPoint;

    // Calculate the start time of the keyframe range

    var startTime = outpoint – rangeDuration;

    // Loop through the keyframes in the range and set their values at the corresponding times

    for (var i = startKeyframe; i <= endKeyframe; i++) {

    var keyframeTime = startTime + (i – startKeyframe) * timeOffset;

    valueAtTime(keyframeTime);

    }

    Christopher Hesse replied 1 year ago 2 Members · 5 Replies
  • 5 Replies
  • Filip Vandueren

    May 11, 2023 at 11:41 am

    An expression cannot “set” the values of keyframes.

    It can only yield a value which is what the property should be at the current time. That value can be based on the keyframes and the timing of layers, but it won’t change those keyframes.

    I’m not entirely sure what you mean by “play a number of keyframes”. The code appears to want to do that based on the outpoint of the layer.
    Since the expression as it is now is wrong, maybe you should explain in plain words what you expect it to achieve, because now we don’t know what to change to get to your expectation.

  • Christopher Hesse

    May 11, 2023 at 12:44 pm

    Let me try to clarify what I mean 🙂

    What I want is for AE to play through a number of keyframes defined by a starting and ending keyframe, but timeshifted to the outpoint of the layer. E. g. if I have 6 keyframes on a layer, from 0 secs to 5 secs, I’d like to play keyframes 4 to 6, but at the outpoint of the layer minus the time necessary to play through these keyframes.

  • Filip Vandueren

    May 11, 2023 at 1:33 pm

    Something like this would work, but it can be a bit more simplified:

    var startKeyframe = 4; // Change this value to the index of the first keyframe you want to play
    var endKeyframe = 6; // Change this value to the index of the last keyframe you want to play
    // Calculate the total duration of the keyframe range
    var rangeDuration = key(endKeyframe).time - key(startKeyframe).time;
    // Calculate the time of the outpoint of the layer
    var outpoint = thisLayer.outPoint;
    var t = linear(time, outpoint-rangeDuration, outpoint, key(startKeyframe).time, key(endKeyframe).time);
    valueAtTime(t);
  • Christopher Hesse

    May 11, 2023 at 2:20 pm

    Awesome, that worked! Well, sort of, because the expression ignores all keyframes that come before the specified keframes, but that is, because again, I did not specify my question particularly well. So what I need is for all keyframes before the specified keyframes to be played normally. But I do have an idea on how to achieve this :).

  • Christopher Hesse

    May 16, 2023 at 9:05 am

    This is the final code I came up with, added by some help from ChatGPT.

    startKeyframe = 4;

    endKeyframe = 6;

    rangeDurationOut = key(endKeyframe).time – key(startKeyframe).time;

    if (time < key(startKeyframe).time) {

    // Play through keyframes from keyframe(1) until startKeyframe

    t = linear(time, inPoint, key(startKeyframe).time, key(1).time, key(startKeyframe).time);

    } else if (time < key(startKeyframe).time + rangeDurationOut) {

    // Pause at startKeyframe until rangeDurationOut is reached

    t = key(startKeyframe).time;

    } else {

    // Continue playing from startKeyframe until endKeyframe

    t = linear(time, outPoint – rangeDurationOut, outPoint, key(startKeyframe).time, key(endKeyframe).time);

    }

    valueAtTime(t);

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