## Nuke wave expressions to copy and paste

May 16, 2011 21 Comments

## Using waves to drive animation in Nuke

Creating repeating or random patterns in the curve editor can often be made easier by using expressions that describe a curve. Sine, noise and random functions are very useful, but forcing them into the exact shape you want can be a little time-consuming. So, following my previous post talking about my gizmo WaveMaker, I thought I’d share some handy expressions it uses which you can copy and paste straight from this blog when you need them, rather than installing the gizmo itself.

Edit on 12/10/2013: I have just released Animation Maker, a handy Nuke extension that contains all the waves talked about here, plus lots of Ease curves too. See the article here.

Click on any value in any node, press ‘=’ and type an expression. Right click and choose ‘Curve Editor…’ to view the curve.

These expressions give you full control over the width, height and position of the wave. Either replace the following variables with your actual values, or add your own sliders to the node so you can adjust them on the fly (see below for how to do that).

**waveLength** (how many frames between peaks of the wave)

**minVal** (the wave will never go below this value)

**maxVal** (the wave will never go above this value)

**offset** (shift the wave back and forth in time)

Edit on 15/01/12: I have also provided simple versions that will work immediately once you paste them in. They will just produce a wave between 0 and 1 but that may be all you need.

## Some useful wave expressions:

### Sine

(((sin(((frame*(pi*2/(waveLength/2))/2)+offset))+1)/2) * (maxVal-minVal) ) + minVal

Simple version: **(sin(frame/5)/2)+0.5 **(Wave between 0 and 1, replace the ‘5’ to adjust speed)

### Square

((((sin(((frame*(pi*2/(waveLength/2))/2)+offset))+1)/2) * (maxVal-minVal) ) + minVal) > ((maxVal/2)+(minVal/2)) ? maxVal : minVal

Simple version: **(sin(frame/5)/2)+0.5 > 0.5 ? 1 : 0** (Wave between 0 and 1, replace the ‘5’ to adjust speed)

### Triangle

(((((2*asin(sin(2*pi*(frame/waveLength)+offset)))/pi) / 2)+0.5) * (maxVal-minVal) ) + minVal

Simple version: **(((2*asin(sin(2*pi*(frame/30))))/pi) / 2)+0.5** (Wave between 0 and 1, replace the ’30’ to adjust speed)

### Sawtooth

((1/waveLength)*(((frame-1)+offset) % waveLength) * (maxVal-minVal) ) + minVal

Simple version: **( 1 / 30 )*( (frame-1) % 30)** (Wave between 0 and 1, replace the two ’30’s to adjust speed)

### Random

((random((frame/waveLength)+offset)) * (maxVal-minVal) ) + minVal

Simple version: **random(frame/5)** (Wave between 0 and 1, replace the ‘5’ to adjust speed)

### Noise

(((1*(noise((frame/waveLength)+offset))+1 ) /2 ) * (maxVal-minVal) ) + minVal

Simple version: **(noise(frame/5)/2)+0.5** (Wave between 0 and 1, replace the ‘5’ to adjust speed)

### Bounce

((sin(((frame/waveLength)*pi)+offset)>0?sin(((frame/waveLength)*pi)+offset):cos((((frame/waveLength)*pi)+offset)+(pi/2))) * (maxVal-minVal) ) + minVal

Simple version: **sin(((frame/10)*pi)) > 0 ? sin(((frame/10)*pi)) : cos(((frame/10)*pi)+(pi/2))** (Wave between 0 and 1, replace the ’10’s to adjust speed)

### Sawtooth (parabolic)

((sin((1/(pi/2))*(((frame-1)+offset)/(waveLength/2.46666666)) % (pi/2)))>0.99999? 1 : (sin((1/(pi/2))*(((frame-1)+offset)/(waveLength/2.46666666)) % (pi/2))) * (maxVal-minVal) ) + minVal

### Sawtooth (parabolic, reversed)

((cos((1/(pi/2))*(((frame-1)+offset)/(waveLength/2.46666666)) % (pi/2)))>0.99999? 1 : (cos((1/(pi/2))*(((frame-1)+offset)/(waveLength/2.46666666)) % (pi/2))) * (maxVal-minVal) ) + minVal

### Sawtooth (exponential)

((((((exp((1/(pi/2))*(((frame-1)+offset)/(waveLength/4.934802)) % pi*2)))/534.5)) – 0.00186741)>0.999987? 1 : (((((exp((1/(pi/2))*(((frame-1)+offset)/(waveLength/4.934802)) % pi*2)))/534.5)) – 0.00186741) * (maxVal-minVal) ) + minVal

**Note: the waveLength value doesn’t really correspond to the peaks in Random or Noise waves, so you will probably need a smaller wavelength value for those.*

## To add your own sliders:

- Right click your node, choose ‘Manage User Knobs…’
- Click ‘Add…’ and choose ‘Floating Point Slider…’
- Type ‘waveLength’ into its name and label boxes. Give it a max and min value of 0 and 100.
- Repeat with each of the four variable names (above).

These four sliders will appear in the ‘User’ tab of the node. You may find it better to create a ‘NoOp’ node, add the above sliders, but also add several extra floating-point sliders called ‘Wave1’, ‘Wave2’, ‘Wave3’ etc. This way you can use the same sliders to control several different expressions, then you can use any one of them to drive animations in your script.

It’s worth noting many of these wave shapes can be created using a few keyframes and telling nuke to ‘loop’ that section. But using expressions with a few sliders you can mess around with them a lot easier. Besides, maths is fun.

## One more thing:

To describe a **CIRCLE**, put this in the X:

(((sin(((frame*(pi*2/(waveLength/2))/2)+offset))+1)/2) * (maxVal-minVal) ) + minVal

and this in Y:

(((cos(((frame*(pi*2/(waveLength/2))/2)+offset))+1)/2) * (maxVal-minVal) ) + minVal

Simple version (X): **sin(frame/5)*300**

Simple version (Y): **cos(frame/5)*300**

(replace the ‘5’ to adjust speed, replace the ‘300’ to change the circle size)

*Tip: put it in the X and Z in a TransformGeo after a Card to make a carousel type animation.*