Dynamic knobs in Nuke.

Featured image

  Python Quick Tip


You might have seen gizmos with knobs that dynamically disable/enable, or hide/show, based on the value of another knob. This is achieved by accessing the knobChanged knob, which is hidden to users, and can be accessed via Python.

You can disable a selected node's mix knob with this line of code:

1nuke.selectedNode().knob('mix').setEnabled(False)

knob_disable

Similarly, you can hide a selected node's mix knob with this line of code:

1nuke.selectedNode().knob('mix').setVisible(False)

knob_hideshow

Super easy. Now to do hide/show and disable/enable some knobs dynamically, I'll create a NoOp with some custom knobs.

gizmo_eg

And when this code is run in the script editor...

 1# Tell Nuke which node should have the dynamic knobs.
 2nuke.toNode('NoOp1').knob('knobChanged').setValue("""
 3
 4# Set some variables for ease-of-use.
 5node = nuke.thisNode()
 6parent_knob = nuke.thisKnob()
 7
 8# If "Show / Hide Knobs" is set to hide, hide the 3 knobs.
 9if parent_knob.name() == "show_hide_knobs" and parent_knob.value() == "Hide":
10    node.knob('file').setVisible(False)
11    node.knob('slider').setVisible(False)
12    node.knob('button').setVisible(False)
13else:
14    node.knob('file').setVisible(True)
15    node.knob('slider').setVisible(True)
16    node.knob('button').setVisible(True)
17
18# If "Disable Knobs" is checked, disable the 3 knobs.
19if parent_knob.name() == "disable_knobs" and parent_knob.value() == True:
20    node.knob('file').setEnabled(False)
21    node.knob('slider').setEnabled(False)
22    node.knob('button').setEnabled(False)
23else:
24    node.knob('file').setEnabled(True)
25    node.knob('slider').setEnabled(True)
26    node.knob('button').setEnabled(True)
27""")

It does as advertised!

dynamic_knobs

Stepping through this line-by-line, we start by accessing the hidden-to-the-user knobChanged knob on our node named NoOp1. We're setting a value, which is multiple lines of code, so we'll use 3x quotation marks """ to define the start of this code block.

1# Tell Nuke which node should have the dynamic knobs.
2nuke.toNode('NoOp1').knob('knobChanged').setValue("""

Then, we create two variables to make accessing nuke.thisNode() and nuke.thisKnob() easier to access later on. We'll need to use these functions to access specific knobs on the NoOp1 node...

1# Set some variables for ease-of-use.
2node = nuke.thisNode()
3parent_knob = nuke.thisKnob()

Next, we're checking if there is a knob on our current node called show_hide_knobs. If it is, we're also checking if the value of this knob is set to Hide.

1# If "Show / Hide Knobs" is set to hide, hide the 3 knobs.
2if parent_knob.name() == "show_hide_knobs" and parent_knob.value() == "Hide":

If both conditions are True, set the 3x knobs' visibility to False.

1# If "Show / Hide Knobs" is set to hide, hide the 3 knobs.
2if parent_knob.name() == "show_hide_knobs" and parent_knob.value() == "Hide":
3    node.knob('file').setVisible(False)
4    node.knob('slider').setVisible(False)
5    node.knob('button').setVisible(False)

...However if not, they should be True, or visible.

1# If "Show / Hide Knobs" is set to hide, hide the 3 knobs.
2if parent_knob.name() == "show_hide_knobs" and parent_knob.value() == "Hide":
3    node.knob('file').setVisible(False)
4    node.knob('slider').setVisible(False)
5    node.knob('button').setVisible(False)
6else:
7    node.knob('file').setVisible(True)
8    node.knob('slider').setVisible(True)
9    node.knob('button').setVisible(True)