I was writing an update to my setProjDir script, which is available on nukepedia at the moment of writing this. It is afunction requested from a user, for the script to affect selected nodes only. I’ve decided to go a step further and include options like exclude selected nodes or readgeo nodes. To do that, I’m writing a popup panel for user to change these options.
In the process I discovered that the third value in creating a knob doesn’t necessarily set the default of a knob, for instance
nuke.Boolean_Knob('checkReadGeo', 'Exclude ReadGeo nodes', '0')
The line above doesn’t set the boolean knob “checkReadGeo” as 0, when the panel pops up it’s still default to 1. A solution I found in Nuke user forum is use a setValue() right after. But a better solution I found is define a function yourself to set the value while you create the knob, and it becomes much simpler if you have a lot of knobs to create. So the line above became this:
def newUserKnob(knob, value): knob.setValue(value) return knob p.checkReadGeo = newUserKnob(nuke.Boolean_Knob('checkReadGeo', 'Exclude ReadGeo nodes', '0'), 0)
The next trick I found is putting every custom knobs on a new line instead of next to each other. In my previous attempt to write a python panel this problem didn’t came up because it will automatically put knobs in new line when they becomes too long. To set them into new line yourself, you need setFlag.
p.checkReadGeo = newUserKnob(nuke.Boolean_Knob('checkReadGeo', 'Exclude ReadGeo nodes', '0'), 0 p.checkReadGeo.setFlag(0x1000)
A list of nuke flags can be found on nukepedia here.
So if I were to put every custom knobs into their own new line, I’d include setFlag in the for loop which I add knobs to the panel. My final code to create the panel became this:
p = nukescripts.PythonPanel('Conform file paths to Project Directory') p.nodesSelection = nuke.Enumeration_Knob('nodesSel', 'Nodes selections', ['All nodes', 'Selected nodes only', 'Exclude selected nodes']) p.checkReadGeo = newUserKnob(nuke.Boolean_Knob('checkReadGeo', 'Exclude ReadGeo nodes', '0'), 0) p.readGeoText = nuke.Text_Knob('readGeoText', '', 'Will affect configured scenegraph') for k in (p.nodesSelection, p.checkReadGeo, p.readGeoText): k.setFlag(0x1000) p.addKnob(k)
And that’s the new tricks I learned today, setting default value and new line flag on in custom panel!