Sweet Python snippets

Collection of small and useful python snippets I’ve created or collected over the years.

# Toggle paint's lifetime between single/all. 
def paintLifeTimeAll():
    for i in nuke.selectedNodes():
        if i['toolbar_lifetime_type'].value() == 'all':
            i['toolbar_lifetime_type'].setValue('single')
        else:
            i['toolbar_lifetime_type'].setValue('all')

nuke.menu('Nuke').addMenu('sj_tools').addCommand('Cycle Ops/Cycle Paint Lifetime', "paintLifeTimeAll()", 'alt+q')


# Toggle paint's opacity between 1/0.1
def paintOpacity():
    for i in nuke.selectedNodes():
        if i['toolbar_opacity'].value() == 1:
            i['toolbar_opacity'].setValue(0.1)
        else:
            i['toolbar_opacity'].setValue(1)

nuke.menu('Nuke').addMenu('sj_tools').addCommand('Cycle Ops/Cycle Paint Opacity', "paintOpacity()", 'alt+1')


# Close all panels from properties.
def closeAll():
    for n in nuke.allNodes():
      n.hideControlPanel()

nuke.menu('Nuke').addMenu('sj_tools').addCommand('Close All Control Panels', lambda: closeAll(), 'ctrl+shift+d')



# Cycle B-box.
# Toggles b-box value between union and b-side. Works on all nodes with a b-box knob; Merge, Keymix, Copy etc
def bbox_B():
    for node in nuke.selectedNodes():
        if node.knob('bbox'):
            if node['bbox'].value() == "union":
                node['bbox'].setValue('B')
            else:
                node['bbox'].setValue('union')

nuke.menu('Nuke').addMenu('sj_tools').addCommand('Cycle Ops/Cycle bbox', lambda: bbox_B(), 'ctrl+B')


# Cycle Unpremult/Premult
# Toggles between premult and unpremult. Works on all nodes with a unpremult knob; Grade, Color Correct, Saturation etc
   for i in nuke.selectedNodes():
      if i.knob('unpremult'):
         if i['unpremult'].value() == 'alpha':
            i['unpremult'].setValue('none')
         else:
            i['unpremult'].setValue('alpha')

nuke.menu('Nuke').addMenu('sj_tools').addCommand('Cycle Ops/Cycle unpremult', lambda: unpremult(), 'ctrl+U')


# Cycle Alpha/RGB
# Toggles channels between alpha and rgb. Useful for green screen work or whenever else you want to grade/modify the alpha channel.
def gradeAlpha():
   for i in nuke.selectedNodes():
      if i.knob('channels'):
         if i['channels'].value()=='alpha':
            i['channels'].setValue('rgb')
         else:
            i['channels'].setValue('alpha')
            
nuke.menu('Nuke').addMenu('sj_tools').addCommand('Cycle Ops/Cycle Alpha', lambda: gradeAlpha(), 'ctrl+shift+A')


# Cycles the brush color in the paint node between r,g,b. Useful for QC/frame annotations
def paintColor():
   for i in nuke.allNodes('RotoPaint'):
      if i.knob('toolbar_paint_color'):
         print i['toolbar_paint_color'].value()
         if i['toolbar_paint_color'].value() == [1, 0, 0, 1]:
            i['toolbar_paint_color'].setValue([0, 1, 0, 1])
         elif i['toolbar_paint_color'].value() == [0, 1, 0, 1]:
            i['toolbar_paint_color'].setValue([0, 0, 1, 1])
         else:
            i['toolbar_paint_color'].setValue([1, 0, 0, 1])

nuke.menu('Nuke').addMenu('sj_tools').addCommand('Cycle Ops/Cycle PaintColor', "paintColor()", 'Ctrl+2')


# Random color on roto shapes similar to 'cycle mask colors' in After Effects.
# Will create a random hue but saturation and value will be constant, vivid and bright to avoid hard-to-see shapes  :
import random
import colorsys

def costomRotoColor():
    node = nuke.thisNode()
    cKnob= node['curves']
    rRoot = cKnob.rootLayer

    for shape in rRoot:
        attrs = shape.getAttributes()
        if attrs.getValue(1,'ro') == 0 and attrs.getValue(1,'go') == 0 and attrs.getValue(1,'bo') == 0:
            convert = colorsys.hsv_to_rgb(random.random(),1,1)
            attrs.set('ro', convert[0])
            attrs.set('go', convert[1])
            attrs.set('bo', convert[2])
            attrs.set('ao', 1)
            break

nuke.addKnobChanged(costomRotoColor, nodeClass='Roto')
Previous
Previous

sj_curveMapper

Next
Next

sj_mergeOps