给MarkingMenu设置自定义的快捷键

16 篇文章 1 订阅

承接上篇,有时候我们需要设置自己想要的快捷键,而非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模块就是上篇博文里的完整代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值