给MarkingMenu设置自定义的快捷键
承接上篇,有时候我们需要设置自己想要的快捷键,而非Alt、Shift、Ctrl这类快捷键,这就是这篇要谈的东西。
首先我们需要给我们的MarkingMenu建runTimeCommand,就是这种东西。
建完这个之后,就可以设置两个快捷键,一个是press,一个是release
press的时候,命令是显示我们的markingmenu;当release的时候,再把我们markingmenu删掉就行,这就实现了markingmenu自定义快捷键的功能。
创建这两个可以用如下代码,一个press,一个release:
# name is user defined script command name
if not cmds.runTimeCommand('%s_press' % name, ex=1):
cmds.runTimeCommand('%s_press' % name, ann='%s' % name, cat='Custom Scripts', hc='', cl='python',
c='import animToolset.animToolBar.markingMenu as mar\nmm = mar.MarkingMenu()\nmm.%s()' % function)
if not cmds.runTimeCommand('%s_release' % name, ex=1):
cmds.runTimeCommand('%s_release' % name, ann='%s' % name, cat='Custom Scripts',
hc='', cl='python', c='import animToolset.animToolBar.markingMenu as mar\nmm = mar.MarkingMenu()\nmm._remove_old()')
建好runtimecommand之后,就要设置快捷键了,在UI上是很简单了,但用脚本的话有点麻烦,这里自己经过反复测试,不能直接用python的command,cmds.nameCommand来创建这两个namecommand,结果只能成功一个,要不就是press的namecommand成功,要不就是release的namecommand成功,总之就是二者不能同时成功,必须是mel来执行才能全部注册好,当然如果你本来就是用mel来注册那就更没问题了,同样也是两个。
# here must use mel to execute
pressNameCommandCmd = "nameCommand -ann \"{0}_pressNameCommand\" -command (\"{0}_press\") {0}_pressNameCommand".format(name)
releaseNameCommandCmd = "nameCommand -ann \"{0}_releaseNameCommand\" -command (\"{0}_release\") {0}_releaseNameCommand".format(name)
mel.eval(pressNameCommandCmd)
mel.eval(releaseNameCommandCmd)
最后为他们赋予个快捷键就行
cmds.hotkey(k='a', rn='%s_releaseNameCommand' % name, **kwargs)
cmds.hotkey(k='a', n='%s_pressNameCommand' % name, **kwargs)
在快捷键这里,我用了**kwargs,为得是后面如果给某个快捷键设置alt,ctrl,shift等,到那时只需要在函数上加上alt=1,就行,让代码更加通用点。
最后附上完整的代码
import os
import sys
import maya.cmds as cmds
import maya.mel as mel
import markingMenu
reload(markingMenu)
mm = markingMenu.MarkingMenu()
class MarkingMenu_Gui():
def __init__(self):
pass
def register_hotkey(self, name, function, **kwargs):
hotkey_set = cmds.hotkeySet(q=1, cu=1)
if hotkey_set == 'Maya_Default':
if cmds.hotkeySet('AnimLazyPlayblast', ex=1):
cmds.hotkeySet('AnimLazyPlayblast', e=1, delete=1)
cmds.hotkeySet('AnimLazyPlayblast', src='Maya_Default', cu=1)
else:
cmds.hotkeySet('AnimLazyPlayblast', src='Maya_Default', cu=1)
if not cmds.runTimeCommand('%s_press' % name, ex=1):
cmds.runTimeCommand('%s_press' % name, ann='%s' % name, cat='Custom Scripts', hc='', cl='python',
c='import animToolset.animToolBar.markingMenu as mar\nmm = mar.MarkingMenu()\nmm.%s()' % function)
if not cmds.runTimeCommand('%s_release' % name, ex=1):
cmds.runTimeCommand('%s_release' % name, ann='%s' % name, cat='Custom Scripts',
hc='', cl='python', c='import animToolset.animToolBar.markingMenu as mar\nmm = mar.MarkingMenu()\nmm._remove_old()')
# here must use mel to execute
pressNameCommandCmd = "nameCommand -ann \"{0}_pressNameCommand\" -command (\"{0}_press\") {0}_pressNameCommand".format(name)
releaseNameCommandCmd = "nameCommand -ann \"{0}_releaseNameCommand\" -command (\"{0}_release\") {0}_releaseNameCommand".format(name)
mel.eval(pressNameCommandCmd)
mel.eval(releaseNameCommandCmd)
cmds.hotkey(k='a', rn='%s_releaseNameCommand' % name, **kwargs)
cmds.hotkey(k='a', n='%s_pressNameCommand' % name, **kwargs)
def register_all_hotkey(self, *args):
self.register_hotkey('ani_menu', '_build_ani')
self.register_hotkey('shotgun_menu', '_build_shotgun', alt=1)
cmds.savePrefs(hk=1)
print '>>> register hotkey'
最后补充一点,当设置alt、ctrl、shift组合快捷键的时候,需要在popupMenu命令里也为其相应的设置好才行,否则不能正确执行,如我这里设置了alt=1,那么在我popupMenu代码里如下:
cmds.popupMenu(SHOTGUN_MENU_NAME, mm=1, b=1, aob=1, ctl=0, alt=1, sh=0, p="viewPanes", pmo=1, pmc=self._build_shotgun_markingMenu)
markingMenu模块就是上篇博文里的完整代码。