0

I'm new in writing scripts. In Maya, I'd like to highlight a specific range in timeline without shift+LMB scrubbing. I want to enter start/end values and make the script select that range. Is that even possible? I haven't found any solution yet.

Thanks in advance.

2 Answers2

0

After trying to get it to work then digging around, no, and here's some links that support this:

http://ewertb.mayasound.com/mel/mel.027.php

Set highlighted range in Maya's Time Slider

You can query it but you can't set it which is kind of surprising. Though you should be able to mimic its behavior using cmds.keyframe and cmds.scaleKey.

Green Cell
  • 4,677
  • 2
  • 18
  • 49
0

Here's the biggest hacky workaround there is - in Python however. Using Qt Mouse press, move and release events to mimic the mouse behavior on the time slider widget to have it select. I wouldn't recommend it to a beginner, but at least it works.

In code I'm temporarily changing the time slider range to maximize the screen space for the frames I want to click on.

from maya import mel
from maya import OpenMayaUI as omui 

from shiboken2 import wrapInstance 
from PySide2 import QtCore, QtGui, QtWidgets


def select_time_slider_range(start, end):
    
    app = QtWidgets.QApplication.instance()
    
    widgetStr = mel.eval('$gPlayBackSlider=$gPlayBackSlider')
    ptr = omui.MQtUtil.findControl(widgetStr)
    slider = wrapInstance(long(ptr), QtWidgets.QWidget)
    
    slider_width = slider.size().width()
    slider_height = slider.size().height()
    
    # Store time slider settings
    min_time = cmds.playbackOptions(query=True, minTime=True)
    max_time = cmds.playbackOptions(query=True, maxTime=True)
    animation_start_time = cmds.playbackOptions(query=True, animationStartTime=True)
    animation_end_time = cmds.playbackOptions(query=True, animationEndTime=True)
    t = cmds.currentTime(query=True)
    
    # Set the time slider to the range we want so we have
    # perfect precision to click at the start and end of the
    # time slider.
    cmds.playbackOptions(minTime=start)
    cmds.playbackOptions(maxTime=end)
    
    a_pos = QtCore.QPoint(0, slider_height / 2.0)         
    b_pos = QtCore.QPoint(slider_width, slider_height / 2.0)
    
    # Trigger some mouse events on the Time Control
    # Somehow we need to have some move events around
    # it so the UI correctly understands it stopped
    # clicking, etc.
    event = QtGui.QMouseEvent(QtCore.QEvent.MouseMove, 
                              a_pos, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.NoModifier)
    app.sendEvent(slider, event)
    
    event = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress, 
                              a_pos, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.ShiftModifier)
    app.sendEvent(slider, event)
   
    event = QtGui.QMouseEvent(QtCore.QEvent.MouseMove, 
                              b_pos, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.ShiftModifier)
    app.sendEvent(slider, event)

    event = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonRelease, 
                              b_pos, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.ShiftModifier)
    app.sendEvent(slider, event)
    
    event = QtGui.QMouseEvent(QtCore.QEvent.MouseMove, 
                              b_pos, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.MouseButton.LeftButton, 
                              QtCore.Qt.NoModifier)
    app.sendEvent(slider, event)
    app.processEvents()
    
    # Reset time slider settings
    cmds.playbackOptions(minTime=min_time)
    cmds.playbackOptions(maxTime=max_time)
    cmds.playbackOptions(animationStartTime=animation_start_time)
    cmds.playbackOptions(animationEndTime=animation_end_time)
    cmds.currentTime(t)

select_time_slider_range(-200, 500000)
Roy Nieterau
  • 1,226
  • 2
  • 15
  • 26
  • Genius! I don't know what's going on in this code but it works perfectly fine! Thanks man! – Ümit Çalık Oct 23 '20 at 19:17
  • For what it's worth, you can avoid the time slider twitching/updating during the code by setting the `timeControl` of the time slider to unmanaged during the script using `cmds.timeControl(widgetStr, edit=True, manage=False)` and enabling it at the end again. That way you don't see anything happen with the time slider at all and it's less obvious it's doing all these hacks. – Roy Nieterau Jan 11 '22 at 14:13