Difference between revisions of "Nuke Python"

From Antoine Vienne | Wiki
Jump to navigation Jump to search
(Maxwell Render - Check .mxi sequence Sampling levels)
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=Nuke Python Scripts=
 
=Nuke Python Scripts=
 +
 +
=== Getting advanced settings/Knobs informations ===
 +
<pre>
 +
#For a specific knob, here for "mov32_settings" in a Write node
 +
i = nuke.selectedNode()
 +
print i.knob('mov32_settings').value()
 +
 +
#Or for the whole node - https://berniebernie.fr/wiki/
 +
print(nuke.selectedNode())
 +
 +
</pre>
 +
 +
=== Create multiple Writes nodes for multiples Reads - With RGBA channels activated ===
 +
<pre>
 +
//Select Read nodes first
 +
 +
root_frame_path = "/YOUR_PATH"
 +
 +
 +
nodes = nuke.selectedNodes()
 +
 +
 +
for n in nodes:
 +
 +
    roto_name = n.name()
 +
 +
    n.setSelected(True)
 +
 +
    w = nuke.createNode( "Write" )
 +
 +
    write_name = roto_name.replace("Roto", "Write_")
 +
 +
    w.knob("name").setValue(write_name)
 +
 +
    image_path = "{root_frame_path}/{write_name}.png".format(root_frame_path=root_frame_path, write_name=write_name)
 +
 +
    w.knob("file").setValue(image_path)
 +
 +
for a in nuke.allNodes('Write'):
 +
 +
    a['channels'].setValue('rgba')
 +
 +
</pre>
 +
 +
=== Change specific attribute on all nodes ===
 +
<pre>
 +
//Works also with "selectedNodes"
 +
//"render_mode" and "textured+wireframe" are examples of attributes to be changed.
 +
 +
sn = nuke.allNodes()
 +
 +
for n in sn:
 +
 +
        n.knob('render_mode').setValue("textured+wireframe")
 +
 +
 +
//Alternative script for selectedNodes:
 +
 +
for a in nuke.selectedNodes('Write'):
 +
 +
a['channels'].setValue('rgba')
 +
 +
 +
//Alternative script for allNodes:
 +
 +
for a in nuke.allNodes('Write'):
 +
 +
a['channels'].setValue('rgba')
 +
 +
</pre>
 +
 +
=== Set localizationPolicy off on all Read nodes ===
 +
<pre>
 +
sn = nuke.allNodes()
 +
 +
for n in sn:
 +
 +
        n.knob('localizationPolicy').setValue("off")
 +
 +
</pre>
 +
 +
 +
=== Set value of BBox parameter in Merge nodes to B ===
 +
<pre>
 +
for n in nuke.allNodes('Merge2'):
 +
 +
    n['bbox'].setValue("B")
 +
 +
</pre>
 +
 
=== Count selected nodes ===
 
=== Count selected nodes ===
 
<pre>
 
<pre>
Line 6: Line 96:
 
</pre>
 
</pre>
  
=== Maxwell Render - Check .mxi sequence Sampling levels ===
+
=== Export multiple files in DXV ===
 +
<pre>
 +
#This script comes from the great Benjamin Vedrenne. DXV is the codec made by and for Resolume.
 +
#Select all Read nodes or sources first.
 +
#This script create a write node under each selected read nodes. Easy for fast and massive file conversion TO VIDEO.
 +
 
 +
for oReadNode in nuke.selectedNodes():
 +
 
 +
    oPath =  oReadNode['file'].value()
 +
    first =  oReadNode['first'].value()
 +
    last =  oReadNode['last'].value()
 +
    newPath = oPath.split('/')[-2]
 +
    oReadNode['selected'].setValue(True)
 +
    Write = nuke.createNode ("Write") 
 +
 
 +
    #Specify file path here
 +
    osdir = 'C:/DXV/tmp'
 +
   
 +
    Write.knob("beforeRender").setValue("import os\nif not os.path.isdir(os.path.dirname(nuke.thisNode()['file'].evaluate())):\n  os.makedirs(os.path.dirname(nuke.thisNode()['file'].evaluate()))")
 +
    Write.knob('channels').setValue('rgba')
 +
    Write.knob('file').setValue(osdir+'/'+newPath+".mov")
 +
    Write.knob('file_type').setValue('mov')
 +
    Write.knob('meta_codec').setValue('DXD3')
 +
    Write.knob('mov32_fps').setValue(24)
 +
    Write.knob('use_limit').setValue(True)
 +
    Write.knob('first').setValue(first)
 +
    Write.knob('last').setValue(last)
 +
    Write.knob('mov32_settings').setValue('000000000000000000000000000001b67365616e000000010000000100000000000001a276696465000000010000000f00000000000000227370746c00000001000000000000000044584433000000000020000003ff000000207470726c000000010000000000000000000000000018000000000000000000246472617400000001000000000000000000000000000000530000010000000100000000156d70736f00000001000000000000000000000000186d66726100000001000000000000000000000000000000187073667200000001000000000000000000000000000000156266726100000001000000000000000000000000166d70657300000001000000000000000000000000002868617264000000010000000000000000000000000000000000000000000000000000000000000016656e647300000001000000000000000000000000001663666c67000000010000000000000000004400000018636d667200000001000000000000000052534c4d00000014636c75740000000100000000000000000000001c6364656300000001000000000000000043736552030000000000001c766572730000000100000000000000000003001c00010000')
 +
 
 +
</pre>
 +
 
 +
=== Reformat selected Read nodes and Write it ===
 +
<pre>
 +
#Select Read nodes and put the right path below
 +
 
 +
for oReadNode in nuke.selectedNodes():
 +
 
 +
    oPath =  oReadNode['file'].value()
 +
    first =  oReadNode['first'].value()
 +
    last =  oReadNode['last'].value()
 +
    newPath = oPath.split('/')[-1]
 +
    oReadNode['selected'].setValue(True)
 +
    Reformat = nuke.createNode ("Reformat")
 +
    Reformat.knob('format').setValue('square_1K')
 +
    Write = nuke.createNode ("Write") 
 +
 
 +
    #Specify file path here
 +
    osdir = 'C:/YOUR_PATH'
 +
   
 +
    Write.knob("beforeRender").setValue("import os\nif not os.path.isdir(os.path.dirname(nuke.thisNode()['file'].evaluate())):\n  os.makedirs(os.path.dirname(nuke.thisNode()['file'].evaluate()))")
 +
    Write.knob('channels').setValue('rgb')
 +
    Write.knob('file').setValue(osdir+'/'+newPath)
 +
    Write.knob('file_type').setValue('jpeg')
 +
    Write.knob('_jpeg_quality').setValue(1)
 +
    Write.knob('use_limit').setValue(True)
 +
    Write.knob('first').setValue(first)
 +
    Write.knob('last').setValue(last)
 +
 
 +
</pre>
 +
 
 +
 
 +
=== Start selected Read nodes at frame 1 ===
 +
<pre>
 +
for n in nuke.selectedNodes('Read'):
 +
 
 +
    n['frame_mode'].setValue("1")
 +
    n['frame'].setValue("1")
 +
 
 +
</pre>
 +
 
 +
 
 +
=== DPX Writes in sRGB (D60 sim.) from Read nodes ===
 +
<pre>
 +
for oReadNode in nuke.selectedNodes():
 +
 
 +
    oPath =  oReadNode['file'].value()
 +
    first =  oReadNode['first'].value()
 +
    last =  oReadNode['last'].value()
 +
    newPath = oPath.split('/')[-1]
 +
    oReadNode['selected'].setValue(True)
 +
 
 +
    Write = nuke.createNode ("Write") 
 +
 
 +
    #Specify file path here
 +
    osdir ='C:/YOUR_PATH'
 +
   
 +
    Write.knob('channels').setValue('rgb')
 +
    Write.knob('colorspace').setValue('83)')
 +
    Write.knob('file').setValue(osdir+'/'+newPath)
 +
    Write.knob('file_type').setValue('dpx')
 +
    Write.knob('use_limit').setValue(True)
 +
    Write.knob('first').setValue(first)
 +
    Write.knob('last').setValue(last)
 +
 
 +
</pre>
 +
 
 +
=== Set Create directories ON on all Write nodes ===
 +
<pre>
 +
for n in nuke.allNodes('Write'):
 +
 
 +
    n['create_directories'].setValue(True)
 +
 
 +
