Skip to content

Instantly share code, notes, and snippets.

@rondreas
Last active Feb 13, 2018
Embed
What would you like to do?
Hotkey overlay to toggle modes in Maya,
import pymel.core as pm
from functools import partial
from maya import OpenMayaUI as omui
try:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from shiboken2 import wrapInstance
except ImportError:
from PySide.QtCore import *
from PySide.QtGui import *
from shiboken import wrapInstance
# TODO Fix shortcut ambiguity issue
def _getMainMayaWindow():
mayaMainWindowPtr = omui.MQtUtil.mainWindow()
mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QWidget)
return mayaMainWindow
def toggleMoveMode(currentMode):
toggleModes = {0:'Object', 2:'World', 10:'Component'}
if currentMode == 0:
pm.manipMoveContext('Move', e=True, m=2) # Object->World
elif currentMode == 2:
pm.manipMoveContext('Move', e=True, m=10) # World->Component
elif currentMode == 10:
pm.manipMoveContext('Move', e=True, m=0) # Component->Object
else:
pm.manipMoveContext('Move', e=True, m=2) # World is default, so from any other mode go back world.
pm.inViewMessage(
amg=toggleModes[pm.manipMoveContext('Move', q=True, m=True)],
pos='topCenter',
fade=True,
)
return True
def toggleRotateMode(currentMode):
toggleModes = {0:'Object', 1:'World', 2:'Gimbal'}
if currentMode == 0:
pm.manipRotateContext('Rotate', e=True, m=1)
elif currentMode == 1:
pm.manipRotateContext('Rotate', e=True, m=2)
elif currentMode == 2:
pm.manipRotateContext('Rotate', e=True, m=0)
else:
pm.manipRotateContext('Rotate', e=True, m=0)
pm.inViewMessage(
amg=toggleModes[pm.manipRotateContext('Rotate', q=True, m=True)],
pos='topCenter',
fade=True,
)
return True
def toggleScaleMode(currentMode):
toggleModes = {0:'Object', 2:'World', 10:'Component'}
if currentMode == 0:
pm.manipScaleContext('Scale', e=True, m=2)
elif currentMode == 2:
pm.manipScaleContext('Scale', e=True, m=10)
elif currentMode == 10:
pm.manipScaleContext('Scale', e=True, m=0)
else:
pm.manipScaleContext('Scale', e=True, m=0)
pm.inViewMessage(
amg=toggleModes[pm.manipScaleContext('Scale', q=True, m=True)],
pos='topCenter',
fade=True,
)
return True
def moveToolShortcutActivated(shortcut):
""" Hotkey overlay for Move Tool Context, """
if 'moveSuperContext' == pm.currentCtx():
toggleMoveMode(pm.manipMoveContext('Move', q=True, mode=True))
else:
shortcut.setEnabled(0)
e = QKeyEvent(QEvent.KeyPress, Qt.Key_W, Qt.NoModifier)
QCoreApplication.postEvent(_getMainMayaWindow(), e)
pm.evalDeferred(partial(shortcut.setEnabled, 1))
def rotateToolShortcutActivated(shortcut):
""" Hotkey overlay for Rotate Tool Context, """
if 'RotateSuperContext' == pm.currentCtx():
toggleRotateMode(pm.manipRotateContext('Rotate', q=True, mode=True))
else:
shortcut.setEnabled(0)
e = QKeyEvent(QEvent.KeyPress, Qt.Key_E, Qt.NoModifier)
QCoreApplication.postEvent(_getMainMayaWindow(), e)
pm.evalDeferred(partial(shortcut.setEnabled, 1))
def scaleToolShortcutActivated(shortcut):
""" Hotkey overlay for Scale Tool Context, """
if 'scaleSuperContext' == pm.currentCtx(): # breaking from the previous naming standard starting with lowercase...
toggleScaleMode(pm.manipScaleContext('Scale', q=True, mode=True))
else:
shortcut.setEnabled(0)
e = QKeyEvent(QEvent.KeyPress, Qt.Key_R, Qt.NoModifier)
QCoreApplication.postEvent(_getMainMayaWindow(), e)
pm.evalDeferred(partial(shortcut.setEnabled, 1))
def initShortcuts():
moveModeShortcut = QShortcut(QKeySequence(Qt.Key_W), _getMainMayaWindow())
moveModeShortcut.setContext(Qt.ApplicationShortcut)
moveModeShortcut.activated.connect(partial(moveToolShortcutActivated, moveModeShortcut))
rotateModeShortcut = QShortcut(QKeySequence(Qt.Key_E), _getMainMayaWindow())
rotateModeShortcut.setContext(Qt.ApplicationShortcut)
rotateModeShortcut.activated.connect(partial(rotateToolShortcutActivated, rotateModeShortcut))
scaleModeShortcut = QShortcut(QKeySequence(Qt.Key_R), _getMainMayaWindow())
scaleModeShortcut.setContext(Qt.ApplicationShortcut)
scaleModeShortcut.activated.connect(partial(scaleToolShortcutActivated, scaleModeShortcut))
# Do not call more than once per session, will cause shortcuts to signal ambiguously
initShortcuts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment