I wanted to know if there is an option in Maya 2016 , python script, to get the data from the keyboard, but not with opening UI windows or things like the 'raw_input' command in python, but live, like if you play a game, and you press a button, an action is happening. Is there a way to do that in Maya python scripting?
Asked
Active
Viewed 1,460 times
0
-
include code of what you have tried so far – depperm Jul 27 '15 at 14:45
-
I didn't try anything because I don't know if there is.. I looked in the internet however I found only things like the raw_input functions, which is not helpful..If you know a technique, I would be thankful if you share it here ! – ErezProductions Jul 27 '15 at 16:39
-
1You don't get continous key polling with default maya. You might be able to grab the signals from the main Maya window using PySide but it's likely to be problematic because you'll be competing with the main Maya UI loop. If you just want to do hotkeys that don't require window, just create RuntimeCommand objects – theodox Jul 27 '15 at 21:49
-
But I want to work with python, not with MEL.. Is there a way to create such objjects with python? – ErezProductions Jul 28 '15 at 10:44
-
Pyside is a python wrapper for QT, the windowing system that run's Maya's GUI. QT does know how to listen for input events, to listen for key presses you'd have to hack into that. You can as I said use Python to make hotkeyable RuntimeCommands which will let users trigger events from the keyboard. – theodox Jul 28 '15 at 16:39
-
@theodox I saw that the runTimeCommand is suitable for programming in MEL in Maya, and not in Python.. Is that command works with python too? – ErezProductions Jul 29 '15 at 16:57
-
Yep: you just use [nameCommand](http://download.autodesk.com/us/maya/2011help/CommandsPython/nameCommand.html) to create nameCommand object and it will show up in the hotkey editor. The docs show how to connect it to a hotkey – theodox Jul 29 '15 at 17:08
-
@theodox Thank you very much! But, after I created the hotkey - object, how do I tell it to move? for example if the hotkey is pressed, I want the object to move forward 10 positions. – ErezProductions Jul 29 '15 at 17:13
1 Answers
1
def moveCurrent(direction):
getSel = cmds.ls(sl=True)
if getSel:
if direction == "up":
currentVal = cmds.getAttr("%s.tx" % getSel[0])
cmds.setAttr("%s.tx" % getSel[0], currentVal + 10)
elif direction == "down":
currentVal = cmds.getAttr("%s.tx" % getSel[0])
cmds.setAttr("%s.tx" % getSel[0], currentVal - 10)
elif direction == "left":
currentVal = cmds.getAttr("%s.tz" % getSel[0])
cmds.setAttr("%s.tz" % getSel[0], currentVal - 10)
elif direction == "right":
currentVal = cmds.getAttr("%s.tz" % getSel[0])
cmds.setAttr("%s.tz" % getSel[0], currentVal + 10)
cmds.nameCommand( 'moveCurrentSelectionFuncUp', ann='Move Selected Mode', c='python("moveCurrent(\\\"up\\\")")' )
cmds.nameCommand( 'moveCurrentSelectionFuncDown', ann='Move Selected Mode b', c='python("moveCurrent(\\\"down\\\")")' )
cmds.nameCommand( 'moveCurrentSelectionFuncLeft', ann='Move Selected Mode c ', c='python("moveCurrent(\\\"left\\\")")' )
cmds.nameCommand( 'moveCurrentSelectionFuncRight', ann='Move Selected Mode d ', c='python("moveCurrent(\\\"right\\\")")' )
cmds.hotkey( keyShortcut='F5', name='moveCurrentSelectionFuncUp' )
cmds.hotkey( keyShortcut='F6', name='moveCurrentSelectionFuncDown' )
cmds.hotkey( keyShortcut='F7', name='moveCurrentSelectionFuncLeft' )
cmds.hotkey( keyShortcut='F8', name='moveCurrentSelectionFuncRight' )
Make sure you have viewport focus after running above snippet.

Achayan
- 5,720
- 2
- 39
- 61
-
Thank you! I have 2 questions: 1) If I want the object to move into a direction if the hot-key is pressed? How do I do that? 2) If I want a single object to have more than 1 hot-key? for example if I press W it moves forward, S moves it backwards, A to the left and D to the right? – ErezProductions Jul 31 '15 at 01:01
-
It worked! How ever, if I want to move the object without writing the position (up, down, right and left) in the function, how can I do that? – ErezProductions Jul 31 '15 at 20:03
-
I don't know how you can do that, you may need to figure out :). If the answer helpful then upvote and choose as answer please :) – Achayan Jul 31 '15 at 20:17