Activity › Forums › Adobe After Effects Expressions › case statement
-
case statement
Posted by Jamie Bradshaw on October 27, 2008 at 3:09 pmHi 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
Trevor Asquerthian replied 6 years ago 4 Members · 10 Replies -
10 Replies
-
Dan Ebberts
October 27, 2008 at 3:28 pm -
Jamie Bradshaw
October 27, 2008 at 3:46 pm -
Paul Roper
January 30, 2013 at 11:12 amHello,
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 pmThe 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 amI 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 pmI 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 pmThat’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 <=90)
{
linear(zR,0,90,0,1);
}
else if (zR > 90)
{
linear(zR,90,180,1,0);
}
else if (zR <0 && zR >= -90)
{
linear(zR,0,-90,-1,0);
}
else if (zR <-90)
{
linear(zR,-90,-180,0,-1);
} -
Paul Roper
June 8, 2019 at 12:10 pmJust 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,
Paulmotion graphics | VFX | web design | etc.
https://paulroper.com/ -
Paul Roper
June 8, 2019 at 12:19 pmAARGH! 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/ -
Trevor Asquerthian
April 25, 2020 at 11:11 amStill a useful thread after all these years. Converting the dropdown output to string made the switch work.
Thanks
Reply to this Discussion! Login or Sign Up