</pre>
 +
 
 +
=== DPX Writes from Read nodes - Starting at frame 1 ===
 +
<pre>
 +
for oReadNode in nuke.selectedNodes():
 +
 
 +
    oPath =  oReadNode['file'].value()
 +
    first =  oReadNode['first'].value()
 +
    last =  oReadNode['last'].value()
 +
    newPath = oPath.split('/')[-1]
 +
    oReadNode['selected'].setValue(True)
 +
 
 +
    Write = nuke.createNode ("Write") 
 +
 
 +
    #Specify file path here
 +
    osdir ='C:/YOUR_PATH'
 +
   
 +
    Write.knob('channels').setValue('rgb')
 +
    Write.knob('colorspace').setValue('0)')
 +
    Write.knob('file').setValue(osdir+'/'+newPath)
 +
    Write.knob('file_type').setValue('dpx')
 +
    Write.knob('use_limit').setValue(True)
 +
    Write.knob('first').setValue(1)
 +
    Write.knob('last').setValue(last-first+1)
 +
    Write.knob('create_directories').setValue(True)
 +
 
 +
</pre>
 +
 
 +
=== JPG Writes from Read nodes - With LUT transform ===
 +
<pre>
 +
for oReadNode in nuke.selectedNodes():
 +
 
 +
    oPath =  oReadNode['file'].value()
 +
    first =  oReadNode['first'].value()
 +
    last =  oReadNode['last'].value()
 +
    newPath = oPath.split('/')[-2]
 +
    oReadNode['selected'].setValue(True)
 +
    OCIOFileTransform = nuke.createNode ("OCIOFileTransform")
 +
    OCIOFileTransform.knob('working_space').setValue('sRGB')
 +
 
 +
    #Specify lut path here
 +
    lutpath = 'C:/YOUR_PATH/YOUR_LUT.3dl'
 +
 
 +
    OCIOFileTransform.knob('file').setValue(lutpath)
 +
    Write = nuke.createNode ("Write") 
 +
 
 +
    #Specify file path here
 +
    osdir = 'C:/YOUR_PATH'
 +
   
 +
    Write.knob("beforeRender").setValue("import os\nif not os.path.isdir(os.path.dirname(nuke.thisNode()['file'].evaluate())):\n  os.makedirs(os.path.dirname(nuke.thisNode()['file'].evaluate()))")
 +
    Write.knob('channels').setValue('rgb')
 +
    Write.knob('file').setValue(osdir+'/'+newPath+'/'+newPath+".####.jpg")
 +
    Write.knob('file_type').setValue('jpeg')
 +
    Write.knob('_jpeg_quality').setValue(1)
 +
    Write.knob('use_limit').setValue(True)
 +
    Write.knob('first').setValue(first)
 +
    Write.knob('last').setValue(last)
 +
 
 +
</pre>
 +
 
 +
=== EXR Writes from Read nodes - Out Rec 709 to AcesCG ===
 
<pre>
 
<pre>
//This script comes from the great Marc Dubrois.
+
for oReadNode in nuke.selectedNodes():
 +
 
 +
    oReadNode['colorspace'].setValue('out_rec709')
 +
    oPath =  oReadNode['file'].value()
 +
    first =  oReadNode['first'].value()
 +
    last =  oReadNode['last'].value()
 +
    newPath = oPath.split('/')[-2]
 +
    oReadNode['selected'].setValue(True)
 +
 
 +
    Write = nuke.createNode ("Write")     
 +
    Write.knob("beforeRender").setValue("import os\nif not os.path.isdir(os.path.dirname(nuke.thisNode()['file'].evaluate())):\n  os.makedirs(os.path.dirname(nuke.thisNode()['file'].evaluate()))")
 +
    Write.knob('channels').setValue('rgb')
 +
    oPath = oPath.replace('.%04d.png', '_ACEScg.%04d.exr')
 +
    oPath = oPath.replace('.1001.png', '_ACEScg.1001.exr')
 +
    Write.knob('file').setValue(oPath)
 +
    Write.knob('colorspace').setValue('ACES - ACEScg')
 +
    Write.knob('file_type').setValue('exr')
 +
    Write.knob('use_limit').setValue(True)
 +
    Write.knob('first').setValue(first)
 +
    Write.knob('last').setValue(last)
  
