< Previous | Contents | Next >

Performing Calculations in Parameter Fields

You can enter simple mathematical equations directly in a number field to calculate a desired value. For example, typing 2.0 + 4.0 in most number fields will result in a value of 6.0. This can be helpful when you want a parameter to be the sum of two other parameters or a fraction of the screen resolution.


Using SimpleExpressions

Simple Expressions are a special type of script that can be placed alongside the parameter it is controlling. These are useful for setting simple calculations, building unidirectional parameter connections, or a combination of both. You add a SimpleExpression by entering an equals sign directly in the number field of the parameter and then pressing Return.


image

Entering an equals sign opens the SimpleExpression field with Pick Whip control


image

An empty field will appear below the parameter, and a yellow indicator will appear to the left. The current value of the parameter will be entered into the number field. Using Simple Expressions, you can enter a mathematical formula that drives the value of a parameter or even links two different parameters. This helps when you want to create an animation that is too difficult or impossible to set up with keyframing. For instance, to create a pulsating object, you can use the sine and time functions on a Size parameter. Dividing the time function can slow down the pulsing while multiplying it can increase the rate.


image

A SimpleExpression using the sine and time functions


Inside the SimpleExpression text box, you can enter one-line scripts in Lua with some Fusion-specific shorthand. Some examples of Simple Expressions and their syntax include:


Expression

Description

time

This returns the current frame number.

Merge1.Blend

This returns the value of another input, Blend, from another node, Merge1.

Merge1:GetValue("Blend", time-5)

This returns the value from another input, but sampled at a different frame, in this case five frames before the current one.

Expression




Description

sin(time/20)/2+.5

This returns a sine wave between 0 and 1.

iif(Merge1.Blend == 0,

0,

1)

This returns 0 if the Blend value is 0, and returns 1 if it is not. The iff() function is a shorthand conditional statement, if-then-else.

iif(Input.Metadata.ColorSpaceID == "sRGB", 0, 1)

This returns 0 if the image connected to the current node’s Input is tagged with the sRGB colorspace. When no other node name is supplied, the expression assumes the Input is coming from the current node. It is equivalent to self.Input. The Input in most, but not all, Fusion nodes is the main image input shown in the Node Editor as an orange triangle. Images have members that you can read, such as Depth, Width, Metadata, and so on.

Point(Text1.Center.X, Text1. Center.Y-.1)

Unlike the previous examples, this returns a Point, not a Number. Point inputs use two

members, X and Y. In this example, the Point returned is 1/10 of the image height below the Text1’s Center. This can be useful for making unidirectional parameter links, like offsetting one Text from another.

Text1.Center - Point(0,.1)

This is similar to the previous expression.

This SimpleExpression returns Text instead of a Number or Point.

Text("Colorspace: "..(Merge1. Background.Metadata.ColorSpaceID)

The string inside the quotes is concatenated with the metadata string, perhaps returning:

Colorspace: sRGB

Text("Rendered "..os.date("%b %d,

%Y").. " at "..os.date("%H:%M").."\n on the computer "..os. getenv("COMPUTERNAME").. " running "..os. getenv("OS").."\n from the comp "..ToUNC(comp.Filename))

This returns a much larger Text, perhaps something like:

Rendered Nov 12, 2019 at 15:43 on the computer Rn309 running Windows_NT from the comp \\SRVR\Proj\Am109\ SlateGenerator_A01.comp

os.date("%H:%M")

The OS library can pull various information about the computer. In the previous example, os.date gets the date and time in hours:minutes.

"..os.getenv("COMPUTERNAME").. "

running "..os.

Any environment variable can be read by os.getenv, in this case the computer name and the operating system.

"\n from the comp "..ToUNC(comp. Filename)

To get a new line in the Text, \n is used. Various attributes from the comp can be accessed with the comp variable, like the filename, expressed as a UNC path.

image


image


TIP: When working with long SimpleExpressions, it may be helpful to drag the Inspector panel out to make it wider or to copy/paste from a text editor or the Console.


TIP: When working with long SimpleExpressions, it may be helpful to drag the Inspector panel out to make it wider or to copy/paste from a text editor or the Console.


TIP: When working with long SimpleExpressions, it may be helpful to drag the Inspector panel out to make it wider or to copy/paste from a text editor or the Console.

 

Pick Whipping to Create an ExpressionRemoving SimpleExpressions