Overriding hotkeys

Hi. I’ve become more and more frustrated with Maya’s hotkey setup lately. Before 2016 I had these script files with a bunch of name commands and hotkey mappings.

Sure it could be a lot to scroll through and to update, but it worked nicely as I had this base hotkey file and then depending on what I did I had a modeling hotkey file or a UV editor hotkey file, sharing key combos.
So it was kind of layering and wouldn’t overwrite everything when you switched between. This meant that if I wanted to add something in the base hotkey file I could do that without needing to add anywhere else.

Now because of how the hotkeys work in current Maya this isn’t possible anymore. I have to change the sets so it’s a whole new hotkey scheme and if I want to update some viewport or display hotkey, I have to do the same for each set. Obviously it’s not feasible and prone to errors.
So I was thinking of using the old way of just overriding the hotkeys by running a script for each scheme that just sets the current keys.
Unfortunately I haven’t succeeded and I will post what I’ve tried tomorrow when I’m at the computer. But I can’t seem to set the hotkey command. I have the name command and runtime command so it shows up in the editor, but can’t get the hotkey to set.

In the meantime, how do you do it? What is your approach?

I’ve arrived at something that seems to work well, I’m thinking that I need to make it a general method and then place it in a script for modeling hotkeys, interface hotkeys etc.

But this is what it looks like

import maya.cmds as mc

# There is no way to edit a runtime command so we need to check if it
# exists and then remove it if it does.
my_command_name = 'poly_cube'
my_name_command = "poly_cube_nameCommand"

if mc.runTimeCommand(my_command_name, q=True, exists=True):
    mc.runTimeCommand(my_command_name, e=True, delete=True)

# if namecommand exist, delete it
count = cmds.assignCommand(query=True, numElements=True)
for index in range(1, count+1):
    if(mc.assignCommand(index, query=True, name=True) == name_command ):
        mc.assignCommand(edit=True, delete=index)

# delete hotkey
mc.hotkey( k='k', alt=True, name='' )


# now create the commands again and bind it to the hotkey
# create runtime command        
    category='Custom Scripts',
# create name command   
mc.nameCommand(name_command, ann='My Command', command=my_command_name)

# create hotkey
mc.hotkey(k='k', alt=True, ctl=False, name=name_command)

Well, it didn’t work exactly as I wanted. I could create a new hotkey with the same command name and couldn’t figure out how to actually delete all hotkeys sharing the same command name. So I could end up with <ctrl + shift + “k”> as well as <ctrl + “k”> binding to the same command. Not what I want.

Then I found this:

Which is interesting as you can create shortcuts with PySide2 and make it a bit wilder. However I have to look into this more because it wasn’t working perfectly. The shortcut stopped responding =/