Python Viewer State and Hotkeys

   5808   15   3
User Avatar
Member
185 posts
Joined: May 2015
Offline
Greetings everybody

I am wondering if it is possible to disable hotkeys while in a custom python viewer state. Here is my problem, I am trying to use the “t”, “r” and “e” onKeyEvent in my object level python viewer state, but the viewport hotkeys seem to catch them first and it switches to the translation, rotation or scaling tools. I tried with the onKeyTransitEvent and it does catch the “key down” event and I then return True to consume the key transition, but the viewport still switches to the corresponding transformation tool.

Would there be a way to use those keys or do hotkeys have priority over viewer states key events?

Thanks in advance for you help
User Avatar
Staff
450 posts
Joined: Feb. 2018
Offline
Unfortunately, some hotkeys like the ones you mention cannot be overridden by python states. The hotkey architecture is currently being reworked to fix these kinds of problems.
User Avatar
Member
185 posts
Joined: May 2015
Offline
All right, thanks for the answer mabelzile!
User Avatar
Member
34 posts
Joined: July 2015
Offline
mabelzile
Unfortunately, some hotkeys like the ones you mention cannot be overridden by python states. The hotkey architecture is currently being reworked to fix these kinds of problems.

Hi @mabelzile,

Is it still the case?

Thanks
User Avatar
Staff
450 posts
Joined: Feb. 2018
Offline
Akelian
mabelzile
Unfortunately, some hotkeys like the ones you mention cannot be overridden by python states. The hotkey architecture is currently being reworked to fix these kinds of problems.

Hi @mabelzile,

Is it still the case?

Thanks

Yes it still the case.
User Avatar
Member
95 posts
Joined: Feb. 2020
Offline
mabelzile
Unfortunately, some hotkeys like the ones you mention cannot be overridden by python states. The hotkey architecture is currently being reworked to fix these kinds of problems.

Is this still the case
User Avatar
Member
5 posts
Joined: Oct. 2021
Offline
is this still the case
User Avatar
Member
95 posts
Joined: Feb. 2020
Offline
artchapter
s this still the case

I'm pretty sure this is still the case :/
User Avatar
Member
25 posts
Joined: Nov. 2018
Offline
this is now possible. in your createViewerStateTemplate do the following:

state_typename = kwargs["type"].definition().sections()["DefaultState"].contents()

menu = hou.ViewerStateMenu(state_typename + "_menu", state_label)
del_key = su.hotkey(state_typename, "delete_point", "Del", "delete selected points")
menu.addActionItem("delete_point", "delete selected points", hotkey=del_key)
template.bindMenu(menu)

this will override the delete key ("Del") when in your viewer state. "delete_point" is the key code and corresponds to the key code in "menu.addActionItem". when the user hits the delete key, the user will call what would normally be called when selecting the item from the right mouse button menu. "delete selected points" is the description, and and can be anything you would like. the important part is to use the correct key string, "Del" for delete, "UpArrow" for up arrow, "ENTER" for enter, etc...

su.hotkey is part of a library that is already imported in your viewer sate. it handles a lot of what you could be doing manually as shown in the docs: https://www.sidefx.com/docs/houdini/hom/state_menus.html#hotkeys [www.sidefx.com]

hope that helps
User Avatar
Member
185 posts
Joined: May 2015
Offline
Thank you so much for your answer @playBalster!

I will test this as soon as I can
User Avatar
Member
4 posts
Joined: Aug. 2022
Offline
cval
Thank you so much for your answer @playBalster!

I will test this as soon as I can

Hey @cval, were you able to make it work?
From my testing I can’t override these specific shortcuts.

