## 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.

1. Right click your node, choose ‘Manage User Knobs…’
2. Click ‘Add…’ and choose ‘Floating Point Slider…’
3. Type ‘waveLength’ into its name and label boxes. Give it a max and min value of 0 and 100.
4. 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.

## WaveMaker v3

I’ve added a few more gizmos and python functions to davidemeny.com, feel free to use them as you wish.

Edit on 12/10/2013: This tool has been superceded by my new tool Animation Maker.

One of which helps you generate waves in the curve editor:

## WaveMaker v3

Animating flickering lights or continuous movement is a doddle in Nuke if you know how to generate sine waves, noise waves etc in the expression editor.

For example: a simple sine wave ranging from 0 to 1 with a wavelength of n frames would be “=sin((frame/(n/2))*pi)/2+0.5”. For the maths geeks, whipping up expressions is second nature, but for many of us it’s a hassle to remember all the various combinations when we just want a thing that goes up and down in a repetitive or random way.

WaveMaker gives you a choice of a variety of handy wave shapes, lets you put in a minimum and maximum number, gives you a ‘Speed’ slider, an ‘Offset’ slider and then you just press ‘Generate’ to write out whatever complex expression you need to make that wave. Just link the ‘Result’ value to whatever you want to control and you’re away.

It also lets you use a second, slower wave to affect the other wave. For example, you have a high frequency pulsing noise wave but you want it to fade up and down in intensity over time. Just use a slow sine wave and tick the ‘use modulator wave’.

With version 3, I have now added a ‘squarify’ tick box, which converts any wave you come up with into a square wave version. I have also added loads more wave types, including TRIANGLE and SAWTOOTH.

Have a play around, you can make some pretty weird looking waves. This isn’t a gizmo you will use all the time, but when you do, it’ll save you a lot of time writing long expressions or creating lots of keyframes.