Better hotkeys with PySide2, how to?

75 views
Skip to first unread message

Leto Atreides

unread,
Sep 27, 2024, 7:41:46 PMSep 27
to Python Programming for Autodesk Maya
Hi. as there are a few things that bothers me with Maya's hotkeys system.

I'm curious if I can code up a class in PySide2 to create my own hotkeys system.
I've read a few threads on here and I've experimented with the eventFilter before.

However if I understand it correctly, using eventFilter installed on the Maya main window is bad as all events will go from C++ to python to C++, making Maya slower?

I'm not sure that I should use an eventFilter, but I want to be able to map both key press and key release that works wherever I have my focus in Maya.
To have it context sensitive could also be nice so I can set up that the same key sequence calls a function when in the UV-editor and then another if I have focus in the viewport.

What do you suggest here?

Justin Israel

unread,
Sep 27, 2024, 7:45:46 PMSep 27
to python_in...@googlegroups.com


On Sat, Sep 28, 2024, 11:41 AM Leto Atreides <stru...@gmail.com> wrote:
Hi. as there are a few things that bothers me with Maya's hotkeys system.

I'm curious if I can code up a class in PySide2 to create my own hotkeys system.
I've read a few threads on here and I've experimented with the eventFilter before.

However if I understand it correctly, using eventFilter installed on the Maya main window is bad as all events will go from C++ to python to C++, making Maya slower?

Yea don't do that in python. If you are going to do it, write it in C++


I'm not sure that I should use an eventFilter, but I want to be able to map both key press and key release that works wherever I have my focus in Maya.
To have it context sensitive could also be nice so I can set up that the same key sequence calls a function when in the UV-editor and then another if I have focus in the viewport.

What do you suggest here?

You would have to catch the events before Maya widgets receive them. If you do it at the application with an event filter, it should be C++ and would have to check every object to see if it is one you want to handle. 
Otherwise you have to find each Qt widget reference and install an event filter, to make it more context specific. 


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b3f25ba2-8233-4dc2-9847-d429336f77d0n%40googlegroups.com.

Leto Atreides

unread,
Sep 27, 2024, 8:46:17 PMSep 27
to Python Programming for Autodesk Maya
@Justin Israel
Thank you. That gives me a lot to think about.

How does Maya's native hotkeys work. Are they in fact Qt based? And are they done with the eventFilter in C++?
For example looking at Ctrl + L it's mapped to a different command in three different editors.
It seems all editors are context sensitive? If I add a new hotkey to a command under the UV editor, I need the UV editor to be in focus for this command to execute.

For my own custom commands, they are obviously not context sensitive as i can run them from any window that has focus. If I should have mapped Ctrl + L to a custom command, it will override any default context sensitive command defined in Maya.

Justin Israel

unread,
Sep 27, 2024, 10:43:05 PMSep 27
to python_in...@googlegroups.com
On Sat, Sep 28, 2024 at 12:46 PM Leto Atreides <stru...@gmail.com> wrote:
@Justin Israel
Thank you. That gives me a lot to think about.

How does Maya's native hotkeys work. Are they in fact Qt based? And are they done with the eventFilter in C++?
For example looking at Ctrl + L it's mapped to a different command in three different editors.
It seems all editors are context sensitive? If I add a new hotkey to a command under the UV editor, I need the UV editor to be in focus for this command to execute.

Don't quote me on this, since I haven' developed for Maya in many years now and don't know what has changed. But I would expect that since Maya is a Qt app, that its hotkey manager would assign the shortcuts to the target widget windows, which make them context sensitive. 
 

For my own custom commands, they are obviously not context sensitive as i can run them from any window that has focus. If I should have mapped Ctrl + L to a custom command, it will override any default context sensitive command defined in Maya.

The way Qt events work is that they are delivered to the QApplication which then delivers them to the most targeted widget. If the widget does not handle the event, it will continue bubbling up the parent object hierarchy until it is handled or falls off. So if you are trying to fight Maya's hotkey implementation, then you have to get to the event before Maya's widgets do. That means installing event filters either on the application or the widgets. The problem with adding event filters to widgets is that some get destroyed and recreated, which means your event filter is gone, 

 
On Saturday, September 28, 2024 at 1:45:46 AM UTC+2 Justin Israel wrote:


On Sat, Sep 28, 2024, 11:41 AM Leto Atreides <stru...@gmail.com> wrote:
Hi. as there are a few things that bothers me with Maya's hotkeys system.

I'm curious if I can code up a class in PySide2 to create my own hotkeys system.
I've read a few threads on here and I've experimented with the eventFilter before.

However if I understand it correctly, using eventFilter installed on the Maya main window is bad as all events will go from C++ to python to C++, making Maya slower?

Yea don't do that in python. If you are going to do it, write it in C++


I'm not sure that I should use an eventFilter, but I want to be able to map both key press and key release that works wherever I have my focus in Maya.
To have it context sensitive could also be nice so I can set up that the same key sequence calls a function when in the UV-editor and then another if I have focus in the viewport.

What do you suggest here?

You would have to catch the events before Maya widgets receive them. If you do it at the application with an event filter, it should be C++ and would have to check every object to see if it is one you want to handle. 
Otherwise you have to find each Qt widget reference and install an event filter, to make it more context specific. 


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b3f25ba2-8233-4dc2-9847-d429336f77d0n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Leto Atreides

unread,
Sep 28, 2024, 3:20:22 PMSep 28
to Python Programming for Autodesk Maya
Thank you Justin. I've been playing around with it today and though it is cool I think I'll go with the nameCommand and the hotkey command instead.
As you say, my eventfilter is destroyed when its parent is destroyed, ie. the editor is closed. And to install it every time the UV Editor(in this case) opens up, would just be too much of an overhead. Do I modify the UI file or track its creation? And if I would modify a copy of the file, then next time a Maya version comes out and something isn't working correctly I'll have to always remind myself to check and compare these files to see if they have been modified or not. So yeah, probably not the best direction to go. And since I can't make my own functions to be context sensitive from within Maya's Hotkey Editor, like only being able to run inside the UV editor, it kind of defeats the purpose of having context sensitivity as I have a few custom scripts I want to use.

So instead:
the hotkey command works fine, but I will lose the context sensitivity as it will override the key combinations and the key combination as created with the hotkey command will override all context sensitive hotkeys. My purpose is though to have and manage a few files(schemes) for modeling, UV-editing, animation. So when I would switch to a new hotkey scheme(I call them scheme not to be confused with Maya's hotkeys set) it would just replace the hotkeys that are defined in that file and I can just switch easily and fast.

Thanks for all the info on this!
If I feel I'm missing out I might just delve into it in C++ because there are still the disadvantage of a few keys that can't be bound using Maya's hotkey command such as TAB, the section key


Leto Atreides

unread,
Sep 29, 2024, 3:46:39 PMSep 29
to Python Programming for Autodesk Maya
Also I missed that you can add your function in userRuntimeCommands to make it context sensitive.

Marcus Ottosson

unread,
Sep 30, 2024, 2:40:13 AMSep 30
to python_in...@googlegroups.com
I've been meaning to mention QShortcut but can never manage to find a peaceful moment. About an eventFilter, this is the guaranteed, sure-fire way of handling all events coming from your mouse and keyboard. All events ultimately start at the QApplication and trickle down, so if you did have an eventFilter there, then that's where you'd get a chance to say "I've got it, do not pass it on to the main window or any children". Including Tab. It's also very likely what Maya already does for many inputs, including Tab and Space, and special-case keys like Alt for camera navigation.

Qt does have a hotkey API called QShortcut but my theory is that Maya is already incorporating an eventFilter prior to giving QShortcut a chance to pick up certain keys.

That said, it does work for most other keys.
from PySide2 import QtWidgets, QtGui
from maya import OpenMayaUI as omui
import shiboken2

def the_b():
    print("I did the B")

main_window_ptr = omui.MQtUtil.mainWindow()
window = shiboken2.wrapInstance(int(main_window_ptr), QtWidgets.QMainWindow)

# Enable
b = QtGui.QKeySequence("B")
shortcut = QtWidgets.QShortcut(b, window)
shortcut.activated.connect(the_b)

# Disable
shortcut.activated.disconnect(the_b)
shortcut.setEnabled(False)
That double-said, this would not be reflected in Maya's hotkey editor and would likely be confusing for users if they are left permanent. And like Justin said, you should avoid an eventFilter from Python, because everyhing in Qt is an event, including drawing of individual widgets, and having an event filter - especially on the QApplication - would funnel every single one of these through Python, serialised and deserialised. However, you could whip up a tiny C++ plug-in to forward relevant events - such as key presses - to your Python code, via MUserMessage for example.


Leto Atreides

unread,
Sep 30, 2024, 12:17:57 PMSep 30
to Python Programming for Autodesk Maya
@Marcus Ottosson
Thank you. Yes. What i've come to realize is that I shouldn't be fighting Maya anymore on this =)
And I think I've found out a few things that I didn't know that can solve the issues I initially had.
I feel that the QShortcut doesn't really add anything to the Maya's native hotkeys implementation(unless it does in fact handle combinations with the mouse buttons). You can't set it to be context sensitive and it can't be setup with a release command. Correct me if I'm wrong.
So for most of the things I think I should stick to the commands Maya offers. I wasn't aware of the hotkeyCtx flag for runTimeCommand  for instance. I've been blind because I knew about the userRunTimeCommand file, just never really had things set up to be context sensitive, but it's for sure useful. Still I need some setup where i can just switch hotkeys, like 3 different schemes all while I work in the viewport and not having to switch manually when firing off hotkeys in the UV-editor, graph editor etc. Will have to look into that, hotkey sets just aren't good for this workflow.

Justin Israel

unread,
Sep 30, 2024, 1:58:31 PMSep 30
to python_in...@googlegroups.com


On Tue, Oct 1, 2024, 5:18 AM Leto Atreides <stru...@gmail.com> wrote:

I feel that the QShortcut doesn't really add anything to the Maya's native hotkeys implementation(unless it does in fact handle combinations with the mouse buttons). You can't set it to be context sensitive and it can't be setup with a release command. Correct me if I'm wrong.

I'll just correct this part. QShortcut has context settings for whether it should apply to the whole app, the window, or the focus widget (and/or its children) 


Leto Atreides

unread,
Sep 30, 2024, 5:28:15 PMSep 30
to Python Programming for Autodesk Maya
Right, but you have to create it with the window and this is easy to  set up and maintain for your custom ones, but not so much for Graph Editor, UV Editor etc, right? Or am I missing the point?

Playing around with the runTimeCommand and its context flag, the nameCommand and the hotkey command and it's working well for my purposes. 

Justin Israel

unread,
Sep 30, 2024, 5:49:06 PMSep 30
to python_in...@googlegroups.com
On Tue, Oct 1, 2024 at 10:28 AM Leto Atreides <stru...@gmail.com> wrote:
Right, but you have to create it with the window and this is easy to  set up and maintain for your custom ones, but not so much for Graph Editor, UV Editor etc, right? Or am I missing the point?

That's correct. I was just pointing out that QShortcut supports contexts.
 

Leto Atreides

unread,
Sep 30, 2024, 5:51:56 PMSep 30
to Python Programming for Autodesk Maya
Cool, got it! Just wanted to be sure.
Thanks
Reply all
Reply to author
Forward
0 new messages