Python for Nuke: How to select a node by name inside a tree with Phyton?

1.5k views Asked by At

I have several grade nodes inside a tree and I want to select one by name and return back its control values.

I've tried to add a variable to the grade and print the controls, but no luck selecting it inside the tree

for i in range (g.getNumKnobs()):
    print g.knob (i).name()
2

There are 2 answers

0
tk421storm On

If the node is inside a Group, then you need to start your code telling nuke to look inside rather than at the root.

You can do this:

nuke.toNode('GroupNode').node('Grade').allKnobs()

or, if you have more code you'd like to to do inside that node:

with nuke.toNode('GroupNode') as parent:
    parent.node('Grade').allKnobs()
    
0
UmeSucré On

Here is a function to print all the knobs of a node and the corresponding value :

def print_all_knobs(node):
    for knob_name in node.knobs():
        print(knob_name, ':', node[knob_name].getValue())

To access to a node by its name :

nuke.toNode('Grade1')

If the node is inside a group I'll do :

with nuke.toNode('GroupeNodeName'):
    for grade in nuke.allNodes('Grade'): # Return all the node whom Call is Grade
        print(grade.name()) # Print the name before printing the knobs
        print_all_knobs(grade)