Skip to main content
zachneel
Participating Frequently
April 20, 2019
Question

How to find the text animator with the earliest keyframe?

  • April 20, 2019
  • 1 reply
  • 1048 views

So here's my code so far:

var animRot=lays[0].text.animator(i)("Properties").rotation.numKeys;

var animRotKey=lays[0].text.animator(i)("Properties").rotation;

var rotTimeCheck = [];

var earlyRot = 0;

    if (animRot>0)

            {

             for(var x=1; x<animRot; x++)

             {

                        if (x==1){rotTimeCheck = animRotKey.keyTime(x); earlyRot=rotTimeCheck;}

                        else

                        {

                                rotTimeCheck=animRotKey.keyTime(x);

                                if (rotTimeCheck[x-1]<rotTimeCheck)

                                {earlyRot = rotTimeCheck[x-1];}

                             else

                             {earlyRot = rotTimeCheck;}

                         }

                        }

                  }

              var earlyKey = [];

              var earliestKey = 0;

              var earliestAnim = undefined;

              earlyKey = Math.max(earlyOpa,earlyPos,earlySca,earlyRot);

             

              if (i==1)

              {earliestAnim = lays[0].text.animator(i);}

              else

              {

                  if (earlyKey<earlyKey[i-1])

                  {earliestAnim = lays[0].text.animator(i);}

               }      

earliestAnim.name = "dontdeleteplz";

So to spare you a huge chunk of code imagine that there is three more pieces (for opacity, position, and scale,)

and this code always returns earliestAnim as the first animator on my layer. I'm pretty sure I just have some sort of syntax error but I can't seem to figure out what it is.

This topic has been closed for replies.

1 reply

zachneel
zachneelAuthor
Participating Frequently
April 21, 2019

I uploaded the wrong code, I was messing around changing things, the Math.max should actually be Math.min but it still doesn’t work as intended.

zlovatt
Inspiring
April 21, 2019

My approach to this would be a bit different. Instead of doing the same block of code for each property, I'd make a single function to find the earliest property within a set array of properties, and return that.

Here's my approach:

(function() {

  /**

  * Returns the property in the array with the first key

  *

  * @param {Property[]} properties - Array of properties

  * @returns {Property}            - Property with earliest key

  */

  function getPropWithEarliestKey(properties) {

    var earliestProperty = properties[0];

    var earliestKeyTime = Number.POSITIVE_INFINITY;

    for (var ii = 0, il = properties.length; ii < il; ii++) {

      var prop = properties[ii];

      if (prop.numKeys === 0) {

        continue;

      }

      if (prop.keyTime(1) < earliestKeyTime) {

        earliestKeyTime = prop.keyTime(1);

        earliestProperty = prop;

      }

    }

    return earliestProperty;

  }

  var comp = app.project.activeItem;

  var props = comp.selectedProperties;

  var earliestProp = getPropWithEarliestKey(props);

  $.writeln(earliestProp.name);

})();

Note that I'm just getting the selected comp properties in this use case, but you can replace that with any array of properties.

Also-- while not about the code itself, your code styling is very inconsistent making it difficult to read and work with. If you're having trouble trying to parse where an issue may be, or what code blocks are being triggered, it may be worth cleaning it up to a (much) more readable format in line with code samples you'd see online. The same applies to naming of variables; I'd assume "animRot" would be the rotation animator property, not the number of keys on the property.

zlovatt
Inspiring
April 22, 2019

So I just had a chance to try out that code and it didn't seem to work. It's saying prop.numKeys is undefined?

Also if I wanted to get an array of just those four properties on each of the animators on  the text layer what is the best way to go about that? And I'd like it to be so that I just have to select the layer itself, not any properties or keyframes


Right... the above is just a sample approach, as I mentioned, not the full solution to what you're doing.

In what I provided, you'd need to select the properties in AE that you want to check, and then run it, and it should write the name of the property with the earliest key.

To modify it to your needs:

  • have it work on any selected layer
  • create an array of each of the properties in each animator you want to check
  • pass that array into the function, and it'll get you the property with the earliest key