Python

“SMARTPOS” KNOB

tl;dr: A useful custom knob that enables the user to to place objects in 3D while working in screen space.

I called “SmartPost” to a custom knob that enables the user to color-pick positional values from a World Point Position Pass (WPP) and then use those values to drive the XYZ coordinates of a XYZ_Knob existing it tools such as the Axis, the TransformGeo, the Sphere, the Card or the Cube node. By doing so, basically one is able to position geometry, cards, axis and such in worldspace based on color values.
This knob can therefore come in handy when working in animation projects (where the WPP is typically rendered by default), allowing us to place objects in 3D while working in screen space, ultimately saving a great deal of time and guessing work.

important:  make sure to copy this code somewhere in your meny.py file to make it work.

snippet
.__author__ Boris Martinez

# assign nuke.createNode() function to a variable for later use.
nuke_create_node = nuke.createNode

def create_my_nodes(node, knobs = "", inpanel = True):
    """
    this function calls nuke_create_node and add a tab and a color picker knob based on the class
    of the node created.
    @param node: Node class.
    @param knobs: Optional string containing a TCL list of name value pairs (like "size 50 quality 19")
    @param inpanel:  Optional boolean to open the control bin (default is True; only applies when the GUI is running).
    @return: result
    """
    result = nuke_create_node(node, knobs, inpanel) # call to nuke_create_node

    if node == "Sphere" or node == "TransformGeo" or node == "Cube" or node == "Card2" or node == "Cylinder" or node \
            == "Axis2":
        tab = nuke.Tab_Knob('SmartPos', 'SmartPos')
        col = nuke.Color_Knob('PickPos')
        result.addKnob(tab)
        result.addKnob(col)

    return result

# overwrite nuke.createNode
nuke.createNode = create_my_nodes

# KnobChanged callable function

def smart_post_expression():
    """
    This is the function to be called as the first argument for the AddKnobChanged callback later on.
    Sets its context to nuke.thisNode() and picks up the changing knob by the nuke.thisKnob() command.
    @return: None
    """
    n = nuke.thisNode()
    k = nuke.thisKnob()
    if k.name() == "PickPos":
        n['translate'].setExpression('PickPos.r', 0)
        n['translate'].setExpression('PickPos.g', 1)
        n['translate'].setExpression('PickPos.b', 2)
    return None

# adding callbacks to certain node classes.

nuke.addKnobChanged(smart_post_expression, nodeClass="TransformGeo")
nuke.addKnobChanged(smart_post_expression, nodeClass="Sphere")
nuke.addKnobChanged(smart_post_expression, nodeClass="Cube")
nuke.addKnobChanged(smart_post_expression, nodeClass="Card2")
nuke.addKnobChanged(smart_post_expression, nodeClass="Cylinder")
nuke.addKnobChanged(smart_post_expression, nodeClass="Axis2")


 

As a side note, I´d like to note that I don´t think it´s ideal the fact that the new “SmartPos” tab created appears first in the properties bin whenever we create a node that´s intended to have it. I´d like to see the main tab first, but so far I didn´t find the way. Any suggestion would be much appreciated. Thanks for reading!
 
Read more →

3 SIMPLE PYTHON SCRIPTS THAT’LL MAKE YOUR COMPING A TINY BIT EASIER

tl;dr: some usefull Python snippets for Nuke.

So I’m finally applying some Python knowledge I’ve been learning lately to actually usefull stuff. Cool!! I will celebrate it by sharing some very, super simple Python snippets that will enable you to work (slightly) faster within Nuke.
Disclaimer: I’m fairly new to Python so you can expect finding not very elegant, unpythonic code. Sorry for that!
snippet #1
This tiny piece of code enables you to automatically create a new ReadNode reading the filepath from a selected WriteNode. Mapping this code to a hotkey makes it a really handy action to use. So let’s see the actual code:
 
.__author__ Boris Martinez

import nuke, re, math, os

