John Holt
114 supporters
Expressions

Expressions

Mar 03, 2021

The world of expressions in both video editing and motion graphics opens up a world of possibilities. In this article, I am going to bring together expressions I have discovered and experimented with in DaVinci Resolve. This list is by no means complete but more of a working document I will come back to again and again.

When I experiment with an expression I have gotten into the habit of placing a Text Node into the composition and adding the expression in the styled text expression input. This allows me to see the output of the expression in a readable format. I can then tweak the expression to get my desired result.

Another tip: Use the expression first before renaming a node as renaming a node will update the expression.

Placing a "--" in a simple expression will ignore the characters to the right. As in "GetPrefs("Comp.FrameFormat.Rate") -- Returns TL FPS" It is a great way to add a reminder.

A list of expressions with comments.

In no particular order.

Basic for DaVinci Resolve

time This will return the current frame. (use on the "Angle" parameter to rotate an object anti-clockwise)

time-1 Returns the value of the previous frame. (time-n) (n=the previous frame number count, therefore time-10 would be 10 frames back from the current time)

-time Returns the negative value of the current frame. (use on the "Angle" parameter to rotate an object clockwise)

ceil() Rounds up to the next whole number

math.ceil() Rounds up to the next whole number

floor() Rounds down to the next whole number

math.floor() Rounds down to the next whole number

abs Converts a negative number to a positive number

time/600 Returns the current frame divided by 600 (can be used to control speed, the higher the number the slower)

time*600Returns the current frame multiplied by 600 (can be used to control speed, the higher the number the faster)

sin(time) This results in a progression from -1 through to 1 with 14 decimal places

ceil(sin(time)) This results in a progression from 0 to +1 over 2 frames as ceil round the decimal points up.

floor(sin(time)) This results in a progression -1 to 0 over 2 frames as ceil rounds the decimal points down.

comp.RenderEnd This will return the number of frames in the current composition

comp.RenderEnd/2 Returns the halfway position frame number of the composition

(1-(time/comp.RenderEnd)*2) This results in a progression from 0 through to 1 within 50% of the comp length and is ideal for creating a responsive animation

((comp.RenderEnd-comp.RenderStart)) Results in the number of frames in the composition

time/comp.RenderEnd This results in a progression from 0 through to 1 and is ideal for creating a responsive animation

1-(time/comp.RenderEnd) This results in a progression from 1 through to 0 and is ideal for creating a responsive animation

(time*360)/fps 1 Full rotation every second. (fps = the timeline framerate.) used on the "Angle" parameter

(time360)/(fps2) 1 Full rotation every 2 seconds. (fps = the timeline framerate.) used on the "Angle" parameter

ceil(time/fps) Results in counting up in second. (fps = the timeline framerate.)

ceil((time)/comp:GetPrefs("Comp.FrameFormat.Rate")) Results in counting up in second. (useful when fps is not known)

ceil((comp.RenderEnd-time)/fps)Returns in counting down in seconds based on the composition length. (fps = the timeline framerate.)

ceil((comp.RenderEnd-time)/comp:GetPrefs("Comp.FrameFormat.Rate")) Returns in counting down in seconds based on the composition length, (useful when fps is not known)

max(floor(30-(time/fps)),0)Results in counting down from 30 to 0 in whole numbers. (fps = the timeline framerate.)

ceil(time/comp.RenderEnd*100) Returns the percentage up of the composition length.

ceil(time/comp.RenderEnd*100) .. "%" Returns the percentage up of the composition length and concatenates .. the % sign at the end. (All strings require "")

