AnimationCurve and AnimationKey objects
AnimationCurve Objects
AnimationCurve objects contain the animation data of a given animated knob, and can be accessed using a knob’s .animation() or .animations() methods.NOTE: AnimationCurves can be created manually using Python, but for the purposes of this article, we will assume they all come from pre-animated knobs.
1 |
g1 = nuke.toNode("Grade1") |
Here is the curve in question, as shown in Nuke's curve editor:
Like nodes and knobs, these objects have their own set of methods for interpreting and modifying the data they contain.
One of the simplest (and most useful) of these is the .evaluate() method. This simply takes a numeric frame argument (int or float) and returns the curve’s value at that frame. This can be used to perform manual subframe sampling, since non-integer frame values are perfectly legal.
9 |
anim.evaluate(30)
|
11 |
anim.evaluate(28.75)
|

Another basic yet useful method is .setKey(). It takes two arguments: a frame number and a key value, and adds an animation key at the specified frame with (you guessed it) the specified value.
13 |
anim.setKey(48, .044)
|
And here is the modified curve:
This method can also be used to modify an existing key. If a key exists at the frame specified, its value will simply be changed to the new input value.
Just like knobs, AnimationCurves can also be tested to see if they contain expressions, or whether the knob they are attached to is being driven by a set of keys. However, contrary to a knob’s .hasExpression() method, AnimationCurve objects use the method .noExpression(). This returns True if the animation is driven by keys, or False if the animation is linked by an expression.
15 |
anim.noExpression()
|

You can also manually attach an expression to an AnimationCurve, just like you would a knob, using the .setExpression() method. The argument is a string in Nuke expression syntax.
17 |
gWhite = g1['whitepoint'] |
The .expression() method can be used to retrieve the expression that is providing the animation as a string. An important note is that if the parent knob isn’t linked using an expression, this method will still return the value ‘curve.’ Otherwise, the expression will be returned in the familiar Nuke syntax.
25 |
anim.expression()
|

27 |
anim2.expression()
|
To retrieve all the keys contained in an AnimationCurve, use the .keys() method. This returns a list of AnimationKey objects; however, if the AnimationCurve is expression-driven, it will return an empty list.
29 |
keys = anim.keys() |
Similar to .setKey(), the .addKey() method can also be used to add keys to an animation curve. However, instead of simple numeric arguments, this method takes a sequence (list or tuple) of AnimationKey objects, and simply drops them into the curve. Since these AnimationKey objects contain their own x and y values (i.e. they know where to place themselves in time and space), no additional arguments are needed.
33 |
anim2.addKey(keys)
|
These AnimationKey objects are covered in more detail below.
Other useful AnimationCurve methods
35 |
anim.knob()
|
37 |
anim.knobAndFieldName()
|
39 |
anim.knobIndex()
|
41 |
anim.selected()
|
43 |
anim.size()
|
45 |
anim.constant()
|
47 |
anim.clear()
|
AnimationKey Objects
These are returned by an AnimationCurve object’s .keys() method, as described above. They are a simpler type, however, so rather than a list of methods for interacting with or modifying them, they simply contain a set of values that describe how a curve passing through them should behave.NOTE: AnimationKeys can also be created manually using Python, but for the purposes of this article, we will assume they all come from AnimationCurve objects.
49 |
key = keys[1]
|
The simplest of these values are pretty much what you would expect: .x and .y. These define when and where the key exists, respectively, and are Read/Write, meaning you can either access the values by name, or set them equal to a new value.
51 |
key.x
|

53 |
key.y
|

55 |
key.y = -.05
|
Since these key objects typically exist inside an AnimationCurve, any changes to the key will modify the parent curve directly.
The .selected value returns a boolean indicating whether or not the key is selected in the curve editor. This can also be set to a boolean to select or deselect the key, and can be useful for performing operations on a user-defined set of keys.
57 |
key.selected
|
59 |
key.selected = True
|
Each key also contains a .interpolation value, which determines how to calculate the curve’s slope between the key and another key. This can be assigned to any of a list of predefined interpolation constants, or their corresponding integers, from the following list:
nuke.SMOOTH – 0
nuke.CONSTANT – 1
nuke.LINEAR – 3
nuke.CUBIC – 4
When accessed directly, it will return only the integer. A return value other than those in the list about indicates the interpolation has been modified by the user.
61 |
key.interpolation
|
63 |
key.interpolation = nuke.CONSTANT
|
More advanced control of the key’s influence on the curve can be gained using the .la and .ra values, which define the key’s left and right bicubic values, and the .lslope and .rslope values, which define the curve’s derivative immediately to the left and right of the key. However, these will not be covered in detail here.
And always remember: the dir() and help() functions are your best friends when venturing into new Python territory in Nuke.
Comments
Great tutorial, you mentioned at the start "AnimationCurve s can be created manually using Python" I was just wondering how you would do that.
g = nuke.nodes.Grade()
a = nuke.AnimationC urve(g['blackpo int'], 0, 'r')
a.setKey(1, .1)
a.setKey(10, .125)
And so on...
Note that even though you create the AnimationCurve object with an existing knob as an argument, it isn't actually attached to that knob; rather, as near as I can tell, Nuke uses the knob's type to set or test the validity of some properties. Once you have your curve object, however, attaching it to a knob is trivial:
g['blackpoint'].copyAnimation(0, a)
Hope this helps.
RSS feed for comments to this post