Expressions related to time & frame counts
| Meaning | Type | Code |
|---|---|---|
| Current frame number | Number | time |
| Current second | Number | (time-comp.RenderStart)/comp:GetPrefs("Comp.FrameFormat.Rate") |
| Current percent | Number | (time-comp.RenderStart)/(comp.RenderEnd-comp.RenderStart) |
| Framerate | Number | comp:GetPrefs("Comp.FrameFormat.Rate") |
| Duration | Number | comp.RenderEnd - comp.RenderStart |
| First frame number | Number | comp.RenderStart |
| Last frame number | Number | comp.RenderEnd |
Expressions related to the size of images. Note that not every node uses the ID Input for its image inputs.
| Meaning | Type | Code |
|---|---|---|
| Default Width | Number | comp:GetPrefs("Comp.FrameFormat.Width") |
| Default Height | Number | comp:GetPrefs("Comp.FrameFormat.Height") |
| Input Width | Number | Input.Width |
| Input Height | Number | Input.Height |
Input DataWindow Width (pixels) | Number | self.Input.DataWindow[3] - self.Input.DataWindow[1] |
Input DataWindow Height (pixels) | Number | self.Input.DataWindow[4] - self.Input.DataWindow[2] |
Expressions related to the size of images
| Meaning | Type | Code |
|---|---|---|
| X Component | Number | <input>.X |
| Y Component | Number | <input>.Y |
| Create a Point | Point | Point(<x>,<y>) |
Conditional expression patterns
| Expression | Type | Code |
|---|---|---|
| Inline if and/or | Number | condition and if_true or if_false |
| Inline if function | Number | iif(condition, if_true, if_false) |
You can embed full lua scripts in expressions with the : syntax.
: -- begin script
local str = "Hello Lua!"; -- define a local variable
return Text(str); -- the "output" of the expression script
Linear Interpolation (lerp) blends betwen two numbers. Wikipedia
:
-- lerp function:
-- a = first number
-- b = second number
-- t = [0..1] where 0=a and 1=b
local function lerp(a,b,t) return a + (b-a)*t; end;
-- use lerp() to blend between two values based on a Blend slider
local result = lerp(LowValue, HighValue, Blend);
-- output result
return result;
Quick copy & paste snippet
local function lerp(a,b,t) return a + (b-a)*t; end; -- Number Interpolation
local function lerpxy(a,b,t) return Point(a.X + (b.X-a.X)*t, a.Y + (b.Y-a.Y)*t); end; -- Point Interpolation
Find a position t between two numbers.
:
-- unlerp function:
-- a = first number
-- b = second number
-- c = a number typically between a and b
local function unlerp(a,b,c) return (c-a)/(b-a); end;
-- use unlerp() to find the current time as a percentage from start to end
local percent = unlerp(comp.RenderStart, comp.RenderEnd, time);
-- output percent
return percent;
Quick copy & paste snippet
local function unlerp(a,b,c) return (c-a)/(b-a); end;
You can interpolate the sin() function to create a repeating wave over a range of values.
:
-- setup
local lo = 0.0; -- lowest value
local hi = 1.0; -- highest value
local rate = 1.0; -- cycles per second
local phase = -0.25; -- offset relative to one cycle
-- function definitions
local function unlerp(a,b,c) return (c-a)/(b-a); end;
local function lerp(a,b,t) return a + (b-a)*t; end;
-- calculations
local seconds = time/comp:GetPrefs("Comp.FrameFormat.Rate");
local theta = 2 * math.pi * (phase + rate * seconds);
local t = unlerp(-1.0, 1.0, sin(theta));
return lerp(lo, hi, t);
You can use the string.format(str, ...) function to present multiple variables as text in a more readable way.
| shorthand | Type | Usage |
|---|---|---|
%s | String | string.format("your name is: %s", name); |
%f,%.<precision>f | Float | string.format("angle=%.2f", 33.3333333); -- "angle=33.33" |
%i | Integer | string.format("frame=%d", time); |
%% | Literal | string.format("%i%%", 50); -- "50%" |
:
-- a basic clock
local framerate = comp:GetPrefs("Comp.FrameFormat.Rate");
local seconds = time/framerate;
local minutes = floor(seconds/60);
seconds = seconds - (minutes * 60);
-- format the minutes and seconds variables
local str = string.format("%i minutes, %.1f seconds", minutes, seconds);
return Text(str);
You can use the print() function to see what your expression is doing each frame.
:
-- load some values
local r = TopLeftRed;
local g = TopLeftGreen;
local b = TopLeftBlue;
-- check the values of the variables by printing them to the console
print("red:", r, ", green:", ", blue:", b); -- "red: 0.5000, green: 0.1000, blue: 1.0000"
-- we can clean this up a bit with a format string
print(string.format("rgb(%.2f, %.2f, %.2f)", r,g,b)); -- "rgb(0.50, 0.12, 1.00)"
-- output anything
return r+g+b;
You can use the trace() function to print something inline.
:
local a = time/2;
local b = trace(time/2); -- this is the same, but it prints the result
trace() is very helpful for one-line expressions because it can easily be inserted anywhere without effecting the result.
sin(trace(time/comp:GetPrefs("Comp.FrameFormat.Rate")) * 90)
Parameter Constructors
| Constructor | Type | Usage | Example |
|---|---|---|---|
Point(x,y) | Point | Setting Point inputs like Transform.Center | Point(x,y) |
Text(string) | Text | Setting Text inputs like on Text.StyledText | Text("hello world") |
FuIDParam(string) | FuID | Setting some dropdowns like Blur.Filter | FuIDParam("Gaussian") |
Common math functions
| Tag | Type | Description |
|---|---|---|
| abs(x) | Number | Absolute value of x. |
| ceil(x) | Number | Round up. |
| floor(x) | Number | Round down. |
| pow(a, b) | Number | a^b |
| sqrt(x) | Number | Square root of x. |
| random(min, max) | Number | Random number between min and max. |
Common trigonometry functions
| pi | Number | Constant PI. |
| sin(x), cos(x), tan(x) | Number | Trig functions (radians). |
| asin(x), acos(x), atan(x) | Number | Trig arc functions (radians). |
| deg(radians) | Number | Radians to degrees. |
| rad(degrees) | Number | Degrees to radians. |
Because Expressions are Lua code, you can take advantage of the built in Lua math library.
| Function | Type | Description |
|---|---|---|
math.abs(x) | Number | Absolute Value of x |
math.sqrt(x) | Number | Square root of x |
math.ceil(n) | Number | Round up to an integer |
math.floor(n) | Number | Round down to an integer |
math.max(...) | Number | Returns the largest number |
math.min(...) | Number | Returns the lowest number |
math.exp(n) | Number | e^n |
math.log(n) | Number | Natural logarithm of n |
math.modf(n) | Number, Number | Returns integer part, fraction part |
math.fmod(n1, n2) | Number | The remainder of n1/n2 |
math.random() | Number | Pseudo-random float between 0 and 1 |
math.random(lower, upper) | Number | Pseudo-random integer between lower and upper |
math.randomseed() | Number | Sets a seed for the pseudo-random generator |
math.tointeger(any) | Number | String to ineger |
math.type(n) | String|nil | Returns one of "float" "integer" and nil |
math.pi | Number | Constant PI |
math.huge | Number | Constant positive infinity |
math.mininteger | Number | Constant lowest possible integer |
math.maxinteger | Number | Constant largest possible integer |
math.deg(radians) | Number | Radians to Degrees |
math.rad(degrees) | Number | Degrees to Radians |
math.sin(radians) | Number | Trig sin function |
math.cos(radians) | Number | Trig cosine function |
math.tan(radians) | Number | Trig tangent function |
math.asin(value) | Number | Inverse sin |
math.acos(value) | Number | Inverse cosine |
math.atan(value) | Number | Inverse tangent |