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

Sunday, March 07, 2010

Filed under: Post ProductionVisual Effects

Deeper Modes of Expression, Part 3: Deeper Into Arrays

Chris Meyer | 03/07

How to translate values between parameters that have different dimensions.

We’re in the process of serializing the Deeper Modes of Expression bonus chapter from our book Creating Motion Graphics with After Effects into a set of 12 posts here on PVC. One of the more mystifying areas of expressions - especially for nonprogrammers - is dealing with arrays. Problems with arrays are the cause of a large bulk of the error messages you will encounter with expressions. Here we will recap the basics, dive into performing additional math operations on arrays, and end with a couple of nice expressions that can measure the distance between layers, or cause one layer to automatically face another.

Arrays 101

As a refresher, when a property in After Effects has more than one value – such as Position, which has X and Y values – it is referred to as having more than one dimension. The collection of its dimensions is referred to as an array. The answer to 2D position is referred to as “an array with two dimensions.” In expressions, any multidimensional value is referred to as an array: For example, 3D position has three dimensions; color has four dimensions – red, green, blue, and alpha. We will discuss color in expressions in a future installment.

(You will often hear the term vector used interchangeably with array. Some reserve the term vector to mean only an array that reflects a position or direction, rather than a multivalue property such as color. We won’t bother splitting those hairs here; for our purposes, assume the two are interchangeable.)

An array is written as a series of values inside square brackets separated by commas – for example, the position X = 360, Y = 243 is written as [360, 243]. The dimensions of an array can be addressed individually, with numbering starting at zero. This is referred to as “indexing into an array.” These index numbers are also surrounded by square brackets. In the example we’ve given here, position[0] = 360, and position[1] = 243. If you wanted to be completely redundant and write an expression that sets Position equal to itself, you would enter [position[0], position[1]].

If you are tying one property to another property, and both have the same number of dimensions, you don’t need to use indexing; After Effects will automatically line up the respective dimensions between the arrays (for example, X Position to X Scale). If the properties have a different number of dimensions, then you need to worry about using indexes to specify which dimension of an array you are talking about.

If you do not specify an index, After Effects usually assumes you mean the first dimension, or value[0]. However, most of the time you need to explicitly state which index of an array you mean, or After Effects will give you an error message, such as shown below. Note that the error message says “no index specified.” This is easy to fix: you could add [0] or [1] after the word position in the expression text, or repair it by using the pickwhip to drag to the correct parameter (which will automatically create correct expression code) - for example, here we would have pick whipped from Rotation to one of the Position values in the Timeline panel.

Many errors with arrays are caused by not paying attention to the number of dimensions each property has. Trying to assign a one-dimensional Rotation value to equal a two-dimensional Position value without saying which Position dimension we want (above) results in an error that the result must have one dimension, not two (left).

Vector and Array Math

You can perform math operations inside an array’s brackets, as long as you honor the commas separating its dimensions. For example, if you wanted X Position to equal the layer’s Rotation plus 90, and Y Position to equal the layer’s X Scale times two, you would write [rotation + 90, scale[0] * 2].

There are times you want to perform a math operation on an entire array. Often, you can treat an array similar to a normal value. After Effects also has a number of vector math methods (math functions you can perform on arrays), which usually take the form of a word that signifies what you want to do – such as “mul” for multiply – followed by the array and the number you want to modify it by, both enclosed inside parentheses. The After Effects Help file has a complete list of the supported vector math methods. The expression language menu in the Timeline panel will also show you what methods are available, and what format they require.

To remind yourself of the expression methods that are available, and the format they require, click on the arrow to the right of the pick whip and use the expression language menu. You will need to replace its hints – such as “vec1” (for vector 1) – with the properties you’re using, such as position.

Here are the vector math functions we use most often, including alternate ways to accomplish the same result using Vector Math methods:

Addition: If you add a single value to an array, this value will be added only to the first dimension of an array. For example, if a layer’s initial Position was 160,120 and you wrote the expression position + 40, the result would be Position = 200,120. If you wanted to add 40 to both dimensions, you could write position + [40,40], or add(position, [40,40]). Finally, you can brute force the math by writing [position[0] + 40, position[1] + 40].


The expressions for layers 2 through 4 do the same thing: add 40 to both the X and Y Position. The expression for layer 1 adds 40 to only X Position, as we are adding a one-dimensional number to a two-dimensional property.

Subtraction: This works the same way as addition. If you subtract a property with one dimension – such as rotation, or the number 40 – from a property with more than one dimension, only the first dimension will be affected. To affect both dimensions, subtract another array (such as position – [40,40]) or use the “sub” method (for example, sub(position, [40,40]). Finally, you can brute-force the math by writing [position[0] – 40, position[1] – 40].

Multiplication: Unlike addition and subtraction, where you need to address each dimension in an array individually, you can multiply all the numbers in an array by a single value. For example, if you want one layer’s Scale to be 60% of another layer’s Scale, you can write either thisComp.layer(“blue”).scale * 0.60 or mul(thisComp.layer(“blue”).scale, 0.60). If you want to address each dimension in an array individually, you will need to do it by brute force, such as [value[0] * 0.65, value[1] * 0.55].

To have the Scale of other layers be a percentage of a master layer’s Scale, you can either multiply the Scale property of the master layer by a single value, or use the “mul” vector math method. Here, the expressions for layers 2 and 3 accomplish the same task: multiplying the blue layer’s scale array value by 0.60.

Division: Division is very similar to multiplication: You can divide all the numbers in an array by a single value. For example, if you want one layer’s Scale to be half of another layer’s Scale, you can write either thisComp.layer(“blue”).scale / 2 or div(thisComp.layer(“blue”).scale, 2). If you want to address each dimension in an array individually, you will need to do it by brute force, such as [value[0] / 2, value[1] / 3].

next page: measuring the distance between layers; causing one layer to face another

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

               



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…

After Effects Apprentice Free Video: Drawing Parametric Shapes

Chris and Trish Meyer | 01/27

The same tools you’ve used to create masks can also be used to create Shape Layers - with far more options than you might have imagined.

As we mentioned awhile back, we’ve been busy the past year and a half creating an extensive, multi-course video training…

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…

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