After Effects Tip: If/Else Statement

Today’s post is about if-else statements in After Effects. So without any further ado, let’s dive right in!

Alright, so we are in After Effects and for this example, I’ve already added checkboxes to my UKRAMEDIA.COM shape layer.

Checkboxes in shape layers in After Effects

Before we go any further, let me show you how I added those checkboxes.

Effect Controls: Checkboxes

First, select the UKRAMEDIA.COM shape layer, then right-click on the Effect Controls Panel, choose Expression Controls and then click on the Checkbox Controls.

Checkbox Control in Effects Control in After Effects

Next, select the UKRAMEDIA.COM shape layer again and press T to reveal the opacity.

Opacity keyboard shortcut

I’m going to ALT+LMB (CMD+LMB on the Mac) on the opacity stopwatch icon to activate my expression.

Activate Expressions stopwatch icon in After Effects

We are going to set some variables inside of the textbox that will call those checkboxes.

Setting variables in After Effects

First Variable: onSwitch

The first variable is going to be onSwitch and it will be the first checkbox inside of the Effect Controls Panel called ON/OFF Switch.

onSwitch = effect(“ON/OFF Swtich”)(“Checkbox”);

First Variable onSwtich in After Effects

Second Variable: opacity25

The second variable is going to be opacity25 and we will link it to the second checkbox 25% Opacity inside of the Effect Controls Panel.

opacity25 = effect(“25% Opacity”)(“Checkbox”);

Second Variable Opacity25 in After Effects

Third Variable: opacity50

The third variable is going to be opacity50 and we will link it to the third checkbox 50% Opacity inside of the Effect Controls Panel.

opacity50 = effect(“50% Opacity”)(“Checkbox”);

Third Variable Opacity50 in After Effects

Fourth Variable: opacity75

The fourth and final variable is going to be opacity75 and we will link it to the fourth checkbox 75% Opacity inside of the Effect Controls Panel.

opacity75 = effect(“75% Opacity”)(“Checkbox”);

Fourth Variable Opacity75 in After Effects

Your final result should look like this:

onSwitch = effect(“ON/OFF Swtich”)(“Checkbox”);
opacity25 = effect(“25% Opacity”)(“Checkbox”);
opacity50 = effect(“50% Opacity”)(“Checkbox”);
opacity75 = effect(“75% Opacity”)(“Checkbox”);

Final result in After Effects

If Statements

If statements are next! So I am going to say if() and inside of the parenthesis, I am going to say onSwitch is equalled to 1 like so:

if(onSwitch ==1)

if(onSwitch ==1) in After Effects

By the way, the reason why I say equal to 1 is because the checkboxes go from 0 to 1. So if the checkbox is not checked, then it is 0. If it is checked, then it is 1.

So in this statement, I am saying that if the onSwitch is 1 or in other words if the onSwitch is checked, then I want it to have 100% opacity. Else or if it is not checked, then I want the opacity to be 0.

if(onSwitch ==1) {
100
}else {
0
}

onSwitch 0 to 100 opacity in After Effects

If we click out of the textbox, you will notice that the UKRAMEDIA.COM shape layer opacity is now at 0% because the ON/OFF Switch checkbox is not checked.

0 percent opacity in After Effects

However, if we check the ON/OFF Switch, the opacity will switch to 100%.

100 percent opacity in After Effects when the checkbox is checked

In the expressions world, you will see 0 and 1 a lot. To simplify things, look at 1 and 0 as true/false. 0 is false and 1 is true.

With that said, we can now change our current expression by substituting true for 1 and false for 0 and our expression will still work the same.

if(onSwitch ==true) {
100
}else {
false
}

True false statements in After Effects

Okay, next I want to plug in the rest of our checkboxes. So for example, if 25% Opacity is checked, I want the UKRAMEDIA.COM shape layer to change to 25% opacity.

To make that happen, we just need to modify our expression a little by adding an else if statement saying if opacity25 is checked, then I want the UKRAMEDIA.COM shape layer to be at 25% opacity. It is that simple!

if(onSwitch ==true) {
100
} else if (opacity25  == 1){
25
}else {
false
}

Opacity 25 percent in After Effect shape layer if else statement

So now if the 25% Opacity checkbox is checked, you will see the opacity change to 25%.

Opacity 25 is checked in After Effects

We can do the same thing for 50% and 75%. By the way, You can have as many of them as you want. It is that simple!

if(onSwitch ==true) {
100
} else if (opacity25  == 1){
25
} else if (opacity50  == 1){
50
} else if (opacity75  == 1){
75
}else {
false
}

if then statements in After Effects

I hope you found this tip helpful! You can check out my Learn After Effects Expressions Course for more information about Expressions.

The beautiful thing about Expressions is that it absolutely helps you speed up your workflow in After Effects. You will work smarter, not harder.

Also, join our free Facebook group! We have thousands of talented motion designers in this group from around the world who will be happy to help you. Our Facebook group is our best free online resource. Join it while it is still free.

Download Project File

[sociallocker]

Download Tip 33 Project File
[/sociallocker]

About the author:

Sergei Prokhnevskiy

Sergei Prokhnevskiy is a Charlotte, NC based motion graphics designer, an online tutor, an entrepreneur, and Adobe MAX Master Speaker. Sergei is known for his quick and practical After Effects tips and tricks on YouTube, where he generated well over 6 million views on his tutorials. He has authored an online video course on After Effects Expressions and developed scripts for After Effects. He is also a former senior motion graphics designer at Fox Sports where he worked on graphics for NFL, Super Bowl, MLB, World Series, College Football, NASCAR, Women’s World Cup, and NASCAR Race Hub. He is very passionate about learning and teaching others.

5 FREE TIME-SAVING TOOLS

Over 100,000 downloads & counting!

These 5 After Effects tools will seriously speed up your workflow and save you hours of wasted time. More than that, they could be the difference between being able to complete a project on time or not!