Introduction
Last time I covered a pretty complex topic, introducing custom functions into After Effects with Penner's easing functions. I'll like to take a big step back now, and talk about if
statements.
So, what are if
statements?
Basic if
Statements
First - if you dabble in After Effects expressions or have any sort of coding experience, you more than likely are familiar with if
statements. if
statements allow you to tell After Effects to check for certain criteria within the project, and execute a command based on that criteria. An example of this could be linking a layer's opacity to a checkbox control:
if (effect("Checkbox Control")("Checkbox") == 1) 100
else 0
With this statement in place, when the checkbox control is turned on, the layer will be seen at 100% opacity. However, if the control is off, the opacity will be set to 0%, and the layer will be completely transparent. This can be a useful control when making .mogrt files, or, connecting multiple layers to a single, keyframe-able parameter.
The ability to check for criteria is what makes if
statements a powerful tool in your arsenal, even at a basic level. But they can also help enable more sophisticated code for more complex situations.
Checking For Additional Criteria
Sometimes, there may be cause for checking more than one argument in order to return a certain value. For instance, what if I only want a drop shadow layer in a template to be visible when a) there is text inputted into a text layer, and b) the drop shadow checkbox control switch is engaged?
This is possible with the and operator: &&
. Adding this between the if
statement arguments tells After Effects we want both of these things to be true, in order to return a specific result.
if (thisComp.layer("test").text.sourceText != "" && effect("Checkbox Control")("Checkbox") == 1) 100
else 0
This if
statement checks both arguments, making sure the "test" text layer is not empty, and that the checkbox control is on, before returning an opacity value. In this case, if both of those arguments are true, it will return 100, but if neither or both are false, it will return 0.
Another helpful operator to consider is the or operator: ||
.
if (thisComp.layer("test").text.sourceText != "" || effect("Checkbox Control")("Checkbox") == 1) 100
else 0
Using this operator, After Effects will return the first value in the statement if one or both of the statements are true. Only when both are false, will it return the else
value.
Nesting if
Statements
But what if there is a more complex situation, where there are more than 2 potential answers to an argument? If you need to check for an array of criteria, each with its own potential solution, you may need to create a nested if
statement.
For example, I have a text layer which should contain a number between 0 and 4. Depending on what number is displayed, the solid layer behind it will change opacity (0 = 0%, 1 = 25%, 2 = 50%, 3 = 75%, 4 = 100%). However, the solid layer should also check to see whether or not a checkbox control is on. If it is not, the layer shouldn't be visible at all, regardless of the text layer's number.
copy = thisComp.layer("text").text.sourceText;
ctrl = effect("Checkbox Control")("Checkbox");
if (ctrl == 1)
if (copy == "0") 0
else if (copy == "1") 25
else if (copy == "2") 50
else if (copy == "3") 75
else if (copy == "4") 100
else value
else 0
Let's break this down.
First, I make a variable for my text layer, and my checkbox control. Then, I begin my nested if
statement. Because there are more than 2 possible outcomes for this scenario, using &&
in this case would be encumbersome, forcing After Effects to check the checkbox over and over again unnecessarily. Instead, only the first statement checks to see if the checkbox control is on. Then, I create a second if
statement inside the first, which checks what number the text layer is displaying. From there, I create enough else
if
arguments to cover all possible outcomes. Once all text layer outcomes are accounted for, I finish the statement by writing the else
argument for the checkbox control.
Conclusion
Scripting decision making possibilities with After Effects if
statements allows for unique and tailored templates. Whether these expressions are written to streamline keyframe animation, to create a .mogrt file, or are there to react to data from an external source, they provide a level of dynamic adaptability to projects otherwise not possible.
Was this useful? Any questions? Feel free to ask in the comments.
Top comments (4)
Cool article!
Thank you :)
Thank You for Sharing ! 😊
Thanks for reading!