The Language of Expressions
Getting at the Data
myArray = [5,6,7,8];
So here we're using brackets to define the contents of the array "myArray". We would also use brackets to access the individual elements of the array. For example, myArray would be 5, myArray would be 6, and so on. This dual purposing of the brackets can be a little confusing, but a helpful way to remember this is that when you see brackets not appended to anything (as in "myArray = [5,6,7,8];) they are being used to define an array and the numbers between the brackets are the actual elements of the array. When the brackets are appended to an object or property (as in myArray) they are being used to retrieve a particular element of the array and the number between the brackets is the index.
Let's look a simple example that demonstrates both uses. Let's say you wanted to write an expression that added 10 to a layer's x Position and 25 to the layer's y Position. You could write an expression like this:
newX = position + 10; newY = position + 25; [newX,newY]
Let's go through it line by line. In the first line, the index 0 is used to retrieve the x component of the Position property, 10 is added to it, and the result is stored in a variable named "newX". In the second line, the index 1 is used to retrieve the y component of the Position property, 25 is added to it, and the result is stored in a variable named "newY". In the final line, a new two-element array is defined that consists of the elements newX and newY and that array is plugged into the value of the Position property.
Now we're going to take a quick look at basic array math. Most of the array math that you will need to use in your expressions is pretty simple. First let's look at a simple expression for Position that adds two arrays.
a = [100,150]; b = [200,250]; a + b
The result of this is (as you might have guessed) [300,400]. That is, the two arrays ("a" and "b") have been added together, component by component, with the first element of "a" being added to the first element of "b", and so on. So the result of this operation is that the new Position calculated by the expression is x = 300, y = 400. Just to exercise our newfound knowledge of arrays, and to demonstrate what's going on internally, let's write the last line another way.
a = [100,150]; b = [200,250]; [a + b,a + b]
This looks a lot more complicated but it accomplishes the same thing. Here we have manually added the individual components together separately and then built a two-element array for the result. With the first method, we let After Effects do the adding and array building. That's the way you'll want to do it if you can, but there are plenty of situations where you will need to construct the resulting array yourself.
After Effects is pretty forgiving if you try to add two arrays of different lengths. The result will be an array the size of the larger of the two. For example, [1,2,3] + [4,5] will give you [5,7,3]. After Effects has kindly filled in the third element of our [4,5] array with a zero before the addition.
Array subtraction works in a similar way. Can you guess the result of this expression?
a = [500,400]; b = [200,300]; a - b
If you guessed [300,100], you're right. It's getting easier, right? Well, just to confuse you, multiplication and division don't work the same way. After Effects doesn't know how to multiply two arrays together, but it does know how to multiply an array by a number (or "scalar", as we'll call it when we get into vectors). The result is not too surprising. Here's an example:
a = [100,150]; a*10
The result of this would be the array [1000,1500]. After Effects has multiplied each element of the array by 10. Similarly, the result of this division expression:
a = [100,150]; a/10
would be [10,15].
Note that the order of the operands is important for division. After Effects is smart enough to figure out that 10*[100,100] is the same as [100,100]*10, but if you try 10/[100,100] you'll get an error because After Effects doesn't know how to divide a number by an array.