Skip to main content
Inspiring
June 17, 2023
Answered

Getting the sum number from a dynamic Array

  • June 17, 2023
  • 2 replies
  • 911 views

I would like to get the total sum from a dynamic array. I'm not a coder but I can tinker just an FYI.

 

So what I'm trying to do is get the layer heights of multiple text objects so I can get the total height of the layers and resize a shape layer underneath to the total height of the text layers. I was able to find code to get the max width of the text layers but don't know how to get the total height. Below is the for loop to get the width: 

keyword = "Text";
textListWidth = [];
textListHeight = [];
 
for(i = 1; i <= thisComp.numLayers; i++){
layerPath = thisComp.layer(i);
layerWidth = layerPath.sourceRectAtTime().width;
if(layerPath.name.match(keyword)) { textListWidth.push(layerWidth) } else{};
}
 
x = Math.max.apply(null, textListWidth);
 
y = 50; /// temp
 
[x, y]
 
Thanks for any help.
 
This topic has been closed for replies.
Correct answer bentsib1974

It works for me. Did you modify the expression at all, or did you paste it in directly from my post?


Yes, I had modified my code. I must have missed something. It does work. THANK YOU! 

2 replies

Mathias Moehl
Community Expert
Community Expert
June 18, 2023

If you want to wrap shape layer boxes around multiple layers, my (paid) tool Pins & Boxes can also be very helpful:

 

 

It creates all the expressions for you and you can create pretty complex layouts with it in a quick and easy way without worrying about the code.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Dan Ebberts
Community Expert
Community Expert
June 17, 2023

This should work:

keyword = "Text";
textListWidth = [];
y = 0;
 
for(i = 1; i <= thisComp.numLayers; i++){
  layerPath = thisComp.layer(i);
  layerWidth = layerPath.sourceRectAtTime().width;
  layerHeight = layerPath.sourceRectAtTime().height;
  if(layerPath.name.match(keyword)) {
    textListWidth.push(layerWidth);
    y += layerHeight;
  }
}
 
x = Math.max.apply(null, textListWidth);
[x, y]
Inspiring
June 17, 2023

Thank you for the reply but it's not seeming to add all the layer heights together. I think it's taking one height.

Dan Ebberts
Community Expert
Community Expert
June 17, 2023

It works for me. Did you modify the expression at all, or did you paste it in directly from my post?