Seeing as I’ve found very little reference for the use of python in nuke on the web I thought I’d create this post and add to it with little code snippets.
If anyone has some cool snippets to share please feel free to leave them in a comment below.
*lastupdated: 26 April 2012
delete all nodes that are not selected
s = nuke.selectedNodes() b = nuke.allNodes() for n in b: if n not in s: nuke.delete(n)
selects all dependencies (input nodes & their parents) from a selected node
a = nuke.selectedNode() nodesToSelect = [] nodesToSelect.append(a) def climb(node): # print node.name() for n in node.dependencies(): nodesToSelect.append(a) climb(n) climb(a) for x in nodesToSelect: print x.name() # x.setSelected(1)
set all Read nodes to cache locally
for a in nuke.allNodes(): if a.Class()=='Read': a['cached'].setValue(1) a['cacheLocal'].setValue(0)
print last frame of script
print nuke.root()['last_frame'].value()
create a backdrop based on selected Nodes
margin = 100 xpMax = nuke.selectedNode().xpos() xpMin = nuke.selectedNode().xpos() ypMax = nuke.selectedNode().ypos() ypMin = nuke.selectedNode().ypos() for a in nuke.selectedNodes(): if a.xpos() > xpMax: xpMax = a.xpos() if a.xpos() < xpMin: xpMin = a.xpos() if a.ypos() > ypMax: ypMax = a.ypos() if a.ypos() < ypMin: ypMin = a.ypos() bd = nuke.nodes.BackdropNode(bdwidth=(xpMax-xpMin)+margin, bdheight=(ypMax-ypMin)+margin) bd.setXpos(xpMin-margin/2) bd.setYpos(ypMin-margin/2)
disable “postage stamps” on only “Read” nodes
for a in nuke.allNodes(): if a.Class()=='Read': a['postage_stamp'].setValue(0)
disable “postage stamps” on all nodes
for a in nuke.allNodes(): try: a['postage_stamp'].setValue(0) except: pass
“unhide” all nodes’ inputs – useful when receiving a sneaky comp/lighting script
for a in nuke.allNodes(): try: a['hide_input'].setValue(0) except: pass
change the “first” frame of all selected nodes that are “Read” nodes:
(example changes the first frame to 1018)
for a in nuke.selectedNodes(): if a.Class() == 'Read': a['first'].setValue(1018)
print a selected nodes’ methods
import struct node = nuke.selectedNode() for a in node['lookup'].animations(): print dir(a)
print inputs (dependencies) of a selected node:
for a in nuke.selectedNode().dependencies(): print a.name()
print outputs (dependents) of a selected node:
for a in nuke.selectedNode().dependent(): print a.name()
find all the TimeOffset nodes in a Group called “Group2″, and change the value of each offset based on it’s position in the array of found time offsets
tos = [] for a in nuke.toNode('Group2').nodes(): if a.Class()=='TimeOffset': tos.append(a) for b in tos: b['time_offset'].setValue(tos.index(b))
set the ‘bbox’ for any selected Merge, Keymix & Copy nodes to “B”
for a in nuke.selectedNodes(): classTypes = ['Merge' , 'Keymix', 'Copy', ] for n in classTypes: if n in a.Class(): for p in a['bbox'].values(): if 'B' in p: a['bbox'].setValue(a['bbox'].values().index(p))
remove all animation from a selected nodes
for a in nuke.selectedNode().knobs(): nuke.selectedNode()[a].clearAnimated()
add keyframes – animate a mix
for a in nuke.selectedNodes(): a['mix'].setAnimated() a['mix'].setValueAt(1,nuke.frame()) a['mix'].setValueAt(0,(nuke.frame() - 1))
half the colour value of all the Constant nodes in a script
for a in nuke.allNodes(): if a.Class() == "Constant": a['color'].setValue(a['color'].value()[0] / 2 , 0) a['color'].setValue(a['color'].value()[1] / 2 , 1) a['color'].setValue(a['color'].value()[2] / 2 , 2)
find all the transform nodes in a script, and if their input is a Crop, set the ‘scale’ value to be twice it’s current value (also checks if the scale is a list/array or a float)
for a in nuke.allNodes(): if a.Class() == "Transform": if a.input(0).Class() == "Crop": x = a['scale'].value() if type(x).__name__ == 'list': a['scale'].setValue(x[0] * 2 , 0) a['scale'].setValue(x[1] * 2 , 1) if type(x).__name__ == 'float': a['scale'].setValue(x*2)
set all the gain values of all ColorCorrect nodes to be twice their current value
for a in nuke.allNodes(): if a.Class() == "ColorCorrect": a['gain'].setValue(a['gain'].value() * 2)
print files with ‘mov’ in filename
for a in nuke.allNodes(): if 'Read' in a['name'].value(): if 'mov' in a['file'].value(): print a['file'].value()
change font size of all “write” nodes in script
for a in nuke.selectedNodes(): if "Write" in a['name'].value(): a['note_font_size'].setValue(60)
create 20 constants with incrementing colour values
def makeConstants(amount): for i in range(amount): a= nuke.nodes.Constant() color= float( float(i) / float(amount) ) a['color'].setValue(color) makeConstants(20)
change the name of all Text nodes to contents the text “message”
for a in nuke.allNodes(): if a.Class()=='Text': a.setName(a['message'].value())
##Expressions
(in an expression node) – alpha channel – if the y value of the pixel is even, make it 0, else make it 1 (for viewing fields?)
y%2==1?0:1
some helpful code:
http://francoislord.com/blog/tag/python
Changing the value of a specific node in a group through python?
http://www.gfxtalk.com/forum/showthread.php?p=91701
Hey Adam, nice one – I was doing some post Sucker experimenting with Nuke+python, found this then realised it was you.
Cheers,
AC.
Thanks Chapman!
Found some cool nuke python trickery?
If you do feel free to put something up as a comment on this post!
Hope all is well back in Sydney!
How do you disable Read node thumbnails only?
Thanks.
Hey Steve sorry for the delay, am currently doing some travel in Mexico.
Try this:
for a in nuke.allNodes():
if a.Class()==’Read’:
a['postage_stamp'].setValue(0)
#Set Read Nodes Missing Frames Nearest Frame
r = nuke.selectedNodes()
for i in r:
i['on_error'].setValue(“nearest_frame”)
#Set Shuffle Nodes Write Their Incoming Channels Out
s = nuke.allNodes(‘Shuffle’)
for i in s:
i['label'].setValue(“[value in]“)
Nice one Agoston!
Thanks for your code!
Great work Adam,
being late for the party, found out your nuke forum now only.
I have a query:
How to get the paths in Read nodes and if anyone updated, how to reload that again in the scene, without reloading the whole scene
getting paths of all read nodes
for n in nuke.allNodes():
if n.Class() == ‘Read’:
print n['name'].value() + ‘ : ‘ + n['file'].value()
Nice One Anshul! Thanks for sharing!
# print all nodes with no connected output
(maybe helpful to find dead branches end in high complex comps)
for node in nuke.allNodes():
if not len(node.dependent()):
if not node.maxOutputs() == 0:
print node.name()
Looks great Marc! Thanks for sharing your code!
Can you help me please?! I’m having trouble duplicating a node through python.
I have a group node with a few nodes inside. I also have 10 read nodes. What I would like to do it is select all my read nodes and have the script duplicate the group and place input to the read.
any help would be greatly appreciated!
Thanks
Hi Adam, Great stuff!
I see you have code to select all read nodes but how do you select all read nodes above a selected node?
Hey Douglas!
Thanks for the nice words!
I had a quick look, try this (i´ll add it above):
a = nuke.selectedNode()
nodesToSelect = []
nodesToSelect.append(a)
def climb(node):
# print node.name()
nodesToSelect.append(node)
for n in node.dependencies():
climb(n)
climb(a)
for x in nodesToSelect:
print x.name()
x.setSelected(1)
another Nuke Python resource:
http://nullege.com/codes/search/nuke.ask
Hey Adam,
i’ve put some of my Nuke Snippets online:
http://ewok1.com/resources/
best regeards,
Marc
Hey Marc!
Thanks for sharing your your snippets!
Some great ones in there from what I can see.
Keep up the great work!
Cheers
Adam