Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions How do I do a complex if/else structure?

  • How do I do a complex if/else structure?

    Posted by Erin Shelby on November 8, 2007 at 1:28 am

    Hi All,

    I have a slider and basically will be using only values 1-4 on it…

    I have a series of solids underneath it that I want to change opacity depending on what value the slider is at, so:

    solid 1
    if slider is 1
    then opacity 10
    if slider is 2
    then opacity 25
    if slider is 3
    then opacity 50
    if slider is 4
    then opacity 100

    etc etc. Solid 2 is different and solid 3 is different. I was just wondering if there was a way to structure this that is better then a complex nestled if/else strucutre…thoughts?

    -Erin

    Mike Clasby replied 18 years, 6 months ago 4 Members · 4 Replies
  • 4 Replies
  • Dan Ebberts

    November 8, 2007 at 1:39 am

    Maybe like this:

    idx = Math.round(effect(“Slider Control”)(“Slider”));
    myArray = [10,25,50,100];
    myArray[clamp(idx,0,myArray.length-1)]

    Dan

  • Colin Braley

    November 8, 2007 at 1:51 am

    There is no super simple way to do something like this. Some people like to use something called a “switch statement” in situations like this. For instance, the psuedocode you wrote in in you post would look like this written as a switch statement:


    switch ( slider ){
    case 1:
    10
    case 2:
    25
    case 3:
    50
    case 4:
    100
    default:
    100//if the slider wasn't 1 ,2, 3, or 4
    }

    Im not sure if this is the exact syntax you would use for a switch statement in javascipt, and i actully don’t even know if there is a switch constuct in javascript….however, most modern programming languages do have a switch construct.

    Personally, I would never use a swich statement, I prefer just using if statements….I feel that while code using switch statements might be more concise it is more-error prone because people don’t use them as often. Also, there are a bunch of other issues that crop up when you forget to use the “break” keyword peoperly within a switch statement, but I dont think I’ll elaborate on that one…

    Anyway, if you REALLY want to use switch statements, check out wikipedia and read about them…

    https://en.wikipedia.org/wiki/Switch_statement

    ~Colin

    http://www.colinbraley.com

  • Erin Shelby

    November 8, 2007 at 1:56 am

    Thanks Dan/Colin! I’ll give it a whirl.

    -Erin

  • Mike Clasby

    November 8, 2007 at 9:11 pm

    I can’t getColin’s to work, love Dan’s.

    Here is an old style if/else, it needs hold keyframes:

    First you need a Null named “control”. Add a Slider to the Null. Add this expression to Opacity:

    n = thisComp.layer(“Null 1”).effect(“Slider Control”)(“Slider”);
    if (n==1) {
    10
    } else if (n==2) {
    25
    } else if (n==3) {
    50
    } else if (n==4) {
    100
    } else {
    0
    }

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