def checkMXI_SL():
+
</pre>
    '''takes mxi read node and checks its SLs metadata and outputs frames under certain treshold'''
 
  
    sl_threshold = 18.0
+
=== All Read nodes set on nearest frame ===
    txt = nuke.getInput('Print frames with SL below:', str(sl_threshold))
+
<pre>
    if txt:
+
for n in nuke.allNodes('Read'):
        sl_threshold = float(txt)
+
 
     output = ''
+
     n['on_error'].setValue(3)
 +
</pre>
  
    task = nuke.ProgressTask("Checking MXI SLs")
+
=== Same frame on all FrameHolds ===
    task.setMessage("Progress")
+
<pre>
 +
for n in nuke.allNodes('FrameHold'):
  
     for node in nuke.selectedNodes():
+
     n['first_frame'].setValue(993)
        if node.Class() == 'Read':
+
</pre>
            firstframe = int(node['first'].value())
 
            #lastframe = firstframe + 10
 
            lastframe = int(node['last'].value())
 
            for i in range(firstframe, lastframe):
 
                percentage = int(100.0*(i-firstframe)/(lastframe-firstframe))
 
                task.setProgress(percentage)
 
                if task.isCancelled():
 
                    break;
 
  
                sl = node.metadata('SAMPLING_LEVEL',i)
+
=== All Reformats on Cubic ===
                if  sl < sl_threshold:
+
<pre>
                        task.setMessage('Frame ['+str(i)+'] SL'+"{0:.1f}".format(sl))                       
+
for n in nuke.allNodes('Reformat'):
                        logline = 'Frame ' + str(i) + ': ' + "{0:.2f}".format(sl) + '\n'
 
                        output = output + logline
 
            task.setProgress(100)
 
    del task
 
  
     p = nuke.Panel('Results')
+
     n['filter'].setValue(1)
    if output == '':
+
</pre>
        output = 'All MXI are above or equal to '+str(sl_threshold)
 
    p.addNotepad('Results:',output)
 
    ret = p.show()
 
  
checkMXI_SL()
+
=== Enable all Read nodes ===
 +
<pre>
 +
for node in nuke.allNodes('Read'):
 +
    nuke.toNode(node.name()).knob('disable').setValue(False)
 +
</pre>
  
 +
=== Executing button on selected nodes ===
 +
<pre>
 +
sn = nuke.selectedNodes()
 +
for n in sn:
 +
    n.knob('myKnob').execute()
 
</pre>
 
</pre>

Latest revision as of 19:29, 2 August 2024

Nuke Python Scripts

Getting advanced settings/Knobs informations

#For a specific knob, here for "mov32_settings" in a Write node
i = nuke.selectedNode()
print i.knob('mov32_settings').value()

#Or for the whole node - https://berniebernie.fr/wiki/
print(nuke.selectedNode())

Create multiple Writes nodes for multiples Reads - With RGBA channels activated

//Select Read nodes first

root_frame_path = "/YOUR_PATH"


nodes = nuke.selectedNodes()


for n in nodes:

    roto_name = n.name()

    n.setSelected(True)

    w = nuke.createNode( "Write" )

    write_name = roto_name.replace("Roto", "Write_")

    w.knob("name").setValue(write_name)

    image_path = "{root_frame_path}/{write_name}.png".format(root_frame_path=root_frame_path, write_name=write_name)

    w.knob("file").setValue(image_path)

for a in nuke.allNodes('Write'):

    a['channels'].setValue('rgba')

Change specific attribute on all nodes

//Works also with "selectedNodes"
//"render_mode" and "textured+wireframe" are examples of attributes to be changed.

sn = nuke.allNodes()

for n in sn:

        n.knob('render_mode').setValue("textured+wireframe")


//Alternative script for selectedNodes:

for a in nuke.selectedNodes('Write'):

a['channels'].setValue('rgba')


//Alternative script for allNodes:

for a in nuke.allNodes('Write'):

a['channels'].setValue('rgba')

Set localizationPolicy off on all Read nodes

sn = nuke.allNodes()

for n in sn:

        n.knob('localizationPolicy').setValue("off")


Set value of BBox parameter in Merge nodes to B

for n in nuke.allNodes('Merge2'):

    n['bbox'].setValue("B")

