Here you all go - the script is attached. To add hotkeys, you would add to your menu.py: ======================== import contextHotkeys contextHotkeys.addContextHotkey("t", "nuke.createNode('TransformGeo')", [{'selectedType': "3D"}, {'selectedType': None, 'viewedType': "3D"}]) contextHotkeys.addContextHotkey("t", "nuke.createNode('Transform')") ======================== Syntax is as follows: addContextHotkey(key, action, match = None) key : the hotkey you want to assign to action : the command to run when this key is pressed, if the filter matches match : a list of possible matches. Each item in the list is OR'd. Each item should be a dictionary, and each key/value pair is AND'd match options are: selectedClass, selectedType, viewedClass and viewedType the selected node is defined by nuke.selectedNode(). If multiple nodes are selected, it's whatever this returns. the viewed node is defined by v = nuke.activeViewer() ; i = v.activeInput() ; viewed = v.node().input(i) "Class" is the specific class of the node. This can include wildcards "Type" is either "2D" or "3D" (as of 6.3, I should probably add "Particles" to that too). A 3D node is defined as one which has both "display" and "selectable" knobs. If match is not defined, or is passed in as None, then this action is set as the default action for the key. The default action is *always* pushed to the end of the list of things to check against. Otherwise, each defined action is checked against in the order that it was added. Do let me know if anyone has any questions. I will probably tidy this up a bit and push it up onto GitHub at some point in the near future. Hugh Macdonald nvizible – VISUAL EFFECTS [email protected] +44(0) 207 659 2038 +44(0) 7773 764 708 www.nvizible.com On 9 Aug 2011, at 01:35, Tim BOWMAN wrote: That looks sweet -- I've been wishing for smart transform/transformGeo for a while now. Would love to see your code if you don't mind sharing. |
import nuke
import fnmatch
contextHotkeys = {}
_3d_knobs = ["display", "selectable"]
_list_types = [list, tuple, set]
def addContextHotkey(key, action, match = None):
if match == None:
match = {'default': True}
if key not in contextHotkeys:
contextHotkeys[key] = []
m = nuke.menu("Nodes")
m.addCommand("ContextHotkey/Context-%s" % key, "import contextHotkeys ; contextHotkeys.execute('%s')" % key, key)
m.addCommand("ContextHotkey/Context-%sBranch" % key, "import contextHotkeys ; contextHotkeys.execute('%s')" % key, "+"+key)
for h in contextHotkeys[key]:
if h['match'] == match:
contextHotkeys[key].remove(h)
break
contextHotkeys[key].append({'action': action, 'match': match})
def execute(key):
if key not in contextHotkeys:
print "Invalid key"
return
defaultAction = None
try:
selected = nuke.selectedNode()
except ValueError:
selected = None
v = nuke.activeViewer()
i = v.activeInput()
if i == None:
viewed = None
else:
viewed = v.node().input(i)
runAction = None
for h in contextHotkeys[key]:
if 'default' in h['match'] and h['match']['default']:
print "Default action: %s" % h['action']
defaultAction = h['action']
matchList = h['match']
if type(matchList) not in _list_types:
matchList = [matchList]
for match in matchList:
hotkeyMatch = True
if 'selectedClass' in match:
matchClasses = match['selectedClass']
if type(matchClasses) not in _list_types:
matchClasses = [matchClasses]
if selected:
classMatch = False
for matchClass in matchClasses:
if matchClass and fnmatch.fnmatch(selected.Class(), matchClass):
classMatch = True
break
if not classMatch:
hotkeyMatch = False
elif None not in matchClasses:
hotkeyMatch = False
if 'selectedType' in match:
print "Matching selectedType: %s" % match['selectedType']
if match['selectedType']:
if selected:
print "Actual selected type: %s" % nodeType(selected)
if nodeType(selected) != match['selectedType']:
hotkeyMatch = False
else:
hotkeyMatch = False
elif selected:
hotkeyMatch = False
if 'viewedClass' in match:
matchClasses = match['viewedClass']
if type(matchClasses) not in _list_types:
matchClasses = [matchClasses]
if viewed:
classMatch = False
for matchClass in matchClasses:
if matchClass and fnmatch.fnmatch(viewed.Class(), matchClass):
classMatch = True
break
if not classMatch:
hotkeyMatch = False
elif None not in matchClasses:
hotkeyMatch = False
if 'viewedType' in match:
print "Matching viewedType: %s" % match['viewedType']
if match['viewedType']:
if viewed:
print "Actual viewed type: %s" % nodeType(viewed)
if nodeType(viewed) != match['viewedType']:
hotkeyMatch = False
else:
hotkeyMatch = False
elif viewed:
hotkeyMatch = False
if hotkeyMatch:
print "Matched. Running %s" % h['action']
runAction = h['action']
break
if runAction:
break
if not runAction and defaultAction:
runAction = defaultAction
if runAction:
eval(runAction)
def nodeType(n):
for k in _3d_knobs:
if k not in n.knobs():
return "2D"
return "3D"_______________________________________________ Nuke-users mailing list [email protected], http://forums.thefoundry.co.uk/ http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-users
