Python Hotkey in Maya

I want to create a hotkey using python. Normally I would just put the python or mel code directly into Hotkey editor command panel, but in this case I want to use a list to save names to. Obviously I don’t want to initialize the list every time I push the hotkey. So would think I need some sort of global list.

My first thought would be to make a python module that would be sourced at startup using either the userSetup.py or userSetup.mel and then the hotkey actually just calls a procedure in the module. Is this feasible? Is there a better way to accomplish this?

Thanks

Hook up the hotkey to a module.

import pymel.core as pm

pm.nameCommand( 'hotkeyTest', ann='Hotkey Test', c='python("import myScript;myScript.myFuction()")')
pm.hotkey( keyShortcut='a', ctrlModifier=True, name='hotkeyTest')

Not tested it, so not sure it works. To be honest, I am not sure I understand what you want to do either.
So if that doesn’t help you please elaborate what you want to do.

yeah I guess I worded that incorrectly. I do not want to create a hotkey using a python script. I want a maya hotkey to use a python script that will contain a list I can keep through out the session. Using a module and initializing it in a startup file seemed like it could work. I was just asking the best way to initialize the list the first time and then the hotkey would save object names to the list as the hotkey is pressed. I was trying to see if using the start-up files in conjunction with the python module was the way to go or if there was a slicker was to do it?

thanks for the reply!

I could be wrong, but I believe passing data between invocations of a hotkey will require the use of global variables.

“stuff” is not remembered between hotkey presses.

try making your list a global variable, each time you press the hotkey, it should append to the list.

There isn’t a way to save variable data between sessions either what I can remember, you will need to save the list in a file on disk I think.

Edit: Totally forgot about optionVar…

you can use an optionVar to save data between maya sessions.

you can use optionVar, or you can serialize data to your own file on disk as well. Personally i would serialize the data to my own file instead of cluttering up the optionVar.

Within a given session, you can implement memory like this:


Class ToolExample (object):

    ACTIVE = None

    def __init__(self):
         self.name = 'name'
         self.size = 1

    def run(self):
        print "my name is %s and my size is %s" % self.name, self.size"
        self.size += 1

    @classmethod
    def get_active(cls):
        if not cls.ACTIVE:
           cls.ACTIVE = ToolExample()
        return cls.ACTIVE


A runtimeCommand would invoke it like


ToolExample.get_active().run()

If you want to save settings or other state between sessions, you’d have to invent your own way to save or restore the state data; optionVars are good as long as what you want to save is simple:



    def __init__(self):
         saved_name = "name"
         if cmds.optionVar(exists = "ExampleToolName"):
            saved_name = cmds.optionVar(q=ExampleToolName)
         self.name = saved_name
         self.size = 1


You’d probably need to update any optionVars every time you change the command state since you won’t a notification or callback when Maya quits

Here is my solution.

you can implement this class somewhere in your scripts directory.


from pymel.util.common import path
import pickle
import pymel.core as pm


class Storage(object):
    def __init__(self):
        self.dumpPath = path('%s/storage.db' % pm.internalVar(usd=True))
        self.data = {}

    def __enter__(self):
        self.data = self.load()
        return self.data

    def __exit__(self, *args):
        self.dump()

    def dump(self):
        with open(self.dumpPath, 'wb') as f:
            pickle.dump(self.data, f)

    def load(self):
        if self.dumpPath.exists():
            with open(self.dumpPath, 'rb') as f:
                return pickle.load(f)
        else:
            self.dump()
            return self.data

Than when you want to persistently store data you can do something like this.


with Storage() as data:
    data['myData'] = [1, 2, 3, 4, 5, 6]

to retrieve said data


with Storage() as data:
    print data['myData']