Count selected nodes

len(nuke.selectedNodes())

Export multiple files in DXV

#This script comes from the great Benjamin Vedrenne. DXV is the codec made by and for Resolume.
#Select all Read nodes or sources first.
#This script create a write node under each selected read nodes. Easy for fast and massive file conversion TO VIDEO.

for oReadNode in nuke.selectedNodes():

    oPath =  oReadNode['file'].value()
    first =  oReadNode['first'].value()
    last =  oReadNode['last'].value()
    newPath = oPath.split('/')[-2]
    oReadNode['selected'].setValue(True)
    Write = nuke.createNode ("Write")   

    #Specify file path here
    osdir = 'C:/DXV/tmp'
    
    Write.knob("beforeRender").setValue("import os\nif not os.path.isdir(os.path.dirname(nuke.thisNode()['file'].evaluate())):\n  os.makedirs(os.path.dirname(nuke.thisNode()['file'].evaluate()))")
    Write.knob('channels').setValue('rgba')
    Write.knob('file').setValue(osdir+'/'+newPath+".mov")
    Write.knob('file_type').setValue('mov')
    Write.knob('meta_codec').setValue('DXD3')
    Write.knob('mov32_fps').setValue(24)
    Write.knob('use_limit').setValue(True)
    Write.knob('first').setValue(first)
    Write.knob('last').setValue(last)
    Write.knob('mov32_settings').setValue('000000000000000000000000000001b67365616e000000010000000100000000000001a276696465000000010000000f00000000000000227370746c00000001000000000000000044584433000000000020000003ff000000207470726c000000010000000000000000000000000018000000000000000000246472617400000001000000000000000000000000000000530000010000000100000000156d70736f00000001000000000000000000000000186d66726100000001000000000000000000000000000000187073667200000001000000000000000000000000000000156266726100000001000000000000000000000000166d70657300000001000000000000000000000000002868617264000000010000000000000000000000000000000000000000000000000000000000000016656e647300000001000000000000000000000000001663666c67000000010000000000000000004400000018636d667200000001000000000000000052534c4d00000014636c75740000000100000000000000000000001c6364656300000001000000000000000043736552030000000000001c766572730000000100000000000000000003001c00010000')

Reformat selected Read nodes and Write it

#Select Read nodes and put the right path below

for oReadNode in nuke.selectedNodes():

    oPath =  oReadNode['file'].value()
    first =  oReadNode['first'].value()
    last =  oReadNode['last'].value()
    newPath = oPath.split('/')[-1]
    oReadNode['selected'].setValue(True)
    Reformat = nuke.createNode ("Reformat")
    Reformat.knob('format').setValue('square_1K')
    Write = nuke.createNode ("Write")   

    #Specify file path here
    osdir = 'C:/YOUR_PATH'
    
    Write.knob("beforeRender").setValue("import os\nif not os.path.isdir(os.path.dirname(nuke.thisNode()['file'].evaluate())):\n  os.makedirs(os.path.dirname(nuke.thisNode()['file'].evaluate()))")
    Write.knob('channels').setValue('rgb')
    Write.knob('file').setValue(osdir+'/'+newPath)
    Write.knob('file_type').setValue('jpeg')
    Write.knob('_jpeg_quality').setValue(1)
    Write.knob('use_limit').setValue(True)
    Write.knob('first').setValue(first)
    Write.knob('last').setValue(last)


Start selected Read nodes at frame 1

for n in nuke.selectedNodes('Read'):

    n['frame_mode'].setValue("1")
    n['frame'].setValue("1")


DPX Writes in sRGB (D60 sim.) from Read nodes

for oReadNode in nuke.selectedNodes():

    oPath =  oReadNode['file'].value()
    first =  oReadNode['first'].value()
    last =  oReadNode['last'].value()
    newPath = oPath.split('/')[-1]
    oReadNode['selected'].setValue(True)

    Write = nuke.createNode ("Write")   

    #Specify file path here
    osdir ='C:/YOUR_PATH'
    
    Write.knob('channels').setValue('rgb')
    Write.knob('colorspace').setValue('83)')
    Write.knob('file').setValue(osdir+'/'+newPath)
    Write.knob('file_type').setValue('dpx')
    Write.knob('use_limit').setValue(True)
    Write.knob('first').setValue(first)
    Write.knob('last').setValue(last)

