Expressions

Expressions & Curves

lerp, smoothstep, waves and noise for animation in Nuke

Math in Nuke, the series: Merge & Premultiplication · The Grade Node · ColorCorrect · Saturation, Hue & HSV · Keying · Despill · Matching Colour & Light · Edge Extend · Lin, Log & Gamma · Filtering & Resampling · Edge Detection · Vectors · Trigonometry · Expressions & Curves

An expression is a little formula that Nuke evaluates at every frame. With a handful of functions you can animate without keyframes: flicker a light, shake a camera, loop a sequence, fade with a nice ease, hold every other frame. This page collects the maths of animation curves, with a wave generator that writes the expression for you.



Useful Links



Index



01. frame and time offsets

frame is the current frame number, the clock of every animation expression. Any animated knob can also be read at another frame by putting the frame in brackets after its name:

Transform1.translate.x              # now
Transform1.translate.x(frame - 1)   # one frame ago: speed = now - before
Tracker1.translate.x(1001)          # always the value at frame 1001
Transform1.translate.x(frame + 50)  # the same animation, 50 frames earlier


02. Remap: lerp, clamp, fit

lerp(a, b, t) = a + (b − a) × t    (also called mix)
clamp(x, min, max)    clamp(x) = between 0 and 1
fit:  (x − inMin) / (inMax − inMin) × (outMax − outMin) + outMin

lerp goes from a to b as t goes from 0 to 1: it's a Dissolve in one line. fit (not a built-in function, but easy to write) maps any range onto another: for example a speed between 0 and 40 pixels onto a blur size between 0 and 10.



03. step and smoothstep

step(a, x) is 0 before a and 1 after: an on/off switch. smoothstep(a, b, x) goes from 0 to 1 between a and b with a soft start and a soft end (the cubic 3t² − 2t³). Use it with frame for an ease-in-out without keyframes, with a distance for a soft matte (as in the position matte), with a luminance for a soft key.

smoothstep(1001, 1024, frame)          # 0 at 1001, 1 at 1024, eased
lerp(0, 300, smoothstep(1001, 1024, frame))   # an eased move from 0 to 300
00.30.50.8100.51t, or xvaluelerp(0, 1, t)smoothstep(0.2, 0.8, x)step(0.5, x)clamp(x, 0.2, 0.8)
The remap family: lerp is a straight line, step a switch, clamp a straight line with two flat ends, smoothstep an S with flat ends.


04. Waves

Every wave has the same four ingredients: a base value, an amplitude, a wavelength (in frames) and a phase (where it starts):

base + amplitude × sin(2π × (frame + phase) / wavelength)

Replace the sine with another shape and you have the whole family: triangle, square, sawtooth and bounce are all built from fmod (the remainder, which makes things repeat) and abs. Pick one in the Lab below and it writes the expression.

012243648-101frames, one wave every 24valuesinesquaretrianglesawtooth
Four waves with the same wavelength of 24 frames. The exact expressions for each one are in the Wave Lab below.


05. random, noise, fBm

random(x, y, z) gives a value between 0 and 1 that is always the same for the same arguments: random(frame) jumps to a new value every frame (good for flicker and hard jitter), random() with no arguments changes every time it's evaluated (avoid it: it won't render the same twice).
noise(x, y, z) is smooth Perlin noise between −1 and 1: noise(frame / 10) is an organic wiggle. It is 0 when all its arguments are whole numbers, so divide or multiply frame first and give it a seed like 1.31 rather than 1.
fBm and turbulence stack several octaves of noise, from big slow movements to small fast ones: the natural choice for handheld camera shake.

Use the second argument as a seed: noise(frame / 10, 1.31) and noise(frame / 10, 2.31) are two different curves, so x and y of a shake don't move together.



06. Time tricks: hold, loop, ping-pong

floor(frame / 2) * 2                       # hold every 2 frames (animation on twos)
1001 + fmod(frame - 1001, 24)              # loop 24 frames starting at 1001
1001 + abs(fmod(frame - 1001 + 24, 48) - 24)   # ping-pong: forwards and back

Put them in the first_frame knob of a FrameHold (nodes at the bottom) or in a TimeWarp's lookup. More about time in my TimeOffset and Animation and Curves tutorials.



07. The functions, by family

The full list is in Foundry's table of maths functions (link at the top). Grouped by what they do:

familyfunctions
time and curvesframe, x, curve(f), value(f), y(f)
roundingfloor, ceil, int, trunc, rint, fmod
rangesclamp, min, max, abs, lerp, mix, step, smoothstep
powers and logspow, pow2, sqrt, exp, log, log10, ldexp, exponent, mantissa
trigonometrysin, cos, tan, asin, acos, atan, atan2, hypot, radians, degrees, pi
hyperbolicsinh, cosh, tanh
randomrandom, noise, fBm, turbulence
colourfrom_sRGB, to_sRGB, from_rec709f, to_rec709f, from_byte, to_byte
logictrue, false, ? : , &&, ||

For TCL inside expressions (the [value ...] part), see my TCL for Nuke page.



Wave Lab

Choose a shape, set base, amplitude, wavelength (frames), phase and seed: the plot shows frames 1001 to 1120 and the expression is ready to paste in any knob. The noise and random curves of the Lab have the same character as Nuke's but not exactly the same values.



Nodes to paste in Nuke

Click a box, copy it (Ctrl+C / Cmd+C) and paste it in Nuke's Node Graph (Ctrl+V / Cmd+V).

Camera shake. A Transform with noise on translate and rotate: amplitude, rotation, frequency and seed in the User tab.

Flicker. A Grade whose multiply flickers with noise.

Eased dissolve. A Dissolve that goes from A to B with smoothstep between two frames.

Animation on twos. A FrameHold that holds every N frames.

Loop. A FrameHold that loops a range of frames.



The math series

The other pages about maths in Nuke:
Merge & Premultiplication The Grade Node ColorCorrect Saturation, Hue & HSV Keying Despill Matching Colour & Light Edge Extend Lin, Log & Gamma Filtering & Resampling Edge Detection Vectors Trigonometry
And if you like this, have a look at the Matrices in Nuke series.

Leave a comment