Skip to content

Instantly share code, notes, and snippets.

@rBrenick
Last active November 23, 2021 23:08
Show Gist options
  • Save rBrenick/69a6235d1a0d18d2f762a9993d19d0f4 to your computer and use it in GitHub Desktop.
Save rBrenick/69a6235d1a0d18d2f762a9993d19d0f4 to your computer and use it in GitHub Desktop.
Adds a custom context menu to start and end frame selectors. So you can easily set the start/end frame without typing. NOTE: Might break on large layout refreshes of Maya UI elements (like a layout load)
__author__ = "[email protected]"
import sys
from functools import partial
from maya import mel
from maya import cmds
from maya import OpenMayaUI as omui
from PySide2 import QtCore, QtWidgets, QtGui
from shiboken2 import wrapInstance
if sys.version_info.major > 2:
long = int
def set_playback_start_to_current_frame():
cmds.playbackOptions(e=True, minTime=cmds.currentTime(q=True))
def set_playback_start_to_animation_start():
cmds.playbackOptions(e=True, minTime=cmds.playbackOptions(q=True, animationStartTime=True))
def set_playback_end_to_current_frame():
cmds.playbackOptions(e=True, maxTime=cmds.currentTime(q=True))
def set_playback_end_to_animation_end():
cmds.playbackOptions(e=True, maxTime=cmds.playbackOptions(q=True, animationEndTime=True))
def playback_start_context_menu(time_field_name, _):
def _run_and_deselect(func):
func()
# clear selection from widget (so we don't accidentally write something in it after)
line_edit = wrapInstance(long(omui.MQtUtil().findControl(time_field_name)), QtWidgets.QLineEdit)
line_edit.clearFocus()
menu = QtWidgets.QMenu()
atn = menu.addAction("Set to Current Time")
atn.triggered.connect(partial(_run_and_deselect, set_playback_start_to_current_frame))
atn = menu.addAction("Set to Animation Start")
atn.triggered.connect(partial(_run_and_deselect, set_playback_start_to_animation_start))
cursor = QtGui.QCursor()
menu.exec_(cursor.pos())
def playback_end_context_menu(time_field_name, _):
def _run_and_deselect(func):
func()
# clear selection from widget (so we don't accidentally write something in it after)
line_edit = wrapInstance(long(omui.MQtUtil().findControl(time_field_name)), QtWidgets.QLineEdit)
line_edit.clearFocus()
menu = QtWidgets.QMenu()
atn = menu.addAction("Set to Current Time")
atn.triggered.connect(partial(_run_and_deselect, set_playback_end_to_current_frame))
atn = menu.addAction("Set to Animation End")
atn.triggered.connect(partial(_run_and_deselect, set_playback_end_to_animation_end))
cursor = QtGui.QCursor()
menu.exec_(cursor.pos())
def connect_custom_context_menus():
maya_slider_form = mel.eval('$gPlaybackRangeForm=$gPlaybackRangeForm')
qt_slider_form = wrapInstance(long(omui.MQtUtil().findControl(maya_slider_form)), QtWidgets.QWidget)
min_time_tool_tip_text = mel.eval('uiRes("m_playbackRange.kPlaybackStartTimeAnnot")')
max_time_tool_tip_text = mel.eval('uiRes("m_playbackRange.kPlaybackEndTimeAnnot")')
min_time_widget = None
max_time_widget = None
for le in qt_slider_form.findChildren(QtWidgets.QLineEdit):
if le.toolTip() == min_time_tool_tip_text:
min_time_widget = le
if le.toolTip() == max_time_tool_tip_text:
max_time_widget = le
if not min_time_widget or not max_time_widget:
cmds.warning("Failed to find Qt widgets for min and max time")
return
# get maya ui element names to send on to context menu builder
min_time_name = omui.MQtUtil().fullName(long(omui.MQtUtil().findControl(min_time_widget.objectName())))
max_time_name = omui.MQtUtil().fullName(long(omui.MQtUtil().findControl(max_time_widget.objectName())))
min_time_widget.customContextMenuRequested.connect(partial(playback_start_context_menu, min_time_name))
max_time_widget.customContextMenuRequested.connect(partial(playback_end_context_menu, max_time_name))
if __name__ == "__main__":
connect_custom_context_menus()
@rBrenick
Copy link
Author

How it looks in the UI when right clicking
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment