Skip to content

runTimeCommands and Events

bob-white edited this page Jul 24, 2017 · 2 revisions

the runtimeCommands module allows you to create maya runTimeCommand objects which fire either ordinary python functions or mGui events.

The Hotkeyable and HotkeyableEvent decorators allow you to promote a function to the Hotkey editor with minimal extra work. For example:

@Hotkeyable: simple runtime commands

@Hotkeyable("moveObject", category="mGui", annotation="Hotkey example")
def move_selected():
    selected = cmds.ls(sl=True) or []
    cmds.xform(selected, t=(1,1,1), r=True)

Will call the move_selected function from a new RunTimeCommand called 'moveObject' in the 'mGui' section of the Hotkey editor (runTimeCommands also become available to mel, so you could also type moveObject; in the mel Listener). The new command will always look like this:

# autogenerated callback for absolute.path.to.module

from absolute.path.to.module import function_name
function_name()

So you typically will not have to worry about scoping. You should also be able to reload the module containing the function without restarting maya, since the reload does not redefine the runTimeCommand proper, only the function that it calls.

@HotkeyableEvent: sligthly more complicated

The HotkeyableEvent decorator uses the same signature as the Hotkeyable decorator, but under the hood it is slightly different. It creates an mGui Event and then hooks the decorated function to it as a handler. If there is only a single handler in play, this will function identically to Hotkeyable. However, you can use the broadcasting functionality of the Event to add and remove different handlers to a single runTimeCommand -- so, for example, you could let different gui tools respond to the same hotkey press without worrying about managing keyboard focus. Use RuntimeEvent.add_handler() and RuntimeEvent.remove_handler()to add or remove handler functions on a named Event object.