Using Python with Graph Scope Variables

Editing Variable Values

You can edit Variable values using Python rather than the Variables tab.

In this case, the value of the GsvKnob can be set and retrieved using the regular ‘setValue’ and ‘value’ functions, using a regular Python dictionary type:

For example:

rootGsvKnob = nuke.root()['gsv']
myValue = {'Custom': {'shot': 'sh001'}, '__default__': {'show': 'hot-chocolate'}}
rootGsvKnob.setValue(myValue)
print(rootGsvKnob.value())
# Result: {'Custom': {'shot': 'sh001'}, '__default__': {'show': 'hot-chocolate'}}

Python Callbacks

Python code can now be triggered when Variables are Added, Removed, Renamed, and Changed in Value.

This enables you to create custom behavior based on these events, such as creating templates of Variables, populating Variables into specific folder structures, and adding password protection for Variable value changes.

Function

Description

nuke.addAfterUserAddGsv[Set]

This allows you to trigger Python code whenever a new variable is added. A function registered with nuke.addAfterUserAddGsv[Set] is called with the GSV [Set] path after it's added.

nuke.addBeforeUserRemoveGsv[Set] You can now programmatically trigger Python code when an existing variable is removed. A function registered with nuke.addBeforeUserRemoveGsv[Set] is called with the GSV [Set] path before its removal.
nuke.addBeforeUserRenameGsv[Set] This enables you to execute Python code when a variable is renamed. A function registered with nuke.addBeforeUserRenameGsv[Set] is called with the GSV [Set] path and new name before the renaming occurs.
nuke.addAfterUserSetGsvValue

This allows you to trigger Python code whenever a variable's value is changed or set. A function registered with nuke.addAfterUserSetGsvValue is called with the GSV path after the value is set.

Note:  For unregistering Python Callbacks, there are nuke.remove functions for each of the registration functions above.

Note:  See more in the Nuke Developer Guide.