Education, RocketStock

Using Arrays in After Effects Expressions

Comments

Arrays can provide you with many new possibilities for After Effects expressions. With this quick introduction to arrays, you’ll be improving your workflow in no time.

Have you found yourself wanting to apply an expression to one axis of a layer’s property? For instance, you might want to apply a wiggle expression to just the X-axis of a layer’s position. Or maybe you want to just disable movement on a certain axis.

These things can be achieved with arrays, though the potential of their use extends far beyond these simpler tasks. Even if you are completely new to using expressions, arrays can be great place to start, as they are the foundation upon which multi-dimensional expressions are generally built. Before proceeding further, let’s address the question you’re likely wondering by now: What is an array?


What is an Array?

An array is a list of values, separated by commas, that each represent an individual axis value. Here’s an example for a 3D layer:

[960, 540, 0];

The first value represents the X-axis (horizontal space), the second value represents the Y-axis (vertical space), and the third represents the Z-axis (depth).  With this example applied to a 3D layer’s position, the layer would have an X-position of 960, a Y-position of 540, and a Z-position of 0, which would be the default centerpoint of a 1920X1080 (1080p) composition. If the layer was 2D instead of 3D, it would look like this instead:

[960, 540];

Because 2D layers are flat, they have no Z-axis and therefore do not require a three-part array. While we used position for our first example, arrays can also be applied to other multi-dimensional properties as well, such as scale. For example, this expression would set a 2D layer’s scale to 50% width by 20% height.

[50, 20];


Value Expression

‘Value’ represents the current input value from the property’s control. Whereas the previously demonstrated arrays lock to the inputted values, the ‘value’ phrase will reflect whatever value you selected outside of the expression.

‘value[0]’ represents the X-value, ‘value[1]’ represents the Y-value, and ‘value[2]’ represents the Z-value.

Because ‘value’ indicates a property’s current input value, this following expression would actually not affect a layer’s position at all:

[value[0], value[1], value[2]];

Arrays in After Effects Example

Your layer would maintain the same values it did prior to entering this expression, and it would also maintain any existing keyframes, as well as any future changes to the input. Keep in mind that the X, Y, and Z  numeric designations in brackets (X[0], Y[1], Z[2]) must always be included in an array, following anything other than an actual numeric value.


Variables

Now that we’ve covered the basics of how arrays work, let’s look at how we can incorporate them into more complex expressions. The best way to apply arrays within a larger expression is using variables. A variable is a character, word, or symbol that represents an established value or expression. For example, if you wanted the letter ‘M’ to represent the value 33, you would type in:

M = 33;

You could then reference that variable later in your array, like this:

[M[0], 24, 370);

This would be the equivalent of:

[33, 24, 270];

In this rudimentary example, using a variable isn’t necessary. It becomes more helpful in scenarios where the variable represents something more complex than a single value. Be sure to always define your variable in a line above any expressions that reference said variable.


Application

With the boring part out of the way, we can move on to the practical application of these principles. Say you want to apply a wiggle expression to one axis, as mentioned earlier. We’ll go with the Y-axis, on a 2D object. First, for efficiency’s sake, you would establish the wiggle expression as your variable. We’ll use the letter ‘R’ — though you can use just about any letter, word, or symbol.

R = wiggle(4,20);

Then, you would set up your array.

[value[0], R[1]];

This would preserve your current X  property, while adding a wiggle expression to the Y-axis. The ‘1’ will be replaced with the wiggle expression ‘R.’ Here is the final expression:

R = wiggle(4,20);

[Value[0], R[1]];


Examples

1. Lock Z-Axis Position: If you wanted to lock your Z-position to 20, while maintaining the ability to change your X and Y axis, (perhaps for something like a template) you would input:

[Value[0], Value[1], 20];

2. Uniform Scale Wiggle: This expression would apply a wiggle expression to uniform scale, as opposed to affecting the X and Y axes separately by default:

rocketstock = wiggle(3,43);

[rocketstock[0], rocketstock[0]];

3. Individual Expression Controllers: You can also link expression controls to arrays, if you want to adjust multiple properties with one control layer. You can even use multiple variables within an expression that each represent something different. This expression would allow ‘Slider Control 01’ to control the X-value, while allowing ‘Slider Control 02’ to control the Y-value.

var1 = comp(“Scene Options”).layer(“Scene Options”).effect(“Slider Control 01”)(“Slider”);

var2 = comp(“Scene Options”).layer(“Scene Options”).effect(“Slider Control 02”)(“Slider”);

[var1[0], var2[1]];

4. If/Else Statement to Scale: To wrap up, here’s an example that utilizes an if/else statement. This would set width scale to 100 if ‘Slider Control 01”s value surpasses 3. Otherwise, the value would remain at 20.

a = comp(“Scene Options”).layer(“Scene Options”).effect(“Slider Control 01”)(“Slider”);

b = if(a > 3) 100 else 20;

[b[0], value[1]];


With your new skills in creating arrays, you should now be able to write expressions for 2D and 3D layer properties with ease. Now get out there and create something!