Getting the index context of a Knob

Written by Ivan Busquets on .


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)
KnobIndex_1

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. 

KnobIndex_2  KnobIndex_3 

 

Comments   

 
0 # Lars Cawley 2010-07-29 04:34
Thank you Ivan. This solves a problem I had come up against with a script that I wanted to pipe the name of a knob sub-field into the name of new user knobs that my script creates. When I only had the knob name all my tabs/knobs for the sub-fields where the same and the expressions didn't work. So now I'm using the knobs names method with your index to get their unique names.
| names(...)
| names(n) -> string
|
| Return name for dimension n. The argument n is an integer.
 
 
0 # daniel bigaj 2022-10-21 21:44
Wow thank you so much for this, I have a function I use to add custom GUI expressions to knobs, finally able to set them per index!
 

You have no rights to post comments

We have 4623 guests and 59 members online