Set Create directories ON on all Write nodes

for n in nuke.allNodes('Write'):

    n['create_directories'].setValue(True)

DPX Writes from Read nodes - Starting at frame 1

for oReadNode in nuke.selectedNodes():

    oPath =  oReadNode['file'].value()
    first =  oReadNode['first'].value()
    last =  oReadNode['last'].value()
    newPath = oPath.split('/')[-1]
    oReadNode['selected'].setValue(True)

    Write = nuke.createNode ("Write")   

    #Specify file path here
    osdir ='C:/YOUR_PATH'
    
    Write.knob('channels').setValue('rgb')
    Write.knob('colorspace').setValue('0)')
    Write.knob('file').setValue(osdir+'/'+newPath)
    Write.knob('file_type').setValue('dpx')
    Write.knob('use_limit').setValue(True)
    Write.knob('first').setValue(1)
    Write.knob('last').setValue(last-first+1)
    Write.knob('create_directories').setValue(True)

JPG Writes from Read nodes - With LUT transform

for oReadNode in nuke.selectedNodes():

    oPath =  oReadNode['file'].value()
    first =  oReadNode['first'].value()
    last =  oReadNode['last'].value()
    newPath = oPath.split('/')[-2]
    oReadNode['selected'].setValue(True)
    OCIOFileTransform = nuke.createNode ("OCIOFileTransform")
    OCIOFileTransform.knob('working_space').setValue('sRGB')

    #Specify lut path here
    lutpath = 'C:/YOUR_PATH/YOUR_LUT.3dl'

    OCIOFileTransform.knob('file').setValue(lutpath)
    Write = nuke.createNode ("Write")   

    #Specify file path here
    osdir = 'C:/YOUR_PATH'
    
    Write.knob("beforeRender").setValue("import os\nif not os.path.isdir(os.path.dirname(nuke.thisNode()['file'].evaluate())):\n  os.makedirs(os.path.dirname(nuke.thisNode()['file'].evaluate()))")
    Write.knob('channels').setValue('rgb')
    Write.knob('file').setValue(osdir+'/'+newPath+'/'+newPath+".####.jpg")
    Write.knob('file_type').setValue('jpeg')
    Write.knob('_jpeg_quality').setValue(1)
    Write.knob('use_limit').setValue(True)
    Write.knob('first').setValue(first)
    Write.knob('last').setValue(last)

EXR Writes from Read nodes - Out Rec 709 to AcesCG

for oReadNode in nuke.selectedNodes():

    oReadNode['colorspace'].setValue('out_rec709')
    oPath =  oReadNode['file'].value()
    first =  oReadNode['first'].value()
    last =  oReadNode['last'].value()
    newPath = oPath.split('/')[-2]
    oReadNode['selected'].setValue(True)

    Write = nuke.createNode ("Write")       
    Write.knob("beforeRender").setValue("import os\nif not os.path.isdir(os.path.dirname(nuke.thisNode()['file'].evaluate())):\n  os.makedirs(os.path.dirname(nuke.thisNode()['file'].evaluate()))")
    Write.knob('channels').setValue('rgb')
    oPath = oPath.replace('.%04d.png', '_ACEScg.%04d.exr')
    oPath = oPath.replace('.1001.png', '_ACEScg.1001.exr')
    Write.knob('file').setValue(oPath)
    Write.knob('colorspace').setValue('ACES - ACEScg')
    Write.knob('file_type').setValue('exr')
    Write.knob('use_limit').setValue(True)
    Write.knob('first').setValue(first)
    Write.knob('last').setValue(last)

All Read nodes set on nearest frame

for n in nuke.allNodes('Read'):

    n['on_error'].setValue(3)

Same frame on all FrameHolds

for n in nuke.allNodes('FrameHold'):

    n['first_frame'].setValue(993)

All Reformats on Cubic

for n in nuke.allNodes('Reformat'):

    n['filter'].setValue(1)

Enable all Read nodes

for node in nuke.allNodes('Read'):
    nuke.toNode(node.name()).knob('disable').setValue(False)

Executing button on selected nodes

sn = nuke.selectedNodes()
for n in sn:
    n.knob('myKnob').execute()