3

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.

budi
  • 6,351
  • 10
  • 55
  • 80

1 Answers1

3

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

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • Hi Andy! Thanks but not what I meant, sorry for the missunderstanding. What I really need is to select the node before the one I want. Imagine I have a bunch of "Write" nodes in my comp and I need to clamp them before writing, so I should select the node before the Write to add the Clamp one so it gets connected between both. I think it could be done via dependencies () or dependant () but not sure if that's the best way. – Jose Vicente de Maria Martinez Jun 20 '17 at 19:53
  • Hi @Andy, for some reason it's not working, Now I'm trying with this code: `allWrites = nuke.allNodes('Write') for write in allWrites: write.setSelected(True) for depNode in depNodes: depNode.setSelected(True) clampNode=nuke.createNode("Clamp")` but it doesn't connect everything. Any further help please? – Jose Vicente de Maria Martinez Jun 21 '17 at 23:19
  • `allWrites = nuke.allNodes('Grade') for write in allWrites: write.setSelected(True) depNodes=nuke.selectedNode().dependencies() for depNode in depNodes: depNode.setSelected(True) clampNode=nuke.createNode("Clamp")` – Jose Vicente de Maria Martinez Jun 23 '17 at 12:26
  • 1
    Sorry for inconveniences but I can't execute the whole script 'cause I've got NUKE Non-Commercial version (There are only 10 nodes available in Python scripting using NC version). So I hope I've got enough ideas to compose your own script. – Andy Jazz Jun 25 '17 at 12:47
  • I'm glad I helped you) – Andy Jazz Jun 26 '17 at 14:21