-
-
Save SEVEZ/a9fcb8bdb0e2b6b52d384229d07e97e8 to your computer and use it in GitHub Desktop.
Autodesk Maya hotkeys with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maya.cmds as cmds | |
import maya.mel as mel | |
from maya.utils import executeDeferred | |
from functools import wraps | |
import re | |
def defer(fn): | |
'''Delays execution of the decorated function until Maya is available.''' | |
@wraps(fn) | |
def deferred(*args, **kwargs): | |
executeDeferred(fn, *args, **kwargs) | |
return deferred | |
def runtime_cmd(name, script, source_type, cat="User"): | |
exists = mel.eval("runTimeCommand -exists {0}".format(name)) | |
if not exists: | |
cmd = "runTimeCommand -c (\"{0}\") -cl {1} -cat User {2}".format( | |
repr(script).strip("'").strip("\n"), source_type, name) | |
print cmd | |
mel.eval(cmd) | |
@defer | |
def hotkey(name, key_sequence, script, source_type="python", | |
release_name="", release_script=None, annotation=None): | |
'''A more convenient function for defining maya hotkeys.''' | |
#Create nameCommands | |
if not annotation: | |
annotation = name | |
runtime_cmd(name, script, source_type) | |
cmds.nameCommand( | |
name, | |
command=name, | |
annotation=annotation, | |
stp=source_type) | |
if release_script and release_name: | |
runtime_cmd(release_name, release_script, source_type) | |
cmds.nameCommand( | |
release_name, | |
command=release_name, | |
annotation=annotation, | |
stp=source_type) | |
#Bind nameCommands to hotkeys | |
ctrl = False | |
alt = False | |
ctrl_match = re.search(r"[Cc]trl\+", key_sequence) | |
alt_match = re.search(r"[Aa]lt\+", key_sequence) | |
if ctrl_match: | |
ctrl = True | |
key_sequence = key_sequence.replace(ctrl_match.group(), "") | |
if alt_match: | |
alt = True | |
key_sequence = key_sequence.replace(ctrl_match.group(), "") | |
if not release_name: | |
cmds.hotkey( | |
keyShortcut=key_sequence, | |
name=name, | |
alt=alt, | |
ctl=ctrl) | |
else: | |
cmds.hotkey( | |
keyShortcut=key_sequence, | |
name=name, | |
alt=alt, | |
ctl=ctrl, | |
releaseName=release_name) | |
#Set up our hotkeys | |
hotline_command = """ | |
import hotline | |
reload(hotline) | |
from hotline.contexts import mayactx | |
hotline.show() | |
""" | |
hotkey("HotLineCommand", key_sequence="h", script=hotline_command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment