Getting the index context of a Knob
Scope:
There seems to be no Knob python method to get the context of a specific index inside a knob with multiple values (like an XYZ_Knob).
nuke.thisKnob() would get you the knob something is executed from, but not the index within the knob.
If, for example, you call a command in the Animation menu by right clicking on translate.y, or box.r, you'll only get the translate or the box knob, but not the subfield inside it.
However, The "animations" command in TCL does see the context it was called from, and returns the name of the knob + field it was called from (ex. "translate.x", "translate.y", etc) .
Recipe:
Here's a small helper function in Python that wraps a TCL proc to take
advantage of that feature.
getKnobIndex() definition | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def getKnobIndex(): tclGetAnimIndex = """ set thisanimation [animations] if {[llength $thisanimation] > 1} { return "-1" } else { return [lsearch [in [join [lrange [split [in $thisanimation {animations}] .] 0 end-1] .] {animations}] $thisanimation] } """ return int(nuke.tcl(tclGetAnimIndex)) |
For convenience, the function returns the index within the knob if it was called from a single field, or -1 if it was called from the "Animation Menu" icon of that knob. (see pic)

We can then call that function from within one of those contexts, and know if the user is trying to execute something from one of the fields in the position knob, or from its Animation menu icon.
This can be useful if you have a command to load animation or an expression into the knob, but want to set it only to the sub-field the user right-clicks on.
Here's a short example:
Example | |
14 15 16 17 18 19 |
n = nuke.createNode('Group') k = nuke.XYZ_Knob("position", "position") n.addKnob(k) m = nuke.menu("Animation") m.addCommand("Knob Index Test", "nuke.message('Command called from knob %s, knob index %s' % (nuke.thisKnob().name(), getKnobIndex()))") |
The function called by "Knob Index Test" should now be aware of the knob context and its index.


Comments
| names(...)
| names(n) -> string
|
| Return name for dimension n. The argument n is an integer.
RSS feed for comments to this post