Dynamic knobs in Nuke.

Categories 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:

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


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

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


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


And when this code is run in the script editor…

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

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

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

# If "Disable Knobs" is checked, disable the 3 knobs.
if parent_knob.name() == "disable_knobs" and parent_knob.value() == True:
    node.knob('file').setEnabled(False)
    node.knob('slider').setEnabled(False)
    node.knob('button').setEnabled(False)
else:
    node.knob('file').setEnabled(True)
    node.knob('slider').setEnabled(True)
    node.knob('button').setEnabled(True)
""")


It does as advertised!

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.

# Tell Nuke which node should have the dynamic knobs.
nuke.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…

# Set some variables for ease-of-use.
node = nuke.thisNode()
parent_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.

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


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

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


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

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