(Page 1 of 3 pages for this article 1 2 3 >)
Sunday, July 04, 2010
Deeper Modes of Expression, Part 7: Making Decisions
Chris Meyer | 07/04
Learning how to craft if/then/else statements plus do/while loops will open the door to a wide range of advanced techniques.
As your expressions become more complex, you will want to start making decisions in the middle of them – for example, if the current value is less than a certain number, do one thing; if it is greater than that number, do something different. Other decisions include “wait until the current time is past the next marker – then do this animation” or even “don’t freak out and give me an error message if you can’t find the marker, keyframe, or other layer you’re looking for.”
These sorts of decisions are often referred to as conditional statements, and expressions in After Effects supports them. The most common type of conditional is know it as an if/then or if/then/else statement. If you are not familiar with JavaScript, their format may be a bit different from what you expect, but they are easy to learn.
A variation on this theme is a do/while loop, which is an essential tool for overcoming expressions’ inability to remember what happened beforehand: You can use these loops to walk through every frame of the animation – accumulating information – until you get to the current frame.
Bracket Etiquette
Before we dive in, we need to discuss a code formatting issue. Programmers may find the way we use curly brackets in our book and this blog to be a bit odd. For example, the open curly bracket would normally appear on the same line as the “if” question, and the close curly bracket would appear on a line by itself after the “then” statement, starting in the same column as the if. An example would be:

We personally find reading this unintuitive. Therefore, we tend to rearrange the curly brackets so that they appear on the same line, or at least in the same vertical column. For example:

...or…

These expressions will all work the same, despite the differences in formatting. We will use our own formatting throughout this series; we hope the real programmers out there can forgive us.
Note that if there is only one statement inside a conditional clause, brackets aren’t necessary; you could leave them out altogether in the examples above.
If/Then/Else
Setting up an if/then statement in After Effects consists of two main steps:
- Asking the “if” question (inside parentheses like these, with no semicolon afterward)
- Telling After Effects that if “if” proved to be true, what to do then {placed inside curly brackets like these}; followed by a semicolon
If the answer to the “if” questions proves to be false, the “then” part of the expression inside the curly brackets is ignored, and After Effects will jump ahead in the expression to the next line after the closing curly bracket. Optionally, you can place an “else” statement at this point, {again nested inside curly brackets like these}.
Let’s say you wanted a layer to stay invisible until the current time was later than the first marker in the layer. The if/then expression for Opacity would be

This is demonstrated in the figures below; pay attention to the red Opacity value (remember, red values mean they are determined by expressions). The way we wrote this expression is that first we set up a temporary value, then changed it only when a certain “if” test turned out to be true:


Another approach is the if/then/else statement, in which you make a test, give one answer if it was true, and another answer if it was false. This same expression, written as an if/then/else, would look like this:

It’s your choice if you use an if/then or an if/then/else statement; both do the same job. Quite often, it comes down to which you find clearer and easier to understand. Note that an if/then/else statement can have multiple “else” clauses (sometimes referred to as “else if” conditionals); just remember to use the curly brackets to keep your statements straight.
You may have noticed in the figures above that the layer becomes visible only after the current time is beyond the first layer marker. This is because we used the “greater than” symbol >. If you really wanted “greater than or equal to,” then you need to use the more complex symbol >=.
After Effects and JavaScript are picky about their order – you can’t use =>. If you don’t have a JavaScript reference handy, you can usually poke around and figure out which order is correct. As a default, start by putting the symbols in the same order you would normally say them – for example, greater than (>) or equal to (=).
next page: some more complex examples; performing automatic error checking
(Page 1 of 3 pages for this article 1 2 3 >)
You must be registered to comment. This is an effort to reduce spam. Please REGISTER HERE.
|