About Variables, Linking and Expressions

Linking Parameters

You can access information from any node and use it in a different node by linking variables and parameters.

To use a parameter inside of the same node, just type the parameter name. For example, the Move2D node links the yScale to be equal to the xScale by default:

To use a parameter in a different node, use the node name, followed by the parameter name, separated by a period:

node.parameter

This example links the value parameter from the node Fade1 and multiplies it by 2.

If you want to refer to a globally declared variable, simply use its name in the parameter field. This example links to the global variable my_val.


You can declare a variable anywhere in the script. However, to make it available in the interface, you have to prefix it with the curve declaration:

curve parameter_name = expression;

For example, to declare the my_val variable for the above example, you would do:

curve my_val = 1;

Linking to a Parameter at a Different Frame

You can use the syntax parameterName@@time to look at a value at a different frame:

Move2D1.yPan@@(time-1)

will look at Move2D1's yPan parameter at the previous frame.

 

Variables

You can declare your own variables, as shown above. However, each image node also carries with it the information about that node, using the variables width, height, and bytes. When you are referring to these, they work exactly the same as above. For example, by default, the center of rotation on Move2D is set to:

 width/2

This places the center at the mid point of the current image.

When referring to these from a different node, you place the node name before the variable:

node_name.width

In some cases, there might arise a problem. For example, in a Resize node, what happens when you put the Resize to be equal to width/2? This potentially causes a loop because it is changing width based upon a value that is looking for the width. To solve this, Shake always refers to the input width, height, and bit depth when you are referring to these from inside of that node. Therefore, width/2 takes the incoming width and divides it by 2. When you refer to the width, height, and bit depth of a different node, you use that node's output values.

At any time, you can also use the variable time, which refers to the current frame number. For example, put cos(time/5)*50 into an angle on Rotate and you get a nice rocking motion.


Expressions

These are all examples of using expressions, which really help out the lazy compositor by doing your work for you. In any parameter, you can combine any value with a math expression, trig functions, an animated curve, a variable, or even a conditional expression. For example, as mentioned above, the center of an image can be found by using:

 xCenter = width/2
 yCenter = height/2

These take the per-image variables width and height and divides them by 2.

You can type an expression in any field. Some functions, like ColorX, WarpX, and TimeX, even support locally-declared variables. See ColorX for more explanation, and also a list of examples.

If you are using the command line method, you may have to enclose your expressions in quotes to avoid problems with the OS reading the command. For example, instead of:

shake my_image.iff -rot 45*6

try

shake my_image.iff -rot "45*6"
Expressions Explanation
* Multiply
/ Divide
+ Plus
- Subtract
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
&& And
|| Or
! Not
expr1?expr2:expr3 If expr1 is true (non-zero), then to expr2, else do expr3
Examples Explanation
1/2.2 1 divided by 2.2. Gives you the inverse of 2.2 gamma.
2*Linear(0,0@1,200@20) Multiplies the value of an animated curve by 2.
2*my_curve Multiplies a variable by 2.
sqrt(my_curve-my_center)/2 Subtracts my_center from my_curve and takes the square root of that, and then divides by 2.
time>20?1:0 If time is greater than 20, then the parameter is 1, else it equals 0.
cos(time/5)*50 gives a smooth ping-pong between -50 and 50.

 

These are just samples to let you know you can do this sort of thing. For a complete listing of Shake functions that can be used in an expression, jump to Function By Class: Expressions.


Precedence

The above operators are listed in order of precedence, meaning which order
Shake evaluates each operator, left to right. If this is a pain to keep up with
(it is), make liberal use of parentheses to force the order of evaluation. For
instance:

a = 1 + 2 * 4 -2 

This expression does "2*4" first since the "*" has precedence over "+" and "-" which gives you "a=1+8-2". Then from left to right, Shake does "1+8", giving "a=9-2", finally resulting in "a=7". To add and subtract before multiplying, use parentheses to control the evaluation.

a = (1 + 2) * (4 - 2) 

This results in "a=3*2" or "a=6". Parentheses have the highest precedence in an expression.