Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Can I nest if/else statements?

  • Can I nest if/else statements?

    Posted by Al Edwards on August 12, 2020 at 10:41 pm

    I’m trying to use a drop-down menu to contol a layer’s opacity.

    If the first item on the drop-down menu is selected, I want to run an if/else statement that will change the opacity depending on another layer’s rotation.

    Is this possible with if/else statements? Should I be using switch() instead? If so, how would I write this?

    //My first attempt gets an error message:
    if (v == 0) {
    if (r < 45) {
    100;
    } else {
    0;
    } else if (v == 1) {
    100;
    } else {
    0;
    }

    //My second attempt always returns 100 when v==0 and 0 for all other menu items, regardless of the rotation:
    if (v == 0 && r < 45) {
    100;
    } else if (v == 1) {
    100;
    } else {
    0;
    }

    Al Edwards replied 6 years ago 2 Members · 3 Replies
  • 3 Replies
  • Filip Vandueren

    August 12, 2020 at 11:00 pm

    You can’t first do an else, and then an else if, that’s probably the error, a missing } before the else if.

    if you’re running in the new javacscript engine, it would expect the outcome of the expression to be on the last executed line.
    So this would be safer:


    output = 0;
    if (v == 0) {
    if (r < 45) {
    output = 100;
    }
    } else if (v == 1) {
    output = 100;
    }
    output; // explicitly finish with the desired outcome

    Also needs a lot less “else” clauses, because the default 0 is already initialized

  • Al Edwards

    August 12, 2020 at 11:52 pm

    Hi Filip,

    When I pasted your code into AE, I didn’t get any errors. However, for v==0, the opacity stays at 100 all the time, even when r>45. Also, for v==1, I’m getting 0 when it should be 100. (v==2 is 0 too, but that’s correct).

    It did the same thing with the code I used in my second attempt. No errors, but still not the expected outcome. Any ideas as to why this is happening?

    My JavaScript knowledge is a little hazy when it comes to debugging the more complicated stuff like this!

  • Al Edwards

    August 13, 2020 at 12:35 am

    Solved!

    I made two adjustments that fixed the expression:

    1) When I pick-whipped the menu effect, I had to add .value to the end.
    2) Apparently the index of the first menu item is 1, not 0. So I needed to put v==1, not v==0 to reference that first menu item.

    I will paste the working code below in case it’s helpful to anyone.

    Thanks for the help!

    v = thisComp.layer("Control Layer").effect("Dropdown Menu Control")("Menu").value;
    r = thisComp.layer("Layer 1").transform.rotation;

    output = 0;
    if (v == 1) {
    if (r &lt; 45) {
    output = 100;
    }
    } else if (v == 2) {
    output = 100;
    }
    output;

    //This way of writing it works too:

    if (v == 1 && r &lt; 45) {
    100;
    } else if (v == 2) {
    100;
    } else {
    0;
    }

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