Nuke Python Temp

From bernie's
Jump to navigation Jump to search

Switch off heavy nodes

Simple

Will toggle on/off all the nodes in the list

disableList = ['Defocus' , 'VectorBlur', 'iBlur', 'Convolve', 'MagicDefocus.gizmo']

toggle = -1
nodes = nuke.selectedNodes()
if not nodes:
    nodes = nuke.allNodes()
for n in nodes:
    if n.Class() in disableList:
        if toggle == -1:
            toggle = 1.0 if n['disable'].getValue() == 0.0 else 0.0
        n['disable'].setValue(toggle)

More Subtle

Creates a switch control linking either to disable knobs or multiplies a value by the switch node (like the Transform's 'motionblur' knob). Because how the fuck did the Foundry not add that by default ? Right now still in WIP because i'm not happy about how it still gives you an expression error when you delete the switch node. TBD

# this will create a global control switch to enable/disable 'slow' nodes
# or 'disable' knobs by multiplying them by the switch value 

slownodes = {
    'Transform':'motionblur',
    'MagicDefocus.gizmo':'disable',
    'VectorBlur2':'disable',
    'Convolve':'disable'
    }

#use the following code to get the selected nodes class types if it's not obvious
#print('\n'.join([n.name()+' > '+n.Class() for n in nuke.selectedNodes()]))

nuke.Undo.name("Add Slow Switch")
nuke.Undo.begin()

nodes = []
allSelection = nuke.selectedNodes()

for nodetype, knob in slownodes.items():
    nodes = nuke.selectedNodes(nodetype)
    if not nodes and not allSelection:
        nodes = nuke.allNodes(nodetype)
    if nodes:
        s = nuke.toNode('SlowNodesSwitch')
        if not s:
            s = nuke.nodes.Switch(name='SlowNodesSwitch',which='!disable',tile_color=16728063)
            s['disable'].setValue(1)

            selectedNodes = allSelection
            if not allSelection:
                selectedNodes = nodes
            xPos = [int(n['xpos'].value()) for n in selectedNodes]
            yPos = [int(n['ypos'].value()) for n in selectedNodes]
            bbox = [[min(xPos),max(xPos)],[min(yPos),max(yPos)]]
            s.setXYpos(bbox[0][0]-200,bbox[1][1]) # bottomleft of selected nodes?

        for node in nodes:
            print(' '+node.name()+'>')
            if knob == 'disable':
                node['disable'].setExpression('!parent.SlowNodesSwitch.which')

                #clean things up if we delete this node. not cleanest code ie it still prompts expression errors and 
                #node names can't be changed
                destroyCode = '\nnuke.toNode(\''+node.name()+'\')[\'disable\'].clearAnimated()'
                destroyCode += '\nnuke.toNode(\''+node.name()+'\')[\'disable\'].setValue(0)'
                s['onDestroy'].setValue(s['onDestroy'].value()+destroyCode)

            else:
                # multiply knob by 0 to keep value when at 1
                curKnob = node[knob]
                if not curKnob.hasExpression():
                    value = curKnob.value()
                    if value != 0:
                        curKnob.setExpression( str(value)+' * parent.SlowNodesSwitch.which')
                        destroyCode = '\nnuke.toNode(\''+node.name()+'\')[\''+ knob +'\'].clearAnimated()'
                        destroyCode += '\nnuke.toNode(\''+node.name()+'\')[\''+ knob +'\'].setValue('+str(value)+')'
                        s['onDestroy'].setValue(s['onDestroy'].value()+destroyCode)
                    else:
                        print('  Skipping {}.{}, already at 0'.format(node.name(),knob))
                else:
                    print('  Skipping {}.{}, already has an expression: {}'.format(node.name(),knob,curKnob.toScript()))
    del nodes
nuke.Undo.end()

#print('\n'.join([n.name()+' > '+n.Class() for n in nuke.selectedNodes()]))