Some Nuke snippets

Extract with mask

extract

def extract_with_mask():
	node = nuke.selectedNode()
	maskNode = nuke.selectedNode().input(node.optionalInput())
	nodeChild = nuke.selectedNode().dependent()
	nodeParent = nuke.selectedNode().input(0)
	
	#deconnexion
	#input
	cpt = 0
	while cpt <= node.minInputs():
		node.setInput(cpt, None)
		cpt = cpt + 1
	#output
	for n in nodeChild:
		if n.Class() in ["Merge2", "DeepRecolor", "Shuffle", "Dissolve"]:
			if n.input(0) == node:
				n.setInput(0, nodeParent)
			else:
				n.setInput(1, nodeParent)
		else:
			n.setInput(0, nodeParent)
	
	#mask connexion
	node.setInput(node.optionalInput(), maskNode)
	
	#positions
	node['xpos'].setValue(node.xpos()+15)
	node['ypos'].setValue(node.ypos()+15)
nuke.menu("Nuke").addCommand("Custom/Extract node with mask", "extract_with_mask()", "ctrl+shift+e")
extract_with_mask()

Duplicate with inputs

copyWithInputs

def duplicate_with_inputs():
	node = nuke.selectedNode()
	
	# clean selection
	for n in nuke.selectedNodes():
		n.setSelected(False)
	
	# copy
	node.setSelected(True)
	nuke.nodeCopy("%clipboard%")
	node.setSelected(False)
	
	# paste
	copyNode = nuke.nodePaste("%clipboard%")
	copyNode.setSelected(False)
	nuke.show(copyNode)
	
	# connexion
	for i in range(node.inputs()):
		copyNode.setInput(i, node.input(i))
		
	# positions
	copyNode['xpos'].setValue(node.xpos()+25)
	copyNode['ypos'].setValue(node.ypos()+25)
	
nuke.menu("Nuke").addCommand("Custom/Duplicate with inputs", "duplicate_with_inputs()", "ctrl+shift+c")
duplicate_with_inputs()

Invert Disable

invertdisable

def invertDisable():
	for n in nuke.selectedNodes():
		if n['disable'].value() == True:
			n['disable'].setValue(False)
		else:
			n['disable'].setValue(True)
nuke.menu("Nuke").addCommand("Custom/Invert disable", "invertDisable()", "shift+d")
invertDisable()

Switch $gui

gui

def switchGui():
	for n in nuke.selectedNodes():
		if n['disable'].value() == True:
			n['disable'].clearAnimated()
			n['disable'].setValue(False)
		else:
			n['disable'].setExpression('$gui')
nuke.menu("Nuke").addCommand("Custom/Switch $gui", "switchGui()")
switchGui()

Dot and hide

dothide

def dotAndHide():
	node = nuke.selectedNode()
	nodeParent = nuke.selectedNode().input(0)
	
	dotNode = nuke.nodes.Dot()
	
	
	#connexion
	dotNode.setInput(0, nodeParent)
	node.setInput(0, dotNode)
	
	#positions
	dotNode['xpos'].setValue(node.xpos()+34)
	dotNode['ypos'].setValue(node.ypos()-20)
	
	dotNode['hide_input'].setValue(1)
nuke.menu("Nuke").addCommand("Custom/Dot and Hide", "dotAndHide()", "ctrl+.")
dotAndHide()

Frame Range

def frameRange():

	settingsList = ['hold','loop','bounce','black']
	panel = nuke.Panel('Frame Range')
	panel.addSingleLineInput('first frame', int(nuke.Root().knob('first_frame').value()))
	panel.addEnumerationPulldown('before', ' '.join( settingsList ))
	panel.addSingleLineInput('last frame', int(nuke.Root().knob('last_frame').value()))
	panel.addEnumerationPulldown('after', ' '.join( settingsList ))
	panel.show()

	if panel.value('before') :
		first = int(panel.value('first frame'))
		before = panel.value('before')
		last = int(panel.value('last frame'))
		after = panel.value('after')
		for n in nuke.selectedNodes():
			if n.Class() == 'Read' or n.Class() == 'DeepRead':
				n['first'].setValue(first)
				n['origfirst'].setValue(first)
				n['before'].setValue(before)
				n['last'].setValue(last)
				n['origlast'].setValue(last)
				n['after'].setValue(after)
nuke.menu("Nuke").addCommand("Custom/Frame Range", "frameRange()", "ctrl+f")
frameRange()

Disable/Enable DOF

def disableDof():
	for s in nuke.allNodes():
		classTypes = ['OFXcom.frischluft.openfx.depthoffield_v1', 'ZBlur' , 'ZDefocus2', 'Defocus']
		for n in classTypes:
			if n in s.Class():
				s['disable'].clearAnimated()
				s['disable'].setValue(1)

def enableDof():
	for s in nuke.allNodes():
		classTypes = ['OFXcom.frischluft.openfx.depthoffield_v1', 'ZBlur' , 'ZDefocus2', 'Defocus']
		for n in classTypes:
			if n in s.Class():
				s['disable'].clearAnimated()
				s['disable'].setValue(0)
				
def dofOnRender():
	for s in nuke.allNodes():
		classTypes = ['OFXcom.frischluft.openfx.depthoffield_v1', 'ZBlur' , 'ZDefocus2', 'Defocus']
		for n in classTypes:
			if n in s.Class():
				s['disable'].setExpression('$gui')
nuke.menu("Nuke").addCommand("Custom/DOF/Disable DOF", "disableDof()")
nuke.menu("Nuke").addCommand("Custom/DOF/Enable DOF", "enableDof()")
nuke.menu("Nuke").addCommand("Custom/DOF/Only on render", "dofOnRender()")

Leave a Reply