Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Getting the sum number from a dynamic Array

Explorer ,
Jun 17, 2023 Jun 17, 2023

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.
 
TOPICS
Expressions , How to , Scripting
748
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jun 17, 2023 Jun 17, 2023

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

Translate
Explorer , Jun 17, 2023 Jun 17, 2023

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

Translate
Community Expert ,
Jun 17, 2023 Jun 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]
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 17, 2023 Jun 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 17, 2023 Jun 17, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 17, 2023 Jun 17, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 18, 2023 Jun 18, 2023
LATEST

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines