Search code examples
pythonnuke

Python for Nuke: Select node before current


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.


Solution

  • 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