Understanding If/Else Conditional Code in After Effects Expressions

In this article, we'll look at if/else conditional code used in After Effects expressions to make a decision based on some condition.

Expressions are possibly the most powerful feature in Adobe After Effects, but a lot of users don’t use them because they seem intimidating. In this article, we’ll look at if/else conditional code used in After Effects expressions to make a decision based on some condition.

if/else after effects

We’ll start with taking an example of a rotating EditingCorp logo where we want it to have 100% opacity from zero to 180 degrees of rotation but only 50% from 180 to 360 degrees.

The code for our Opacity expression will be:

if (rotation < 180) 100 else 50

Now, let’s breakdown the above expression. “if” is the keyword that tells the expression that this is a conditional statement. Next comes the condition (rotation < 180). we’re checking to see if the rotation is less than 180 degrees. The next part tells the expression what to do if the condition is correct. In the above expression, “100” will set the opacity to 100 when the rotation is less than 180 degrees. Next is the optional keyword “else” that tells After Effects to do some optional task if the condition is false. Finally we have the value “50” which tells After Effects to set the opacity to 50 if the stated condition is false.

There are other ways to write this expression by only using the “if” statement and omitting the “else” statement.

t = 50
if (rotation <180) t = 100;
t

Lets consider another expression:

if (rotation < 180) 100

Expressions in After Effects must evaluate something of it will generate an error. In case of above expression, After Effects would accept it but it will fail as soon as the rotation value goes beyond 180 degrees as there is nothing to use as the final statement. A slightly more complicated way to write this expression is:

if (rotation < 180) {
100
}else{
50
}

From the above examples, you can easily understand the concept of using if/else conditional statements when writing expressions in After Effects. This can help in achieving different results based on some pre-defined conditions.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

21,162FansLike
2,813SubscribersSubscribe
 500+ LIGHTROOM PRESETS 
 WORTH $467 FOR JUST $29  

Discount
Upto 94%

LIMITED TIME OFFER
GET THE BUNDLE FOR JUST $29
close-link
Download 546 Lightroom Presets Worth $467 For Just $29.