Nuke python snippets

Useful nuke python snippets and tips I have collected through the web for a while. It’s a personal reminder to avoid reading my old scripts or search several times for the right topic of the foundry forum and our lovely nuke python reference.

TCL snippets

Smooth Animation Curve

#smooth -> float knob
translate.x.integrate(frame-smooth, frame+smooth)/(2*smooth)

Python snippets

Duplicate selected node

node = nuke.selectedNode()
for n in nuke.allNodes():
    n.setSelected(False)

newNode = nuke.createNode(node.Class(), node.writeKnobs(nuke.WRITE_NON_DEFAULT_ONLY | nuke.TO_SCRIPT), inpanel=False)

node.writeKnobs(nuke.WRITE_USER_KNOB_DEFS | nuke.WRITE_NON_DEFAULT_ONLY | nuke.TO_SCRIPT)

Avoid properties panel duplication when adding/removing knobs by python (on linux)

PySide.QtGui.QApplication.processEvents()

rgb color to hexadecimal

node = nuke.createNode('Blur')
r = 0
g = 1
b = 0
hexColour = int('%02x%02x%02x%02x' % (r*255,g*255,b*255,1),16)
node['tile_color'].setValue(hexColour)

hexadecimal color to rgb

V = 16711680
R = (255 & V >> 24) / 255.0
G = (255 & V >> 16) / 255.0
B = (255 & V >> 8) / 255.0

Overwrite FrameHold node by default with current Frame

nuke.menu('Nodes').addCommand( "Time/FrameHold", "nuke.createNode('FrameHold')['first_frame'].setValue( nuke.frame() )", icon='FrameHold.png')

Load toolset, createnode, node copy/paste

nuke.loadToolset("/path/to/file.nk")
nuke.createNode("file.nk", inpanel=True) # when available in nuke plugin path
nuke.nodePaste("/path/to/file.nk")
nuke.nodeCopy("/destination/path/file.nk") # write node(s) in nk file

Tips & Tricks

 

Setname() / name set value

By default when you create a node in Nuke, it renames itself with an increment of one if there is already another node with the same name inside the comp. This is what mimics node.setname() function. It sets the name knob and then increments if there is already another existing node with this name. Rather that node[‘name’].setValue() which sets the name knob of the node and nothing more.

Difference setXpos()/node[‘xpos’]

When we set directly the value of the xpos/ypos knob, we loose this step into the undo caching.
node.setXpos() and node.setYpos() and node.setXYpos() keep the action into the undo caching

Callbacks knobchanged & cie

“knobChanged” is an “hidden” knob which holds code executed each time that we touch any node’s knob. Thanks to that we can filter some user actions on the node and doing cool stuff like dynamically adding things inside a group.

code = """
knob = nuke.thisKnob()
if knob.name() == 'size':
  print "size : %s" % knob.value()
"""
nuke.selectedNode()["knobChanged"].setValue(code)

Unfortunately, the xy position of a node into the nodegraph, if it’s selected or not, and all other properties, which might not be linked to the node functionality itself (see callback introduction), are also stored inside a “hidden” knob. It means that we compile and execute code practically anytime. (when we open the node, when we move it, when we click on it ..)

Another solution is to use the nuke callbacks function. We can easily filter some actions depending of a node class. If we want to populate dynamically a gizmo we must keep it as a “group” class.


So, what can we do if we have several groups to populate ?

A simple and proper way is to add an hidden text knob named by example “subClass” and then easily play with our group-subclass subdivision.

  • By creating a call back function which operates on group class.
  • Looking after the presence of a “subClass” knob,
  • get the value
  • and execute the desired code associate to our subclass.
def knobChangedGroup():

    node = nuke.thisNode()
    if node.Class() == 'Group':
        if subClass(node) == "mySubClass":
        	print nuke.thisKnob().name()

def subClass(node):

    if node.knob('subClass'):
        subClass = node['subClass'].value()
        return subClass
    else:
        return None

nuke.addKnobChanged(knobChangedGroup,nodeClass='Group')