Setting up a hotkey in maya with python

3.5k views Asked by At

I was looking into making new hotkeys for some custom scripts in python and wanted to use the pm.nameCommand and pm.hotkey commands. The issue is that when I run the following code from the script editor it runs fine and everything is dandy but when I run it from a script then I get errors when trying to use the hotkey.

import pymel.core as pm
import toolTest

#clear existing hotkey
pm.hotkey(keyShortcut='a', ctrlModifier=True, name='')
#create named command for custom tool
#For some reason you need to run the python tool command through a python command in mel
pm.nameCommand( 'hotkeyTest', ann='Hotkey Test', c='python(\"toolTest.testing()\");')
#assign it a hotkey
pm.hotkey( keyShortcut='a', ctrlModifier=True, name='hotkeyTest')

here is the toolTest.py file referenced above

def testing():
    print "Testing Hotkeys"

If you run everything above in the script editor then it should work fine. Then if you put the first section of code into a file (hotkeyTest.py) and run that from the script editor you get the following error while trying to use the hotkey.

# Error: line 1: NameError: file <maya console> line 1: name 'toolTest' is not defined # 

Does anyone know how to use python to set maya hotkeys for a custom tool from an external script?

Thanks!

1

There are 1 answers

0
Bleeding Fingers On

mel's python function runs the code in Python's __main__ module which doesn't have toolTest.

So try:

pm.nameCommand( 'hotkeyTest', ann='Hotkey Test', c='python("import toolTest;toolTest.testing()")')