Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Add up the heights of however many layers are present?

  • Add up the heights of however many layers are present?

    Posted by Chris Biagi on April 29, 2019 at 6:46 pm

    Hi!

    I’m building a template for which I need to know the total of all the text layers’ heights in pixels, by adding up all their sourceRectAtTime(0).height values. The template consists of a variable number of text layers, which are arranged on multiple lines, centered horizontally. Just a standard title screen basically.

    I have a general idea of how the logic could work, but I’m not quite sure where to start with this. The number of lines of text will vary, which the template must accommodate automatically. Ultimately I will use the total height of all the lines of text to calculate how to center the block of multiple text layers vertically.

    I’ve named all my text layers like, “TXT – name“. I use name.split(" - ")[0] to detect the string “TXT,” to determine which layers are designated as text layers.

    I believe I can use the code above as a basis to count how many “TXT” layers exist. Then I can use that value to tell AE which layer indexes’ heights need to be added up. Something like,

    If there are 2 Text layers detected, add up the heights of layer 1 + layer 2

    If there are 4 Text layers detected, add up the heights of layer 1 + layer 2 + layer 3 + layer 4

    etc…

    Thanks

    Chris Biagi replied 7 years, 3 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    April 29, 2019 at 7:26 pm

    This would be the general idea:


    h = 0;
    for (i = 1; i <= thisComp.numLayers; i++){
    if (i == index) continue;
    if (thisComp.layer(i).name.split(" - ")[0] != "TXT") continue;
    h += thisComp.layer(i).sourceRectAtTime(time,false).height;
    }
    h

    Dan

  • Chris Biagi

    April 29, 2019 at 8:37 pm

    Looks like I’ll have to go do some tutorials on “For” loops, that’s new to me. I’ll update once I have this working, thanks Dan.

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