(Page 1 of 2 pages for this article 1 2 >)
Saturday, March 06, 2010
Deeper Modes of Expression, Part 3: Deeper Into Arrays
Chris Meyer | 03/06
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.
|
 |
Chris Meyer | 09/04
Adding imperfections can make graphics more compelling to watch.
Nothing is more boring that having to keyframe supposedly random movements. Fortunately, there’s a simple, powerful expression that can add randomization to virtually any parameter for you: wiggle. Here we’ll…
Chris Meyer | 08/01
Causing animations or footage to loop by repeating already-placed keyframes or segments of time.
Our previous installment was about decision-making loops (if/then and while/do statements). This installemnt is about a different kind of loop: repeating…
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.…
|
|
|
|