0

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?

8
  • include code of what you have tried so far Commented Jul 27, 2015 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 ! Commented Jul 27, 2015 at 16:39
  • 1
    You 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 Commented Jul 27, 2015 at 21:49
  • But I want to work with python, not with MEL.. Is there a way to create such objjects with python? Commented Jul 28, 2015 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. Commented Jul 28, 2015 at 16:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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?
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 :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.