Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects How to find the sum of the vectors inside a custom array?

  • How to find the sum of the vectors inside a custom array?

    Posted by Crystal Goh on October 25, 2019 at 6:55 am

    Hi all, I am pretty new to expressions and trying to do an expression that finds the total width from my image layers ( which all have different widths, number of image layers not fixed).
    My current issue is not knowing how to sum up the vectors inside the array I made. This stackflow thread has answered the question but when using their code, I still get errors: https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers

    Would really appreciate any help!


    // Variables
    var myArray = [];
    var i = 0;
    var curLayer = thisComp.layer(i);

    // Execution
    for (; i<=thisComp.numLayers ; i++){
    myArray.push(curLayer.sourceRectAtTime().width);
    }

    myArray.slice(2); /*Theres 2 layers in my comp that are not image layers, hence slicing them off.*/
    //Next line am VERY unclear on getting the total width number with reduce()
    return myArray.reduce(???);

    Dan Ebberts replied 6 years, 10 months ago 2 Members · 1 Reply
  • 1 Reply
  • Dan Ebberts

    October 25, 2019 at 4:34 pm

    I guess I’d do it like this:


    accum = 0;
    for (i = 3; i <= thisComp.numLayers; i++){
    accum += thisComp.layer(i).sourceRectAtTime(time,false).width;
    }
    accum

    Dan

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