Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Conditinal statement if/else

  • Conditinal statement if/else

  • Nihad Spahic

    January 19, 2022 at 7:44 pm

    Hey guys,

    I am trying to link opacity to the “Settings” adjustment layer, on a settings layer there are 4 slider controls and one checkbox control.

    I want to achieve if any of the sliders is equal to an index number, the opacity will be 100, and another state if those are not true, is that if the checkbox is checked then opacity will be 100.

    My code only “listens” checkbox control

    This is my code:

    var a = thisComp.layer("Settings").effect("01")(1);
    var b = thisComp.layer("Settings").effect("02")(1);
    var c = thisComp.layer("Settings").effect("03")(1);
    var d = thisComp.layer("Settings").effect("04")(1);
    var e = thisComp.layer("Settings").effect("Visibility")(1);
    if(a == index || b == index || c == index || d == index){
    100;
    }else{
    0;
    }
    }if(e == 1){
    100;
    }else{
    0;

  • Dan Ebberts

    January 19, 2022 at 8:09 pm

    This should work:

    var a = thisComp.layer("Settings").effect("01")(1);

    var b = thisComp.layer("Settings").effect("02")(1);

    var c = thisComp.layer("Settings").effect("03")(1);

    var d = thisComp.layer("Settings").effect("04")(1);

    var e = thisComp.layer("Settings").effect("Visibility")(1);

    a == index || b == index || c == index || d == index || e == 1 ? 100 : 0

  • Nihad Spahic

    January 19, 2022 at 8:27 pm

    Thank you! Works like a charm!

    Could you please explain to me what is happening in the last line of code, I understand this part a == index || b == index || c == index || d == index || e == 1 any of these needs to be true, but I don’t understand “? 100 : 0”

    Thanks

  • Dan Ebberts

    January 19, 2022 at 9:42 pm

    It’s the JavaScript conditional operator. It’s an alternative to if/else that I like to use because you can put it all on one line without having to add brackets:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

  • Nihad Spahic

    January 20, 2022 at 7:17 am

    Thanks Dan!

  • Nihad Spahic

    January 24, 2022 at 6:17 pm

    Hey Dan,

    I have one more question, if I have on a settings layer four color controls named Color 01, 02, 03, and 04 and on each layer color fill. Is it possible to control color fill separately?

    For example, I have 20 layers which opacity I am controlling with 4 sliders as in the example above, I would love to colorize those 4 layers with separate colors.


    Cheers!

  • Dan Ebberts

    January 24, 2022 at 8:20 pm

    I’m not sure I understand exactly, but maybe a fill color expression like this:

    var a = thisComp.layer("Settings").effect("01")(1);

    var b = thisComp.layer("Settings").effect("02")(1);

    var c = thisComp.layer("Settings").effect("03")(1);

    var d = thisComp.layer("Settings").effect("04")(1);

    if (a == index){

    thisComp.layer("Settings").effect("Color 01")(1);

    }else if (b == index){

    thisComp.layer("Settings").effect("Color 02")(1);

    }else if (c == index){

    thisComp.layer("Settings").effect("Color 03")(1);

    }else if (d == index){

    thisComp.layer("Settings").effect("Color 04")(1);

    }else{

    value;

    }

  • Nihad Spahic

    January 24, 2022 at 8:39 pm

    Thank you so much! It works. Basically, when I select 4 of twenty layers based on their index number, I can change their color separately.

Viewing 1 - 8 of 8 posts

Log in to reply.

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