So I thought I’d share a bit of code I came up with. Like I said above I needed to create shorctCuts for my buttons and menus but instead of using shortCuts on buttons I’d rather have Maya shortCuts for the ACTUAL function call.
I ended up creating an EchoCommand decorator and used this decorator for each button function call.
Here I have a simple function say stored in myModule.py:
def saySomething(s):
print "You said {}".format(s)
For my button I have this.
self.myButton.clicked.connect(lambda *args: self.saySomething("I'm handsome"))
So you click the button and it prints “You said I’m handsome”. Thanks, you’re so nice!
Now here is the decorator code (I’ve been putting all my decorator code in a decorator.py file and import the commands I need)
class EchoCommand(object):
"""
Similar to Maya's echo commands
Prints out the module and function with arguments
"""
def __init__(self, function):
self.function = function
self.function_call = self.function.__name__
self.module = inspect.getmodule(self.function).__name__
def echo(self, *arguments, **namedArguments):
print "from {} import {}".format(self.module, self.function_call)
print "{}{}".format(self.function.func_name, arguments)
self.function.__call__(*arguments, **namedArguments)
def d_echoCommand(function):
"""
Echo Command
"""
return EchoCommand(function).echo
Let’s implement the decorator now:
@d_echoCommand
def saySomething(s):
print "You said {}".format(s)
Now when you hit the button you should get the echoed command
from myModule import saySomething
saySomething("I'm handsome",)
So now the user can take that bit of code and create a Maya Hotkey, make it repeatable or run it manually.
Hope you find it helpful!
Cheers,
-Sean