Difference between revisions of "Nuke Python Temp"

From bernie's
Jump to navigation Jump to search
(Created page with "===Switch off heavy nodes=== ====Simple==== Will toggle on/off all the nodes in the list <pre> 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) </pre> ====More Subtle==== Creat...")
 
Line 47: Line 47:
             yPos = [int(n['ypos'].value()) for n in selectedNodes]
             yPos = [int(n['ypos'].value()) for n in selectedNodes]
             bbox = [[min(xPos),max(xPos)],[min(yPos),max(yPos)]]
             bbox = [[min(xPos),max(xPos)],[min(yPos),max(yPos)]]
             s.setXYpos(bbox[0][0]-200,bbox[1][1]) # bottomleft of selected nodes
             s.setXYpos(bbox[0][0]-200,bbox[1][1]) # bottomleft of selected nodes?
            #s['onDestroy'].setValue(test)
 
         for node in nodes:
         for node in nodes:
             print(' '+node.name()+'>')
             print(' '+node.name()+'>')
             if knob == 'disable':
             if knob == 'disable':
                 node['disable'].setExpression('!parent.SlowNodesSwitch.which')
                 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:
             else:
                 # multiply knob by 0 to keep value when at 1
                 # multiply knob by 0 to keep value when at 1
Line 60: Line 67:
                     if value != 0:
                     if value != 0:
                         curKnob.setExpression( str(value)+' * parent.SlowNodesSwitch.which')
                         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:
                     else:
                         print('  Skipping {}.{}, already at 0'.format(node.name(),knob))
                         print('  Skipping {}.{}, already at 0'.format(node.name(),knob))
Line 67: Line 77:
nuke.Undo.end()
nuke.Undo.end()


#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()]))
#print('\n'.join([n.name()+' > '+n.Class() for n in nuke.selectedNodes()]))
#todo: nuke.addOnDestroy(call, args=(), kwargs={}, nodeClass='*')[source]
#s['onDestroy'].setValue('''
#print(nuke.thisNode().dependencies())
#''')
#
#nuke.selectedNodes()[0]['onDestroy'].setValue(str(test))
</pre>
</pre>

Revision as of 17:18, 30 May 2023

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 WIP because it fucks up when i delete it.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'
    }

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()

#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()]))