1 Reply Latest reply on Aug 8, 2012 9:50 PM by Dan Fredley

    How can I access the layer keyframes?


      I'm beginning in After Effects Scripting, and I want to know how to have access to layer properties keyframes. I want to make a script that modifies the position of selected keyframes.

        • 1. Re: How can I access the layer keyframes?
          Dan Fredley Level 1

          One of these two free scripts might do what you want:





          If they don't work out for you, you can always look at their source code and possibly tweak it to do what you want it to.


          These scripts also deal with looping through keyframes to accomplish certain tasks:





          Studying other quality scripts is one of the best ways to learn scripting.


          For a quick example on how to access the time and value of a single keyframe, create a comp, add a solid, set one keyframe on the position property and then run this script:

              var proj = app.project; // The Application Object is at the top level followed by the Project Object
              var comp = proj.activeItem;  // Set the comp variable to the active composition
              var layer = comp.layer(1); // Set the layer variable to the first layer of the composition object
              var position = layer.position; // Set the position variable to the position property of the layer object
              var numKeys = position.numKeys; // Get the number of keyframes on the position property through the numKeys attribute
              var myKeyframe, myKeyframeTime, myKeyframeValue; // Set empty variables for later assignment
              if (numKeys > 0) {
                  myKeyframe = 1; // Set the variable to the number one to represent the first keyframe on the property
                  myKeyframeTime = position.keyTime(myKeyframe).toString(); // Get the keyframe time of myKeyframe in seconds
                  myKeyframeValue = position.keyValue(1).toString(); // Get the keyframe value of myKeyframe represented as an array [x,y,z]
                  alert ('The first keyframe on layer 1 is at ' + myKeyframeTime + ' seconds and the value is [' + myKeyframeValue + ']'); // Send an alert to AE with the information
              } else {
                  alert ('There are no keyframes on layer 1.'); // If there aren't any keyframes on the position property of the first layer then alert the user


          Beyond that it gets complicated by looping through keyframes backwards, collecting values, times, keyInInterpolationTypes, keyOutInterpolationTypes, keyInSpatialTangents, keyOutSpatialTangents, etc. then removing them only to create new keyframes with the same information at offset times or values.  It's important to keep in mind that scripting cannot get or set values for Custom Value keyframes (such as curves and levels).  Unfortunately, there are limitations to scripting.