An example plugin that dynamically changes the knob set based on the contents of the text field.
DynamicKnobs
Updated: 27 August 2010
Author: jonathan
Compatible Nuke versions: 5.2 or later
Compatibility: Source
This is an example plugin - the actual Op does nothing except hold the knobs.
The user can enter a list of parameters and their default values which are then parsed and used to construct knobs dynamically. These knobs could then be used (for example) to drive a dynamically constructed shader.
A note to plugin writers:
Dynamic knobs are finicky and to aid in their management I've added a DynamicKnobManager class that attempts to clean up the plugin code and consolidates the state variables need to keep track of the knobs. As is evident this is not a well fleshed-out part of Nuke's API and could use some love to help plugin writers out.
In particular there's three issues:
1) Upon Op construction the knobs are initialized using whatever value the knob that supplies the source text contains by default (in this case knob 'shader_code') but then the Op must know to update itself once a script loads and the knob is changed by 'from_script()'. It's unclear when a knob is changed by script load or by user manipulation making this a dicey affair. For example if the knobs simply rebuilt themselves avery time the source text changed this would dramatically slow down the user's ability to type into the text field as it would occur on every keystroke! So being able to reliably identify the 'initialization' phase of a knob from the 'user interaction' phase would be very helpful.
2) The plugin writer must also store which knob the additional knobs are supposed to be added *after*, plus the current number of inserted knobs, plus whether the knobs have been loaded for the very first time. This is a fair amount of state management that must be replicated properly in every plugin.
3) When the list of added knobs is changed (a knob is added or deleted,) the entire set of added knobs must be rebuilt, destroying whatever value they're currently set to. The plugin writer must save the current values, build the new knob with its default value, then restore the current values. This is a pain...
Hope this helps some plugin writers out,
-jonathan
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.