(Page 1 of 3 pages for this article  1 2 3 >)

Sunday, July 04, 2010

Filed under: Motion GraphicsPost ProductionVisual Effects

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.

Name:

Email:

Location:

URL:

Smileys

Remember my personal information

Notify me of follow-up comments?

Submit the word you see below:




After Effects Apprentice Free Video: Exploring Shape Effects
After Effects Apprentice Free Video: Drawing Parametric Shapes
After Effects Apprentice Free Video: The Puppet Starch Tool
After Effects Apprentice Free Video: Eraser Tool Modes
After Effects Apprentice Free Video: Tracking with mochaAE
After Effects Apprentice Free Video: Performing a Motion Track
After Effects Apprentice Free Video: Creating 3D Objects using Adobe Repoussé
After Effects Apprentice Free Video: Understanding Axis Modes
Using After Effects as an Advanced Titler for Premiere Pro
After Effects Apprentice Free Video: Creating an Orbit Camera Rig
CMG Hidden Gems: Chapter 45 – What’s Your Preference?
CMG Hidden Gems: Chapter 44 – Prerendering and Proxies
CMG Hidden Gems: Chapter 43 – Advanced Rendering
CMG Hidden Gems: Chapter 42 – Render Queue
CMG Hidden Gems: Chapter 41 – Video Issues
CMG Hidden Gems: Chapter 40B – 3D Channel Effects
CMG Hidden Gems: Chapter 40 – Integrating with 3D Applications
CMG Hidden Gems: Chapter 39 – Integration 101
After Effects Apprentice Free Video: Multiple Playback Speeds
CMG Hidden Gems: Chapter 38 – Import and Interpret
CMG Hidden Gems: Chapter 37B and 37C – Expressions and Scripting Bonus Chapters
CMG Hidden Gems: Chapter 37 – Expressions
CMG Hidden Gems: Chapter 36B – Audio Effects
CMG Hidden Gems: Chapter 36 – Working With Audio
Upgrading to After Effects CS5.5?
CMG Hidden Gems: Chapter 35 – The Puppet Tools
After Effects Apprentice Free Video: The Wiggle Expression
CMG Hidden Gems: Chapter 34 – Roto Brush
CMG Hidden Gems: Chapter 33 – Paint and Clone
After Effects Apprentice Free Video: What Could Go Wrong?







Expression Shorts - Numerical Readout

David Torno | 02/05

Create numerical readouts for use in HUD style graphics.

image

With this Expression, I will show you how to feed numerical property information…

CMG Hidden Gems: Chapter 37B and 37C – Expressions and Scripting Bonus Chapters

Chris and Trish Meyer | 09/02

This time, a collection of resources on expressions and scripting - including a PDF of Bonus Chapter 37C on scripting.

We’re going through our book Creating Motion Graphics with After Effects 5th Edition (CMG5) and pulling out a few “hidden gems” from…

CMG Hidden Gems: Chapter 37 – Expressions

Chris and Trish Meyer | 08/27

Another selection of “hidden gems” (and essential advice), this time from Chapter 37 of Creating Motion Graphics with After Effects.

We’re going through our book Creating Motion Graphics with After Effects 5th Edition (CMG5) and pulling out a few “hidden gems” from…

After Effects Apprentice Free Video: The Wiggle Expression

Chris and Trish Meyer | 08/02

This very simple expression can be used to add randomness to virtually any parameter.

As we mentioned earlier, we’ve been busy this year creating an extensive, multi-course video training series based on…

To be considered for listing, contact pr (at) provideocoalition (dot) com


Copyright © 2011, HD Expo, LLC a division of Diversified Business Communications. DBA Createasphere

All rights reserved. HD EXPO, High Def EXPO, Createasphere, E-Tech, Entertainment Technology Exposition, 3D Production Workshop, VariCamp, P2 Camp, ColorCamp 101, and Lighting, Filters & Gels for HD are all trademarks of HD Expo, LLC.

Terms of Use  |  Privacy Policy

Check PageRank