Wednesday, April 11, 2012

some python snippets for nuke

1. To change selected nodes Values.

for i in nuke.selectedNodes() :

[what is happening when i run this code in script editor inside nuke?

Most compositors and learners (scripting) question is above one. Here is the answer. When you run this into your nuke script editor it will doing some repeated work. Example if you selecting 10 Blur nodes and run this it will assign the name of i to the 10 Blur nodes and it will start to change the 'size' as 0 - 20 one by one.]

2. Hide all nodes input.

for i in nuke.allNodes() :

(for active back use 0 on setValue)

3. Hide selected nodes input.

for i in nuke.selectedNodes() :

4. For lock all connections.


5. To set all read node start to custom frame.

for a in nuke.allNodes():
    if a.Class() == 'Read':

(here i use 1 frame as start frame. If you want to start your read from '1001' use "a['frame'].setValue('1001'))

6. All read nodes missing frames to 'checkerboard'.

for s in nuke.allNodes('Read'):
7. set all roto nodes output as only alpha.

for s in nuke.allNodes('Roto'):

if you want to output as 'rgba' try

for s in nuke.allNodes('Roto'):

8. For roto nodes default output alpha only.

nuke.knobDefault('Roto.output', 'alpha')
( add this line into your )
9. For temporarily disable heavy nodes like - Defocus, VectorBlur, Convolve, oflow, TVIscale.

for s in nuke.allNodes():
    classTypes = ['Defocus' , 'VectorBlur', 'Convolve', 'oflow', 'TVIscale', ]
    for n in classTypes:
        if n in s.Class():

( While working with bigger shot with lot off layers. Compositing tree became very bigger and heavy. In those kind of situvation we need to disable some heavy processing nodes for quicker preview. Use this script for nuke automatically find those nodes and disable those nodes )

10. Expand selected Groups.

for a in nuke.selectedNodes():

For expand all group nodes in node graph.

for s in nuke.allNodes('Group'):

11.For getting sys current Date.  Like ( 30-04-2012 )

[clock format [clock seconds] -format {%d-%m-%Y}]

12. For blur one channel in Nuke.

b = nuke.createNode('Blur')

blur a layer:

b = nuke.createNode('Blur')

13.To set 'Merge' nodes bounding box from union to 'B'.

To selected nodes only:
for s in nuke.selectedNodes("Merge2"):

To all 'Merge' nodes:
for s in nuke.allNodes("Merge2"):

14. To disable extra channels output from 'ScanlineRender'.

By default nukes 'ScanlineRender' nodes render some extra channels rather then 'rgba'. If you have many 'ScanlineRender' node in your script with the default setting, nuke will slow down. To get rid of that extra channel use following simple python script.

Example images:

for s in nuke.selectedNodes("ScanlineRender"):

15. Print all read nodes file path with node name.

for s in nuke.allNodes('Read'):
    print s['name'].value() + ' - ' + s['file'].value()

16. Set transform node's pivot point to center automatically:

for a in nuke.selectedNodes():
a['center'].setExpression('[value name].bbox.r+([value name].bbox.x-[value name].bbox.r)/2', 0)
a['center'].setExpression('[value name].bbox.y+([value name].bbox.t-[value name].bbox.y)/2', 1)

17. Auto crop based on input bbox.

s = nuke.selectedNode()
s['box'].setExpression('[value name].bbox.x', 0)
s['box'].setExpression('[value name].bbox.y', 1)
s['box'].setExpression('[value name].bbox.r', 2)
s['box'].setExpression('[value name].bbox.t', 3)

18. Automatically place the cornerPin nodes pin's.
selNode = nuke.selectedNode()
selNode['from1'].setExpression('[value name].bbox.x', 0)
selNode['from1'].setExpression('[value name].bbox.y', 1)
selNode['from2'].setExpression('[value name].bbox.r', 0)
selNode['from2'].setExpression('[value name].bbox.y', 1)
selNode['from3'].setExpression('[value name].bbox.r', 0)
selNode['from3'].setExpression('[value name].bbox.t', 1)
selNode['from4'].setExpression('[value name].bbox.x', 0)
selNode['from4'].setExpression('[value name].bbox.t', 1)
## copying value's from
selNode['to1'].setExpression('[value name].bbox.x', 0)
selNode['to1'].setExpression('[value name].bbox.y', 1)
selNode['to2'].setExpression('[value name].bbox.r', 0)
selNode['to2'].setExpression('[value name].bbox.y', 1)
selNode['to3'].setExpression('[value name].bbox.r', 0)
selNode['to3'].setExpression('[value name].bbox.t', 1)
selNode['to4'].setExpression('[value name].bbox.x', 0)
selNode['to4'].setExpression('[value name].bbox.t', 1)
19. To change the channel to alpha only to all the blur node which is  connected with roto nodes.

for sel in nuke.allNodes('Roto'):
    for each in sel.dependent():
        if each.Class()=="Blur":

scale up and down cornerpin node

global scale up and down cornerpin This script help you to global scale up or down the selected cornerpin. Recently i had situation to ...