Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python for Nuke: Select node before current

Tags:

python

nuke

How can I select a node via python before the one currently selected?

For example, I want to add a "Clamp" node exactly before all "Write" ones.

like image 992
Jose Vicente de Maria Martinez Avatar asked Sep 27 '26 17:09

Jose Vicente de Maria Martinez


1 Answers

This code snippet allows you to define a node upstream existing Write node.

import nuke

iNode = nuke.toNode('Write1')

def upstream(iNode, maxDeep=-1, found=None):

    if found is None:
        found = set()
    if maxDeep != 0:
       willFind = set(z for z in iNode.dependencies() if z not in found)
       found.update(willFind)

       for depth in willFind:
           upstream(depth, maxDeep+1, found)

    return found

Then call the method upstream(iNode).

And a script's snippet you've sent me earlier should look like this:

allWrites = nuke.allNodes('Grade')
depNodes = nuke.selectedNode().dependencies()

for depNode in depNodes:
    depNode.setSelected(True) 

queueElem = len(allWrites)
trigger = -1

for i in range(1,queueElem+1):
    trigger += 1

    for write in allWrites[(0+trigger):(1+trigger)]: 
        write.setSelected(True)
        nuke.createNode("Clamp")

        for all in nuke.allNodes():
            all.setSelected(False)

enter image description here

like image 105
Andy Jazz Avatar answered Sep 30 '26 07:09

Andy Jazz