My solution is to remove these shortcuts using the hotkeys python module and then onExit to assign them back, but it’s not good practice I suppose.
User Avatar
Member
373 posts
Joined: June 2023
Offline
Is there an easy way to know which hotkeys are "free to use" by a Python state?
Edited by kodra - Dec. 16, 2023 11:29:21
User Avatar
Member
4 posts
Joined: Aug. 2022
Offline
kodra
Is there an easy way to know which hotkeys are "free to use" by a Python state?
I did a little run through all the characters and the ones who seem to be "locked" are:
d (Display options)
e (Scale tool)
j (Modify Channels)
r (Rotate Tool)
s (Volatile Select Mode)
t (Move Tool)
w (Wireframe)
And also these special characters ` \ ?

I wish we could override these in python states!
User Avatar
Member
9271 posts
Joined: July 2007
Offline
yonatanbary
kodra
Is there an easy way to know which hotkeys are "free to use" by a Python state?
I did a little run through all the characters and the ones who seem to be "locked" are:
d (Display options)
e (Scale tool)
j (Modify Channels)
r (Rotate Tool)
s (Volatile Select Mode)
t (Move Tool)
w (Wireframe)
And also these special characters ` \ ?

I wish we could override these in python states!
Are you talking about H20?

while I don't have much experience with Pyhon States limitations I'd find it odd if these were still an issue especially since Car Rig Python State in H20 uses hotkeys like WASD for movement and they seem to work
Edited by tamte - Dec. 17, 2023 16:38:25

Attachments:
carrig_state_shortcuts.gif (10.4 KB)

Tomas Slancik
CG Supervisor
Framestore, NY
User Avatar
Member
373 posts
Joined: June 2023
Offline
I tried to set move tool's hotkey to G (like in Blender). That breaks almost all the built-in Python states, since they all use G.

Ideally it shouldn't break Python states, unless the specific state has transform handle (so move tool makes sense).

I still don't know when Python state's hotkeys take precedence over Houdini's tho.
Edited by kodra - Dec. 17, 2023 22:21:02
User Avatar
Member
25 posts
Joined: Nov. 2018
Offline
here is more detail, another member messaged me privately and i typed it out, so here it is for anyone else who is stuck:

I think the tricky part to understand about this is that you must have created a menu action for this to work. they keys you want to override, when pressed must perform an option you have added to a menu action.

to create a menu action and a hotkey for that action add the following into the template that exists at the end of every viewer state:


def createViewerStateTemplate():
    state_typename = kwargs.definition().sections().contents()
    state_label = "My State Lable"
    state_cat = hou.sopNodeTypeCategory()

    menu = hou.ViewerStateMenu(state_typename + "_menu", state_label)
    s_key = su.hotkey(state_typename, "my_action", "S", "perform my action")
    menu.addActionItem("my_action", "perform my action", hotkey=s_key)
    template.bindMenu(menu)


You must also have a menu item that will perform the thing you want your key to perform. this goes alongside the other callbacks in your viewer state. here is the doc page on menu actions: https://www.sidefx.com/docs/houdini/hom/state_menus.html [www.sidefx.com]

def onMenuAction(self, kwargs):
    menu_item = kwargs

    if menu_item == "my_action":
    self.myAction()


myAction should contain the logic you want to happen when you press the s key. i'm not fully sure "S" is the correct string for the s key so you might have to dig around to find out what that is. i think it is correct though. the lines important for us in the template from above are:

menu.addActionItem("my_action", "perform my action", hotkey=s_key)

this will allow your menu to function. you should be able to click the RMB when in the viewer state and get a menu with an option called "perform my action". "my_action" is the string used to identify the action. this is used in onMenuAction. hotkey=s_key lets the user perform that action by pressing the key you set up in the previous line in the template from above:

s_key = su.hotkey(state_typename, "my_action", "S", "perform my action")

"S" is the key code for the s key. in the example i posted, "Del" is the key code for the delete key. the other parameters you should know.

like i stated in the beginning, the caveat of this is that ya gotta set up menu actions. it is not a huge caveat, considering you may want the user to be able to choose that action from a dropdown anyway. but still it is more work and you might not want the user to do that for some reason.

hope that helps.
  • Quick Links