Python
Hide unconnected inputs on selected nodes
Just a few lines to hide all not connected inputs. In this example the script touches all Beziers.
1 2 3 4 5 6 7 |
for x in nuke.allNodes("Bezier"): if x.input(0): pass else: x["hide_input"].setValue("1") print x["label"].value() |
Convert rgb to hex colour in python
For some things, like the tile colour of a node, you can’t work with rgb but need to use hex values instead. You can use the following code to convert rgb to hex:
1 |
hexColour = int('%02x%02x%02x%02x' % (r*255,g*255,b*255,1),16) |
So, to set a random tile colour for the selected node, type:
1 2 3 4 5 6 7 |
import random n = nuke.selectedNode() r = (float(random.randint( 20, 40)))/100 g = (float(random.randint( 10, 50)))/100 b = (float(random.randint( 15, 60)))/100 hexColour = int('%02x%02x%02x%02x' % (r*255,g*255,b*255,1),16) n['tile_color'].setValue( hexColour ) |
Random colour values in python
To create random values in python, type:
1 2 |
import random r = (float(random.randint( 20, 40)))/100 |
(randint returns an integer between (x, y). float invokes a floating-point number.)
knobChanged callback for custom gizmos
If you, for example, want to change the appearance of a gizmo’s UI based on user input, you’ll need to incorporate callbacks, most probably the knobChanged callback. This is way easier than I would have expected after googling for hours and not getting it to work.
Select the node you are working on and want to apply the callback to. Head over to the script editor and enter this:
1 |
nuke.selectedNode().knob('knobChanged').setValue('n=nuke.thisNode() \nk=nuke.thisKnob() \nif k.name() == "knob": \n print k.value()') |
What this does: addresses the pseudo knob knobChanged and sets its value to a line of python code. It will output the value of knob, whenever knob is changed.