1

使用以下方法很容易获得时间滑块中的高亮范围:

import maya.cmds as cmds
import maya.mel as mel

aTimeSlider = mel.eval('$tmpVar=$gPlayBackSlider')
timeRange = cmds.timeControl(aTimeSlider, q=True, rangeArray=True)

同样,可以使用 Qt 小部件访问

from maya import mel
from maya import OpenMayaUI as omui 

try:
  from PySide2.QtCore import * 
  from PySide2.QtGui import * 
  from PySide2.QtWidgets import *
  from PySide2 import __version__
  from shiboken2 import wrapInstance 
except ImportError:
  from PySide.QtCore import * 
  from PySide.QtGui import * 
  from PySide import __version__
  from shiboken import wrapInstance 

widgetStr = mel.eval( 'string $tempString = $gPlayBackSlider' )    
ptr = omui.MQtUtil.findControl( widgetStr )
timeSliderWidget = wrapInstance(long(ptr), QWidget)

但是,以编程方式设置时间滑块的突出显示范围似乎几乎是不可能的。真的没有办法了吗?

我在想可能有一种方法可以修改 Qt 小部件,但我迷失了这个。

编辑:

这是我试图以编程方式设置的突出显示范围。 Maya 时间滑块高亮显示范围

4

1 回答 1

1

这似乎与此问题相同。

在那里发布了一个hacky解决方法作为答案。

为了完整起见,也将其发布在这里:这是最大的 hacky 解决方法。使用 Qt 鼠标按下、移动和释放事件来模拟时间滑块小部件上的鼠标行为以使其选择。

在代码中,我暂时更改时间滑块范围,以最大化我想要单击的帧的屏幕空间。

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)
于 2020-10-22T16:29:25.807 回答