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.

zachneel
zachneelAuthor
Participating Frequently
April 21, 2019

Thanks man! Yeah I’m super new to scripting and any kind of coding in general, so I know I’m doing things wrong just trying to figure it all out as I go! i appreciate the help.