TransformThis - example for Smart Node Creation
Here is simple recipe for your menu.py, to make a hotkey that creates a normal Transform node for 2D nodes (just like th default 't' does), but if a 3D node is selected, it will create a TransformGeo node instead:
To find out if the selected node is a 3D node, I usually just check for a knob that 2D nodes never have. Let's use the 'render_mode' knob:
'render_mode' in nuke.selectedNode().knobs()
This returns a boolean (True or False) so we can use if in an if statement to check which is true and take it from there:
if 'render_mode' in nuke.selectedNode():
print 'I am a 3D node'
else:
print 'I am a 2D node'
Instead of silly print statements let's just create the respective transform node:
if 'render_mode' in nuke.selectedNode().knobs():
nuke.createNode( 'TransformGeo' )
else:
nuke.createNode( 'Transform' )
Now we just wrap it up into a function:
def transformThis( node ):
if 'render_mode' in node.knobs():
return nuke.createNode( 'TransformGeo' )
else:
return nuke.createNode( 'Transform' )
And lastly, we tie our new function into the UI, overriding the default hotkey (or chose which ever hotkey you prefer):
nuke.menu('Nodes').addCommand( 'Transform/Transform', 'transformThis( nuke.selectedNode() )', 't')
There, we've made Nuke a little bit smarter.