Expressions

DaVinci Resolve Cheat Sheet

Expressions

Time

Expressions related to time & frame counts

MeaningTypeCode
Current frame numberNumbertime
Current secondNumber(time-comp.RenderStart)/comp:GetPrefs("Comp.FrameFormat.Rate")
Current percentNumber(time-comp.RenderStart)/(comp.RenderEnd-comp.RenderStart)
FramerateNumbercomp:GetPrefs("Comp.FrameFormat.Rate")
DurationNumbercomp.RenderEnd - comp.RenderStart
First frame numberNumbercomp.RenderStart
Last frame numberNumbercomp.RenderEnd

Image

Expressions related to the size of images. Note that not every node uses the ID Input for its image inputs.

MeaningTypeCode
Default WidthNumbercomp:GetPrefs("Comp.FrameFormat.Width")
Default HeightNumbercomp:GetPrefs("Comp.FrameFormat.Height")
Input WidthNumberInput.Width
Input HeightNumberInput.Height
Input DataWindow Width (pixels)Numberself.Input.DataWindow[3] - self.Input.DataWindow[1]
Input DataWindow Height (pixels)Numberself.Input.DataWindow[4] - self.Input.DataWindow[2]

Point

Expressions related to the size of images

MeaningTypeCode
X ComponentNumber<input>.X
Y ComponentNumber<input>.Y
Create a PointPointPoint(<x>,<y>)

If

Conditional expression patterns

ExpressionTypeCode
Inline if and/or Numbercondition and if_true or if_false
Inline if functionNumberiif(condition, if_true, if_false)

Scripts

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

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

Inverse Linear 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;

Wave Functions

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);

Format Strings

You can use the string.format(str, ...) function to present multiple variables as text in a more readable way.

shorthandTypeUsage
%sStringstring.format("your name is: %s", name);
%f,%.<precision>fFloatstring.format("angle=%.2f", 33.3333333); -- "angle=33.33"
%iIntegerstring.format("frame=%d", time);
%%Literalstring.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);

Debugging

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)

Types

Parameter Constructors

ConstructorTypeUsageExample
Point(x,y)PointSetting Point inputs like Transform.CenterPoint(x,y)
Text(string)TextSetting Text inputs like on Text.StyledTextText("hello world")
FuIDParam(string)FuIDSetting some dropdowns like Blur.FilterFuIDParam("Gaussian")

Math

Common math functions

TagTypeDescription
abs(x)NumberAbsolute value of x.
ceil(x)NumberRound up.
floor(x)NumberRound down.
pow(a, b)Numbera^b
sqrt(x)NumberSquare root of x.
random(min, max)NumberRandom number between min and max.

Trig

Common trigonometry functions

piNumberConstant PI.
sin(x), cos(x), tan(x)NumberTrig functions (radians).
asin(x), acos(x), atan(x)NumberTrig arc functions (radians).
deg(radians)NumberRadians to degrees.
rad(degrees)NumberDegrees to radians.

Lua

Because Expressions are Lua code, you can take advantage of the built in Lua math library.

FunctionTypeDescription
math.abs(x)NumberAbsolute Value of x
math.sqrt(x)NumberSquare root of x
math.ceil(n)NumberRound up to an integer
math.floor(n)NumberRound down to an integer
math.max(...)NumberReturns the largest number
math.min(...)NumberReturns the lowest number
math.exp(n)Numbere^n
math.log(n)NumberNatural logarithm of n
math.modf(n)Number, NumberReturns integer part, fraction part
math.fmod(n1, n2)NumberThe remainder of n1/n2
math.random()NumberPseudo-random float between 0 and 1
math.random(lower, upper)NumberPseudo-random integer between lower and upper
math.randomseed()NumberSets a seed for the pseudo-random generator
math.tointeger(any)NumberString to ineger
math.type(n)String|nilReturns one of "float" "integer" and nil
math.piNumberConstant PI
math.hugeNumberConstant positive infinity
math.minintegerNumberConstant lowest possible integer
math.maxintegerNumberConstant largest possible integer
math.deg(radians)NumberRadians to Degrees
math.rad(degrees)NumberDegrees to Radians
math.sin(radians)NumberTrig sin function
math.cos(radians)NumberTrig cosine function
math.tan(radians)NumberTrig tangent function
math.asin(value)NumberInverse sin
math.acos(value)NumberInverse cosine
math.atan(value)NumberInverse tangent