My goal: Set up a simple class to add some keyboard macros to Maya at startup.
My Issue: The intended key mappings are not set in the hotkey editor. Editing them manually does work, but as these are default commands, they need to be set programmatically.
-thanks
class Macros(object):
'''
'''
def __init__(self):
'''
'''
def setMacro(self, name=None, k=None, cat=None, ann=None):
'''
Sets a default runtime command with a keyboard shotcut.
args:
name = 'string' - The command name you provide must be unique. The name itself must begin with an alphabetic character or underscore followed by alphanumeric characters or underscores.
cat = 'string' - catagory - Category for the command.
ann = 'string' - annotation - Description of the command.
k = 'string' - keyShortcut - Specify what key is being set.
modifiers: alt, ctl, sht - Modifier values are set by adding a '+' between chars. ie. 'sht+z'.
'''
command = "from macros import Macros; Macros.{0}();".format(name)
#set runTimeCommand
pm.runTimeCommand(
name,
annotation=ann,
category=cat,
command=command,
default=True,
)
#set hotkey
#modifiers
ctl=False; alt=False; sht=False
for char in k.split('+'):
if char=='ctl':
ctl = True
elif char=='alt':
alt = True
elif char=='sht':
sht = True
else:
key = char
pm.hotkey(keyShortcut=key, name=name, ctl=ctl, alt=alt, sht=sht) #set only the key press.
# ------------------------------------------------------
@staticmethod
def hk_back_face_culling():
'''
hk_back_face_culling
Toggle Back-Face Culling
1
'''
print 'hk_back_face_culling()'
Call:
m = Macros()
m.setMacro(name='hk_back_face_culling', k='1', cat='Display', ann='Toggle back-face culling')
Edit: I got it halfway working by adding a nameCommand.
#set command
nameCommand = pm.nameCommand(
'{0}Command'.format(name),
annotation=ann,
command='python("{0}")'.format(command),
)
Here is A fully working example. Perhaps it can help someone in the future.