Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Select specific values and get the maximum value from the selection.

  • Select specific values and get the maximum value from the selection.

    Posted by Kyung Ko on November 12, 2019 at 2:43 pm

    Hi all,
    Here’s what I’m trying to do.
    I have 6 text layers and trying to get a longest width value from the only ones that are 100 opacity.
    Here’s the code I got so far and trying to get // parts.
    Please let me know if anyone has any advise. Thanks!

    t1_opc = thisComp.layer(“text1”).transform.opacity;
    t1_w = thisComp.layer(“text1”).sourceRectAtTime().width;

    t2_opc = thisComp.layer(“text2”).transform.opacity;
    t2_w = thisComp.layer(“text2”).sourceRectAtTime().width;

    t3_opc = thisComp.layer(“text3”).transform.opacity;
    t3_w = thisComp.layer(“text3”).sourceRectAtTime().width;

    t4_opc = thisComp.layer(“text4”).transform.opacity;
    t4_w = thisComp.layer(“text4”).sourceRectAtTime().width;

    t5_opc = thisComp.layer(“text5”).transform.opacity;
    t5_w = thisComp.layer(“text5”).sourceRectAtTime().width;

    t6_opc = thisComp.layer(“text6”).transform.opacity;
    t6_w = thisComp.layer(“text6”).sourceRectAtTime().width;

    // Select the text layers with 100 opacity from t1 - t6;
    // Add the selected text layers into the bracket bellow to get the maximum width from the selection.
    // max_w = Math.max(selection)

    max_w

    Kyung Ko replied 6 years, 3 months ago 2 Members · 5 Replies
  • 5 Replies
  • Dan Ebberts

    November 12, 2019 at 6:54 pm

    I think I’d just keep track of the max as the expression chugs through the layers:

    myMax = 0;
    L = thisComp.layer(“text1”);
    if (L.opacity == 100) myMax = L.sourceRectAtTime().width;
    L = thisComp.layer(“text2”);
    if (L.opacity == 100) myMax = Math.max(myMax,L.sourceRectAtTime().width);
    L = thisComp.layer(“text3”);
    if (L.opacity == 100) myMax = Math.max(myMax,L.sourceRectAtTime().width);
    L = thisComp.layer(“text4”);
    if (L.opacity == 100) myMax = Math.max(myMax,L.sourceRectAtTime().width);
    L = thisComp.layer(“text5”);
    if (L.opacity == 100) myMax = Math.max(myMax,L.sourceRectAtTime().width);
    L = thisComp.layer(“text6”);
    if (L.opacity == 100) myMax = Math.max(myMax,L.sourceRectAtTime().width);
    myMax

    Dan

  • Kyung Ko

    November 12, 2019 at 7:29 pm

    Thanks so much for the solution Dan!
    It works like a charm! ☺

  • Kyung Ko

    November 12, 2019 at 8:11 pm

    I made it work with your solution and it’s perfect, but I have a question just for the learning purpose. ☺☺☺
    Instead of linearly tracking down(which I think is the best way), I was trying to use ‘for loop’ to find the text layers in 100 opacity, and add those to ‘Math.max(“selected object”.width . . . )’
    I wonder if it’s even possible.

    I recently learned ‘for loop’ while researching for the problem, and just want to see how it can be done if it’s possible.

  • Dan Ebberts

    November 12, 2019 at 8:21 pm

    Something like this should work:


    myMax = 0;
    for (i = 1; i <= thisComp.numLayers; i++){
    if (i == index) continue; // skip this layer
    if (thisComp.layer(i).name.substr(0,4) == "text"){
    L = thisComp.layer(i);
    if (L.opacity == 100) myMax = Math.max(myMax,L.sourceRectAtTime().width);
    }
    }
    myMax

    Dan

  • Kyung Ko

    February 6, 2020 at 7:23 pm

    Sort of late reply, but HUGE THANKS to Dan!!
    I’m learning a lot from you.

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