Quick Tip: Programmatically Dealing with Hex Colour in Nuke

Categories Python, Quick Tip

Something I’m endlessly frustrated by is having to work around the way Nuke uses confusing hex colour values in Python to do things such as set a node’s ’tile_color’ knob. For example, how are we supposed to remember that 0xff000ff = green? I wrote two simple functions to get around this, which I hope you can make use of too…

def hex_color_to_int(hexValue):
    return int(hexValue+'00', 16)

The hexValue argument still expects a hexadecimal colour value, although rather than an arbitrary string of characters, you can input a web-based colour which is easily found by Googling “hex colour picker”. For example, I can sample a gold colour as #ffd700 from Google, and then set the ’tile_color’ knob on a selected node with the following:

nuke.selectedNode()['tile_color'].setValue(hex_color_to_int('ffd700'))

Similarly, if you prefer to just use float values like in Nuke’s RGB sliders, this works great:

def hex_color_to_rgb(red, green, blue):
    return int('%02x%02x%02x%02x' % (red*255,green*255,blue*255,255),16)

…and then setting ’tile_color’ with:

nuke.selectedNode()['tile_color'].setValue(hex_color_to_rgb(1,0.6,0))

This one works by converting the strange hex colour to RGB values (0 to 255) and using our 3 arguments to multiply red, green and blue values in a way a Compositor would find more intuitive. Regardless of your favourite method, either option is far simpler and more controllable!