Using math functions to increase productivity. Demonstrates use of sin and random functions, how to modify frequency and amplitude with expressions, and shows examples of procedural animation.
- [Voiceover] Hi, this is Steve Wright, welcoming you to this week's Nuke Nugget, how to use math functions to power your compositing with Nuke. Nuke has dozens of built-in math functions like sine, cosine, and square-root. You can use these functions to create animation, solve complex problems, as well as speed up your overall shot development. So math functions are really useful for doing procedural animation, that is, creating animation based on rules or mathematical expressions, not keyframe animation, such as a swinging pendulum.
So, let's open up this Transform node, which is already hooked up to my pendulum. So I need to put in some animation on this rotate that will give me a nice pendulum animation. So, we'll select that field, type "E" on the keyboard to open up the Expression, and I'm going to use a sine function, so I'll say, sine of the current frame number, okay, so here's what's happening. Every frame, this is evaluated, to find its sine.
So right now, since the play head is on frame 26, this is the sine of 26. Click OK, and there it is up there in the window. Now, as I move the play head, you can see the sine number is changing all the time, my pendulum is wiggling, and the curve editor has a nice sine wave function in it here. Now, my problem is, this is not a very nice pendulum swing, so I need to modify my math function with some wxpressions. So, let's open up this Expression and instead of using the word frame, another symbol you can use is t, for time, they're equivalent, so you can use t, it's just shorter, exactly the same thing.
So let's do this. I'd like to slow it down. So I'm gonna take the t, the time, and divide it by five. Now when I click OK, watch down here, in my curve editor, where my sine wave lives. Boom. Now the sine wave is much longer, and the animation is now much slower. Okay, so, that's good, that's what I want. But it doesn't swing very much. So I wanna increase the amplitude of my swing.
Okay, so we'll come back here, open it up. I wanna increase the amplitude, times 10. Now watch the curve, here. All right, when I click okay, it's gonna update that sine wave. Boom, there we go. Now it's going from minus 10 to plus 10. So let's play that, see how we like it. Well, that looks a little better. Now, the nice client wants it to start on frame one, absolutely dead zero.
Well, on frame one, it starts off with 1.9866, blah-blah-blah. So I need a little offset to shift the data so that I get zero on frame one. Turns out that's real easy to do. I'm gonna select this, highlight, copy, with command or control-C. So I've captured this number right here, okay. Let's open up our expression, and now, I'm going to subtract that value from my original. Click OK.
And I've got zero. So on frame one now, I'm starting with zero degrees of rotation. And then we'll play. There we go, very nice. Let's go have some fun with a random number generator. Back to the Node Graph. Let's switch over to this Transform node, on this checkerboard. I'm gonna clear the property bin and open up this Transform node. What I want, is I'd like to jiggle this guy up and down, like maybe I'm trying to do a camera shake or something.
So I'll come to the Transform y-parameter, open up the Expression, and I'll say, "random paren frame paren." Okay, so this says, give me a random number based on the current frame number. Actually, I should use my newfound letter t, just 'cause it's shorter, save some typing. So, on the current time. So, if we say OK to that, and then we open up the curve editor, there is my random numbers, down here, for 100 frames.
Now, the deal is, that they are ranging between zero and one. So this thing is gonna just jiggle up and down between zero and one pixels, which is not very exciting. Let's play that, and see what we get. There it is, I think you can see, there's just a little bit of jitter there, okay. But we need to massage this, and make a much better shot. All right, so here's what we're gonna do. We're gonna open this guy up and start modifying this parameter. Again, I wanna slow it down, so I'm gonna take my t and divide it by three, so this'll be three times slower.
Watch my curve down here when I click OK. Boom, you see, it's spread it out, now it's much slower. We'll play that, then you can see that, barely, but the amplitude is not very much fun. So, let's jack it up. So we'll go back here, modify it, and I'm gonna increase it by 20 times, so now I'm getting like 15 pixels on this guy. Okay, now watch it here.
I'm gonna zoom out. My added scale parameter is gonna change the data. Instead of ranging zero to one, it'll now range zero to 20, so, click OK, boom. There we go, zero to 20. Okay, so, let's play that, and okay, well, that looks pretty good. Lemme re-home the Viewer here. Now, the problem is, my motion is only positive. It only wiggles up. I want it to wiggle up and down.
So, to do that, I'm gonna need to change my data. Instead of changing zero to 20, it's gonna need to go from minus 10 to plus 10. So, let's modify it one more time, by saying, I wanna subtract 10 from it. Click OK, watch the curve editor. Boom, there we go. Minus 10 to plus 10. And now, the animation occurs exactly in the range that the nice client wants.
Very nice. Okay, one more example. Let's take a look at using math expressions for color correction. So, I'm gonna clear my property bin. Okay, I'll open up this Grade Node. Now, you should be warned that the Nuke variables are rather unusual here. If I put my cursor in this window here, you see it says the Nuke variable for this window is multiply. And down here, it's gamma.
That kind of makes sense. But, offset is called add. So if I wanna talk to this, I'm gonna have to use add. And if I wanna talk to gain, I'm going to use white. Okay, so white is the variable for gain. Furthermore, let me open this up for you here, we actually have four parameters: red, green, blue, and alpha. So in fact, this is white.r, white.g, white.blue and white.a, so that's how we talk to these.
All right, so let's start by doing this. I'm going to put in an expression that says, I want the green value to be equal to the red gain, which will be white.r. Okay, so I've put that right here. Now, as I move the red value, the green value is locked to it. That makes perfect sense. And I could've actually done that by command-click and drag-and-drop, to make a link, but I typed in the link myself because I wanna play some games for you.
Here's my games. I'm gonna open this guy up, and I'm going to use a big math function called square root. So I want the green to be the square root of the red. And the red parameter is white.r. Okay, click OK. So now, when I move the red, I'm getting the square root of red in the green channel. I dunno what you do with that, but that's really neat.
Just to confirm the math, I'm gonna put two up here in the red, so the green will now be the square root of two, which is 1.414214, so there we go. These examples are simply illustrative of the types of operations you can do with math functions. How to use them in your composites will obviously depend on the types of compositing problems you have to deal with. So there you have it, how to use Nuke's math functions to power up your Nuke composites. Be sure to check in next week so you don't miss out on my next Nuke Nugget.
What kinds of NUKE tips do you need? Join the Steve Wright's NUKE NUGGETS group on LinkedIn, and post your requests there.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.