ceil((1-time/comp.RenderEnd)*100 Returns the percentage down of the composition length.

ceil((1-time/comp.RenderEnd)*100) .. "%" Returns the percentage down of the composition length and concatenates .. the % sign at the end. (All strings require "")

math.floor(PolylineStroke1.WriteOnEnd*100).."%" Create "Styled Text" percentage of a "Wipe On" effect.

math.floor(Circle1.Radius*100) Count up based on a circle radius.

math.floor(Rectangle1.Width*50).."%" Create "Styled Text" percentage of a "Rectangle Mask"

:randomseed(floor(time/6)) ; return random() will give you a simple 0.1.0 value changing every 6 frames

ceil(time/comp.RenderEnd*100) .. "%" Results in Percentage up of the comp ".." concatenates the % to the end

"Progress: " .. ceil(time/comp.RenderEnd*100) .. "%" Results in Progress Percentage up the comp ".." concatenates the % to the end

ceil((1-time/comp.RenderEnd)*100) .. "%" Results in Percentage down the comp ".." concatenates the % to the end

"Progress: " .. ceil((1-time/comp.RenderEnd)*100) .. "%" Results in Progress Percentage down of the comp ".." concatenates the % to the end

"Loading " .. ceil(time/24) .. "/" .. ceil(comp.RenderEnd/fps) Results in Loading Time of the comp


Text driven expressions

((Text1.Output.DataWindow[4]-Text1.Output.DataWindow[2])/Text1.Output.Height)Returns the height of DataWindow of a Text node. (useful for determining the height of text)

((Text1.Output.DataWindow[3]-Text1.Output.DataWindow[1])/Text1.Output.Width) Returns the width of DataWindow of a Text node. (useful for determining the width of text)

[1]=left side,[ 2]=top, [3]=right side, [4]=bottom

Text1.StyledText.Value OR Text1.StyledText[0] Returns the text from a text+ node

Text1.StyledText.Value .. Text2.StyledText.Value Concatenates 2 Styled Text fields

string.len(Text1.StyledText.Value Return the length of a String

:_, mylines = string.gsub(Text1.StyledText.Value, "\n", ""); return mylines Returns the number of lines in a Styled Text field

min(time/(comp.RenderEnd/2),1)-(Text1.Delay) Used in the scrambled text randomness to end the scramble halfway through the comp (from word to scrambled) delay being a slider control from 0 to .25 to allow for a delay. Used a TimeSpeed speed -1 to reverse the comp. Delay is a slider set from 0 to .25.

1-(time/comp.RenderEnd) Used in the scrambled text randomness from scrambled to the word.

Write On Expression

time/(comp.RenderEnd/fps) Results in one letter per frame” write on”. (fps = the timeline framerate.)

time/(comp.RenderEnd/fps)*2 Results in two letters per frame “write on” (fps = the timeline framerate.)

time/(comp.RenderEnd/fps)/2 Results in the word “write on” within half the fps 15 frames. (fps = the timeline framerate.)

time/(comp.RenderEnd/comp:GetPrefs("Comp.FrameFormat.Rate"))/2Results in the word “write on” within half the fps when the framerate isn't known. Be careful with odd fps 23.976 etc as the animation will attempt to be in between frames.

time/(comp.RenderEnd/comp:GetPrefs("Comp.FrameFormat.Rate"))/(Text1.WriteOn)Adding a slider Control "WriteOn" with an integer and steps of 2 the user can control the write on speed


Mask Expressions

floor(-1*((Rectangle1.Height))/2) or floor(-Rectangle1.Height)/2Returns the bottom position of a mask.

floor((Rectangle1.Height)/2) Returns the top position of a mask.

I haven't tried yet but changing Height to Width should get the side positions.

(Ideally use a transform as a null object)

Point(Rectangle1.Center.Y-(Rectangle1.Width/2)+(Ellipse1.Width/2))Place the edge of item on the inside left of mask

Point(Rectangle1.Center.Y-(Rectabgle1.Width/2)-(Ellipse1.Width/2)) Place the edge of item on the outside left of mask

Point(Rectangle1.Center.X, (Rectangle1.Center.Y+(Rectangle1.Height/2))) Returns the top position of a rectangle mask

Point(Rectangle1.Center.X, (Rectangle1.Center.Y-(Rectangle.Height/2))) Returns the bottom position of a rectangle mask

Point(Rectangle1.Center.Y+(Rectangle1.Width/2)) Returns the Right position of a rectangle mask

Point(Rectangle1.Center.Y-(Rectangle1.Width/2)) Returns the left position of a rectangle mask

math.floor(((Transform1.XSize*100)-100)+100).."%" % reading to Empty. % reducing as the width of a RectangleMask gets smaller

math.floor((100-Transform1.XSize*100)).."%" % reading to Full. % increasing as the width of a RectangleMask gets larger


Displays the date & os functions

Text1.StyledText = os.date('%x') Returns the date. Change the %x from below will give the stated result.

  • %a abbreviated weekday name (e.g., Wed)

  • %A full weekday name (e.g., Wednesday)

  • %b abbreviated month name (e.g., Sep)

  • %B full month name (e.g., September)

  • %c date and time (e.g., 09/16/98 23:48:10)

  • %d day of the month (16) [01-31]

  • %H hour, using a 24-hour clock (23) [00-23]

  • %I hour, using a 12-hour clock (11) [01-12]

  • %M minute (48) [00-59]

  • %m month (09) [01-12]

  • %p either "am" or "pm" (pm)

  • %S second (10) [00-61]

  • %w weekday (3) [0-6 = Sunday-Saturday]

  • %x date (e.g., 09/16/98)

  • %X time (e.g., 23:48:10)

  • %Y full year (1998)

  • %y two-digit year (98) [00-99]

  • %% the character `%´

Open a local web page (Windows)

os.execute ("start ../galleries/gallery.html" ) Opens the default installed browser with the local .htm file. Used inside an edit control button.

Open a local web page (Mac)

os.execute ("open ../galleries/gallery.html" ) Opens the default installed browser with the local .htm file. Used inside an edit control button.

Opens a Webpage (Mac)

os.execute('open "" "https://www.resolveassets.co.uk"')Used inside an edit control button

Opens a webpage (Windows)

os.execute('start "" "https://www.resolveassets.co.uk"')Used inside an edit control button.

Not an expression as such but the LUA for adding a button to a node in a text editor. This is placed on the line below ViewInfo = OperatorInfo { Pos = { 0, 82.5 } },

UserControls = ordered() {

ScriptButton = {

LINKS_Name = "ScriptButton",

LINKID_DataType = "Number",

INPID_InputControl = "ButtonControl",

INP_Integer = false,

BTNCS_Execute = [[

os.execute('open "" "https://www.resolveassets.co.uk"')

os.execute('start "" "https://www.resolveassets.co.uk"')

]],

ICS_ControlPage = "Controls"

}

}


Reference a value of a node in another node

Transform1:Angle The result is the angle of the transform node. Therefore you can reference any node property inside any other node. (nodeName:property)


Get Value from a specific time/frame

Rectangle1:GetValue("Width", 10) Returns the width of the node "Rectangle1" at frame 10

Rectangle1.Width Returns the value of the width from the "Rectangle1" node at every frame throughout the composition

Merge1:GetValue("Center", time-1).X To get the X-coordinate of a point from the previous frame

self:GetValue(....) To get the value of a control on the same tool at a different time, use the self notation:


Get A Previous Node Value

I haven't really come up with a practical use case for this expression yet but have experimented with changing the colour of a box based on the width of the box. However, the practical applications for this expression are limitless.

self:GetSourceTool("[connected input]"):GetValue("[source parameter]",[source time]

An important note is in both Blackmagic Fusion and DaVinci Resolve most expressions of this nature need a reference to a frame to pull the data from. Although this could be just frame 0 if required, the fun starts when you add time in the expression.

The above expression will look at an input to the node, self:GetSourceTool("[connected input]"): The actual node input for example “EffectMask”. This is not to be mistaken for the name of the node.

It then requires a source time in this example time=frame the node is on multiplied by 2. Expression =

self:GetSourceTool("EffectMask"):GetValue(("Width2",time2))


comp:GetPrefs( )Returns information about the current composition

comp:GetPrefs("Comp.FrameFormat.Rate") Determine Users Timeline Frame Rate

This returns the actual timeline framerate, useful for adding a variable to any other expression which requires the framerate to be calculated.

comp:GetPrefs("Comp.FrameFormat.Width") Returns the width of the Timeline resolution in DaVinci Resolve basically on a 1080p HD resolution would return 1920

comp:GetPrefs("Comp.FrameFormat.Height") Returns the height of the Timeline resolution in DaVinci Resolve basically on a 1080p HD resolution would return 1080

Actual Image width and height
I recently came across someone wanting to get the resolution of the MediaIn1 - MediaIn6 nodes rather than the resolution of the timeline

self.Input.OriginalWidth Returns the width of the input resolution in DaVinci Resolve basically a 4K video on a 1080p HD timeline would return 3840

self.Input.OriginalHeight Returns the width of the input resolution in DaVinci Resolve basically a 4K video on a 1080p HD timeline would return 2160


This next one is new to me and I am still determining its use. Currently, it seems to ensure the KEYFRAMESTRETCHER calculates the required settings based on the length of the Global in/out. After further investigation, this expression is useful when a node is set within the total length of the composition using the global in/out. For example. a comp of 149 frames could have a background node start or end between 0 - 149 by connecting a KeyFrameStretcher to this background node and using this expression would make it responsive.

KeyframeStetcher - Source End

min(Background1.GlobalOut,F1.FPS)This is looking at a background node parameter that reflects the length of the Global in/out. FPS in the expression is the timeline framerate.


Dissolve Switch

A dissolve node can be used as an effective switch between the background and foreground input. They can be linked together to create user options in the inspector.

iif((comp.RenderEnd-time)<(comp.RenderEnd/2),0,1) Switches from background to foreground inputs at the halfway point of a comp


TimeStrecher

This is a powerful node and there is an infinite amount of expressions for this node.

time<HoldFrame and time or time>HoldFrame+HoldTime and time-HoldTime or Hold_Frame Add tools in the EditControls to provide 2 sliders which contains the frames in the comp and then you can pause an animation over a period of time.

The whole tool is here so you can reverse engineer it. TimeStrecher Animation Pause Node


If-then-else

iif(Merge1.Blend > 0.5, 1, 0) To toggle a value based on a condition (e.g. an if-then-else expression), use the iif() function. This will set the input's value to 1 if Merge1.Blend is greater than 0.5 and to 0 if otherwise

Alternatively,

(Merge1.Blend > 0.5) and 1 or 0 the following Lua conditional expression also works:


Operators

Operators are used to evaluate statements. They are combined with functions to perform logical and mathematical calculations in the Number Out and Point Out tabs.

  • x + y x plus y.

  • x - y x minus y.

  • x < y 1.0 if x is less than y, otherwise 0.0.

  • x > y 1.0 if x is greater than y, otherwise 0.0.

  • !x 1.0 if x = 0, otherwise 0.0.

  • -x (0.0 - x).

  • +x (0.0 + x) (effectively does nothing).

  • x ^ y x raised to the power of y.

  • x * y x multiplied by y.

  • x / y x divided by y.

  • x % y x modulo y, (remainder of (x divided by y)).

  • x <= y 1.0 if x is less than or equal to y, otherwise 0.0.

  • x >= y 1.0 if x is greater than or equal to y, otherwise 0.0.

  • x = y 1.0 if x is exactly equal to y, otherwise 0.0.

  • x == y 1.0 if x is exactly equal to y, otherwise 0.0 (identical to above).

  • x <> y 1.0 if x is not equal to y, otherwise 0.0.

  • x != y 1.0 if x is not equal to y, otherwise 0.0 (identical to above).

  • x & y 1.0 if both x and y are not 0.0, otherwise 0.0.

  • x && y 1.0 if both x and y are not 0.0, otherwise 0.0 (identical to above).

  • x | y 1.0 if either x or y (or both) are not 0.0, otherwise 0.0.

  • x || y 1.0 if either x or y (or both) are not 0.0, otherwise 0.0 (identical to above).


Formulas

Formulas are entered into the Number Out or Point Out tabs as part of an expression. They can be made up of the following functions:

  • n1..n9 The value of Number Input 1..9.

  • p1x..p9x The X of Positional Control 1..9.

  • p1y..p9y The Y of Positional Control 1..9.

  • time The current time (frame number).

  • pi The value of pi.

  • e The value of e.

  • log(x) The base-10 log of x.

  • ln(x) The natural (base-e) log of x.

  • sin(x) The sine of x (x is degrees).

  • cos(x) The cosine of x (x is degrees).

  • tan(x) The tangent of x (x is degrees).

  • asin(x) The arcsine of x, in degrees.

  • acos(x) The arccosine of x, in degrees.

  • atan(x) The arctangent of x, in degrees.

  • atan2(x, y) The arctangent of x,y, in degrees.

  • abs(x) The absolute (positive) value of x.

  • int(x) The integer (whole) value of x.

  • frac(x) The fractional value of x.

  • sqrt(x) The Square Root of x.

  • rand(x, y) A random value between x and y.

  • rands(x, y, s) A random value between x and y, based on seed s.

  • min(x, y) The minimum (lowest) of x and y.

  • max(x, y) The maximum (highest) of x and y.

  • dist(x1, y1, x2, y2) The distance between point x1,y2 and x2,y2.

  • dist3d(x1,y1,z1,x2,y2,z2) The distance between 3D points x1,y2,z1 and x2,y2,z2

  • noise(x) A smoothly varying Perlin noise value based on x

  • noise2(x, y) A smoothly varying Perlin noise value based on x and y

  • noise3(x, y, z) A smoothly varying Perlin noise value based on x, y and z

  • if(c, x, y) Returns x if c <> 0, otherwise y.


In this next section, I can not take any credit for any of the expressions, in fact, most of these baffle me. The credit must go to Alex Matravers who put a lot of effort and time into figuring easing expressions out.

1 - cos((time/comp.RenderEnd * pi) / 2) easInSine

sin(((time/comp.RenderEnd) * pi) / 2) easeOutSine

-(cos(pi * (time/comp.RenderEnd)) - 1) / 2 easeInOutSine

(time/comp.RenderEnd) * (time/comp.RenderEnd) easeInQuad

1 - (1 - (time/comp.RenderEnd)) * (1 - (time/comp.RenderEnd)) easeOutQuad

iif((time/comp.RenderEnd) < 0.5, 2 (time/comp.RenderEnd) (time/comp.RenderEnd), 1 - pow(-2 * (time/comp.RenderEnd) + 2, 2) / 2) easeInOutQuad

(time/comp.RenderEnd)^3 easeInCubic

1-(1-time/comp.RenderEnd)^3 easeOutCubic

iif((time/comp.RenderEnd < 0.5), (((time/comp.RenderEnd)^3)4), 1-(((1-time/comp.RenderEnd)^3)4)) easeInOutCubic

(time/comp.RenderEnd) ^ 4 easeInQuart

1 - pow(1 - (time/comp.RenderEnd), 4) easeOutQuart

iif((time/comp.RenderEnd) < 0.5, 8 (time/comp.RenderEnd) (time/comp.RenderEnd) (time/comp.RenderEnd) (time/comp.RenderEnd), 1 - pow(-2 * (time/comp.RenderEnd) + 2, 4) / 2) easeInOutQuart

(time/comp.RenderEnd)^5 easeInQuint

1 - pow(1 - (time/comp.RenderEnd), 5) easeOutQuint

iif((time/comp.RenderEnd) < 0.5, 16 (time/comp.RenderEnd) (time/comp.RenderEnd) (time/comp.RenderEnd) (time/comp.RenderEnd) (time/comp.RenderEnd), 1 - pow(-2 (time/comp.RenderEnd) + 2, 5) / 2) easeInOutQuint

pow(2, 10 * (time/comp.RenderEnd) - 10) easeInExpo

1 - pow(2, -10 * (time/comp.RenderEnd)) easeOutExpo

iif((time/comp.RenderEnd) < 0.5, pow(2, 20 (time/comp.RenderEnd) - 10) / 2, (2 - pow(2, -20 (time/comp.RenderEnd) + 10)) / 2) easeInOutExpo

1 - sqrt((1 - (time/comp.RenderEnd) ^ 2)) easeInCirc

sqrt(1 - pow((time/comp.RenderEnd) - 1, 2)) easeOutCirc

iif((time/comp.RenderEnd < 0.5), (1 - sqrt((1 - (2 (time/comp.RenderEnd)) ^ 2))) / 2, (sqrt(1 - (-2 (time/comp.RenderEnd) + 2) ^ 2) + 1) / 2) easeInOutCirc

(1.70158 + 1) (time/comp.RenderEnd) (time/comp.RenderEnd) (time/comp.RenderEnd) - 1.70158 (time/comp.RenderEnd) * (time/comp.RenderEnd) easeInBack

1 + (1.70158 + 1) pow((time/comp.RenderEnd) - 1, 3) + 1.70158 pow((time/comp.RenderEnd) - 1, 2) easeOutBack

iif((time/comp.RenderEnd) < 0.5, (pow(2 (time/comp.RenderEnd), 2) ((2.594909 + 1) 2 (time/comp.RenderEnd) - 2.594909)) / 2, (pow(2 (time/comp.RenderEnd) - 2, 2) ((2.594909 + 1) ((time/comp.RenderEnd) 2 - 2) + 2.594909) + 2) / 2) easeInOutBack

Enjoy this post?

Buy John Holt a coffee

More from John Holt