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
Sine, cosine and arctangent sound like school, but in a comp they solve very practical problems: the rotation and scale of an object from two tracks, the distance between two points, something that orbits, a pendulum, a rotation in degrees that Nuke wants in radians. This page is the toolbox, with a Lab that turns two trackers into a Transform.
Useful Links
- https://learn.foundry.com/nuke/content/comp_environment/expressions/adding_math_functions.html
- https://learn.foundry.com/nuke/content/comp_environment/warping/tracking_tracker.html
- https://creativecow.net/?p=66428
- https://pluralsight.com/courses/3d-rotation-trackers-trigonometry-nuke-284
- https://en.wikipedia.org/wiki/Atan2
- https://en.wikipedia.org/wiki/Unit_circle
Index
- 01. Degrees and radians
- 02. The unit circle
- 03. Distance: hypot
- 04. Angle: atan2
- 05. Rotation and scale from two tracks
- 06. Circles, orbits and pendulums
- Two Points Lab (try it here)
- Nodes to paste in Nuke
01. Degrees and radians
A Transform's rotate knob is in degrees, but sin, cos and atan2 in expressions work in radians: a full turn is 2π (about 6.283) instead of 360. Nuke has two helpers, radians(x) and degrees(x). Forgetting one of them is the most common bug in trig expressions: if something spins like crazy, you probably passed degrees to sin().
radians = degrees × π / 180 degrees = radians × 180 / π
02. The unit circle
Walk around a circle of radius 1: at the angle θ your position is (cos θ, sin θ). That's all sin and cos are. Multiply by a radius and add a centre, and you have any circle; let θ grow with the frame and the point goes round. The tangent, sin/cos, is the slope of the line from the centre.
x = centre.x + radius × cos(θ) y = centre.y + radius × sin(θ)
03. Distance: hypot
Pythagoras: the distance between two points is the hypotenuse of the triangle made by dx and dy. Nuke has it built in:
distance = hypot(dx, dy) = √(dx² + dy²)
The distance between two tracks at the current frame, divided by the same distance at the reference frame, is how much the object has scaled.
04. Angle: atan2
The opposite question: given dx and dy, what's the angle? atan(dy/dx) works only in half of the cases (it can't tell left from right, and it breaks when dx is 0). atan2 takes the two numbers separately and gives the correct angle all the way round, from −180° to 180°.
angle = degrees(atan2(dy, dx))
atan2 takes dy first and dx second, like in C and Python. Foundry's table writes the arguments as (x, y): if your rotation comes out mirrored, swap them.
05. Rotation and scale from two tracks
Track two points on an object. At every frame the line between them tells you everything:
- translate: how much track 1 has moved from the reference frame;
- rotate: the angle of the line now, minus its angle at the reference frame;
- scale: the length of the line now, divided by its length at the reference frame;
- center: track 1 at the reference frame, so rotation and scale happen around it.
That's exactly what the Tracker does when you ask it for translate, rotate and scale with two tracks. Doing it by hand is useful when the two points come from different trackers, from Roto points or from a 3D projection, or when you want only the rotation. The TwoPoints setup at the bottom of the page does it with expressions.
06. Circles, orbits and pendulums
# a point that orbits (radius 200, 6 degrees per frame), in a Transform translate
x: 200 * cos(radians((frame - 1001) * 6))
y: 200 * sin(radians((frame - 1001) * 6))
# a pendulum: rotate swings between -20 and 20 degrees every 48 frames
20 * sin(2 * pi * (frame - 1001) / 48)
# the angle of a moving object, from its own motion (to make it face where it goes)
degrees(atan2(Transform1.translate.y - Transform1.translate.y(frame - 1), Transform1.translate.x - Transform1.translate.x(frame - 1)))
pi is written pi() in Foundry's function table; if your version doesn't accept pi, use 3.14159265359.
Two Points Lab
Drag the two yellow tracks (and, if you want, the grey reference positions). The blue and pink sides are dx and dy, the yellow side is the distance. The Transform below puts anything stuck to the reference frame on top of the tracks.
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).
Two points to Transform. A NoOp with two points and a reference frame (link p1 and p2 to your tracks with Ctrl/Cmd+drag), and a Transform that computes translate, rotate, scale and centre with hypot and atan2.
Orbit. A Transform that moves in a circle: radius and speed in the User tab.
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
Expressions & Curves
And if you like this, have a look at the Matrices in Nuke series.