Creative Communities of the World Forums

The peer to peer support community for media production professionals.

  • case statement

  • Jamie Bradshaw

    October 27, 2008 at 3:09 pm

    Hi all.

    Is it possible to use a case statement in After Effects expressions? If so can anyone provide me with a quick simple syntax for it.

    Many thanks,

    Jamie Bradshaw


    JimJam•Graphics

  • Dan Ebberts

    October 27, 2008 at 3:28 pm

    In JavaScript, it’s switch():

    https://www.w3schools.com/js/js_switch.asp

    Dan

  • Jamie Bradshaw

    October 27, 2008 at 3:46 pm

    Ah thanks Dan. That might be why I was having trouble with it. 😉

    Jamie Bradshaw


    JimJam•Graphics

  • Paul Roper

    January 30, 2013 at 11:12 am

    Hello,

    Following on from this 2008 thread, I’m trying to use the case statement, without success. Regardless of the value of zR (in the expression below), the outcome is ALWAYS “hello”. Even when zR = 90 (I have another text layer to verify this) or zR =0, it’s always the default value. I’ve tried removing the “break” statements and it makes no difference. I also tried sticking a .value on the end of the “zR=thisComp.layer(“X Axis”).transform.zRotation” to see if that had any effect, but no it didn’t.

    The rotation (and therefore zR ranges from -180 to +180).

    Any thoughts on where I’m gong wrong, anyone? Please?!

    Thanks,

    Paul

    zR=thisComp.layer("X Axis").transform.zRotation;
    switch (zR)

    {

    case 90:
    1234567;
    break;

    case 0:
    100000;
    break;

    default:
    "hello";

    }

  • Dan Ebberts

    January 30, 2013 at 5:14 pm

    The values must not be exact. If you change zR to a string, it seems to work:

    zR= “” + thisComp.layer(“X Axis”).transform.zRotation;

    Dan

  • Paul Roper

    January 31, 2013 at 11:51 am

    I gave that a try and it worked perfectly! I wonder if AE is storing the value 90 internally as something bizarre. Looks very much like a bug to me.

    My next question is regarding the syntax of the case statement – if instead of, say, case being exactly 90, how would I make the case statement activate if I wanted it to do so when, for example, zR > 90? I tried:

    case >90:

    and

    case zR >90:

    (having first removed the “” + to convert the variable into a string, so it’s back to a number again)

    ..but AE gives an error saying that > does not have a value.

    Any suggestions?

    Thanks,

    Paul

  • Dan Ebberts

    January 31, 2013 at 4:38 pm

    I wouldn’t use a swith statement for a range of values. I’d do it with a series of if/elseif statements:


    if (zR <= 90){
    (do stuff)
    }else if (zR <= 180){
    (do different stuff)
    }else if (zR <= 270){
    (etc.)
    }else{ // this catches everything where zR > 270
    (etc.)
    }

    Dan

  • Paul Roper

    January 31, 2013 at 5:38 pm

    That’s pretty much exactly what I did (if…then)!

    I just thought it was kinda “untidy”. The whole point of the expression was to convert a value like this:

    source value: -180 -90 0 90 180
    output value: 0 -1 0 1 0

    …in a linear way, not a sinusoidal way, before you say something like “sin(zR)!”! So I was wondering if there was any more elegant way to do it rather than:

    zR=thisComp.layer("target").transform.zRotation;
    if (zR >=0 && zR &lt;=90)
    {
    linear(zR,0,90,0,1);
    }
    else if (zR > 90)
    {
    linear(zR,90,180,1,0);
    }
    else if (zR &lt;0 && zR >= -90)
    {
    linear(zR,0,-90,-1,0);
    }
    else if (zR &lt;-90)
    {
    linear(zR,-90,-180,0,-1);
    }

  • Paul Roper

    June 8, 2019 at 12:10 pm

    Just to continue this 11 year old thread, I am once again trying (without success) to use switch / case in an expression. The expression is to control a colour on a layer by a Slider control, so if I set the slider to 1, the colour is white, slider = 2 makes the colour dark blue and slider = 3 makes the colour orange.

    Firstly, I tried this:

    Choice = Math.floor(effect("Colour")("Slider").value);
    switch( Choice ) {
    case 1:
    [255,255,255,0]
    break;
    case 2:
    [20,27,77,0]
    break;
    case 3:
    [249,56,34,0]
    break;
    default:
    [249,56,34,0]
    }

    …then I converted it to a string to see if that helped:

    Choice = "" + Math.floor(effect("Colour")("Slider").value);
    switch( Choice ) {
    case "1":
    [255,255,255,0]
    break;
    case "2":
    [20,27,77,0]
    break;
    case "3":
    [249,56,34,0]
    break;
    default:
    [249,56,34,0]
    }

    …then I just went with the ugly multiple if…then route:

    Choice = "" + Math.floor(effect("Colour")("Slider"));
    if ( Choice == "1"){ [255,25,255,0] } else
    if ( Choice == "2"){ [20,27,77,0] } else
    if ( Choice == "3"){ [249,56,34,0] } else {[255,0,0,0]};

    …but none of them work at all! Any ideas, anyone?!

    Thanks,
    Paul

    motion graphics | VFX | web design | etc.
    https://paulroper.com/

  • Paul Roper

    June 8, 2019 at 12:19 pm

    AARGH! I found the error! AE was expecting the RGBA values to range from 0 to 1, not 0 to 255! D’oh! All fixed now!

    motion graphics | VFX | web design | etc.
    https://paulroper.com/

Viewing 1 - 10 of 11 posts

Log in to reply.

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