def AutoReadFromWrite():

    try:
        n = nuke.selectedNode()
		
    except:
    
        m = nuke.message('select a WriteNode,dude')
        m.show()
	else:
		if n.Class() == 'Write':
			#fetching data from the write node
            p = n.knob('file').getValue()
			fstf = n.knob('first').getValue()
			lstf = n;knob('last').getValue()
			colorspace = str(i.knob('colorspace').getValue()
			#fetching data from the write node
            r = nuke.createNode('Read')
            r.knob('file').setValue(p)
			r.knob('first').setValue(int(firstFrame))
			r.knob('last').setValue(int(lastFrame))
			r.knob('origfirst').setValue(int(firstFrame))
			r.knob('origlast').setValue(int(lastFrame))
			r.knob('colorspace').setValue(colorspace)
		else:
			m = nuke.message("Select a Write node, dude!")
		
		
 AutoReadFromWrite()
snippet #2
This ones takes advantage of some of the concepts from the AutoReadFromWrite function to enable the user to open in the explorer the folder that the filepath knob of a selected ReadNode is pointing to. 
 
.__author__ Boris Martinez

import nuke,os

def AutoExploreFromRead():
    
    try:
        n = nuke.selectedNode()
        f = n.knob("file").getValue()
        p = os.path.dirname(f)
        print "path is %s" %p
        os.startfile(p)
    except:
        p = nuke.message("no file selected")
        p.show()
        print "no file selected"

AutoExploreFromRead()
snippet #3
Here we have a little trickier code comprising concepts we did not see in the previous ones. Stuff like iterations, classes and GUI creation. Functionality wise, it’s really simple! What it does is just creating a randomized animation curve in selected lights. Useful if you want to create some randomized animation for lights in the scene (it affects specifically the intensity value of selected lights).
 
.__author__ Boris Martinez

import nuke
import random
import nukescripts


def lightRandomizer(m,i,o):
    try:
        nodes = nuke.selectedNodes()
        variation = None
        for light in nodes:
            for e in range(i,o):
                variation = random.randrange(1,5)
                if light.Class() == "Light2":

                    light['intensity'].setAnimated()
                    light['intensity'].setValueAt(variation * m,e)
    except:
        ep = nuke.message("No nodes selected")
        ep.show()

def lightRandExpression():
    nodes = nuke.selectedNodes()
    variation = None
    for light in nodes:
        variation = random.randrange(1,5)
        if light.Class() == "Light2":
            light['intensity'].setExpression("random(frame)")
            light['intensity'].setAnimated()

#creating the panel for introducing a multiplicator and launching the lightRandomizer function

class modalPanel(nukescripts.PythonPanel):

    def __init__(self):
        nukescripts.PythonPanel.__init__(self,"multiplier")
        self.m = nuke.Double_Knob("intensity multiplier", "intensity multiplier: ")
        self.addKnob( self.m )
        self.fRange = nuke.String_Knob('fRange', 'Animation Range', '%s-%s' % (nuke.root().firstFrame(), nuke.root().lastFrame()))
        self.addKnob(self.fRange)

    def showModalDialog( self ):
        nukescripts.PythonPanel.showModalDialog(self)

    def giveMultValue(self):
        return  self.m.getValue()

    def giveFrameRangeValue(self):
        return self.fRange.getValue()

#wrapper function

def lightRandmWrapper():
    panel = modalPanel() # an instance from the modalPanel Class
    show = panel.showModalDialog()
    multiplier = panel.giveMultValue()
    frameRange = panel.giveFrameRangeValue()
    frameRangeSplitted = frameRange.split("-")
    ini = int(frameRangeSplitted[0])
    fin = int(frameRangeSplitted[-1])

    #print "frameRange is: ", frameRange
    #print type(frameRange)

    lightRandomizer(multiplier,ini,fin)
    #p = nuke.message (" Boris says: lights randomized, cool!")
    #p.show()

lightRandmWrapper()
#lightRandExpression

So, for the time being, this is all folks. Feel free to copy this snippets for your personal use in Nuke. For snippets #1 & #2 I highly recommend mapping them to a hotkey. If you don’t know how to do so, checking Fxphd’s “Python for Nuke & Gizmology” course by Victor Perez would be really helpfull. For other general Python questions, I strongly recommend Mark Lutz’s “Programming Python” book.
 
Read more →