ylSiew

Untitled

Aug 25th, 2015
222
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from PyQt4 import QtGui, QtCore, uic
  2. import sip
  3. import maya.OpenMayaUI as mui
  4. import functools
  5.  
  6. ptr = mui.MQtUtil.mainWindow()
  7. maya_window = sip.wrapinstance(long(ptr), QtCore.QObject)
  8. maya_timeline = maya_window.findChild(QtGui.QWidget, "timeControl1")
  9.  
  10. class TimeLineMarkers(QtGui.QWidget):
  11.     def __init__(self, parent = maya_timeline):
  12.         super(TimeLineMarkers, self).__init__(parent)
  13.         self.layout = QtGui.QVBoxLayout()
  14.         self.setLayout(self.layout)
  15.         self.layout.setContentsMargins(0,0,0,0)
  16.         self.marks = []
  17.     def paintEvent(self, event):
  18.         if not self.marks: return
  19.         color = QtGui.QColor(255, 0, 0, 0)
  20.         painter = QtGui.QPainter(self)
  21.         pen = QtGui.QPen(QtGui.QColor(200,200,255,100))
  22.         pen.setWidth(3)
  23.         painter.setPen(pen)
  24.         for m in self.marks:
  25.             line = QtCore.QLineF(QtCore.QPointF(m,0), QtCore.QPointF(m, 100))
  26.             painter.drawLine(line)
  27.        
  28.     def context_menu(self, point):
  29.         menu = QtGui.QMenu(self)
  30.         menu.addAction("add mark", functools.partial(self.cm_add_mark, point))
  31.         menu.addAction("clear all", self.cm_clear)
  32.         menu.popup(self.mapToGlobal(point))
  33.        
  34.     def cm_add_mark(self, *args):
  35.         pos = args[0]
  36.         self.marks.append(pos.x())
  37.         self.update()
  38.        
  39.     def cm_clear(self):
  40.         self.marks = []
  41.         self.update()
  42.        
  43. class ShiftKeyFilter(QtCore.QObject):
  44.     shift_down = False
  45.     def eventFilter(self, obj, event):
  46.         if event.type() == QtCore.QEvent.KeyPress:
  47.             if event.key() == 16777248:
  48.                 self.shift_down = True
  49.             return False
  50.         if event.type() == QtCore.QEvent.KeyRelease:
  51.             if event.key() == 16777248:
  52.                 self.shift_down = False
  53.             return False
  54.         return False
  55.        
  56. class RightClickFilter(QtCore.QObject):
  57.     def __init__(self, shift_key_filter, context_func):
  58.         super(RightClickFilter, self).__init__()
  59.         self.shift_key_filter = shift_key_filter
  60.         self.context_func = context_func
  61.     def eventFilter(self, obj, event):
  62.         if event.type() == QtCore.QEvent.MouseButtonPress:
  63.             if self.shift_key_filter.shift_down:
  64.                 self.shift_key_filter.shift_down = False
  65.                 self.context_func(event.pos())
  66.             return False
  67.         return False
  68.        
  69. def main():
  70.     global time_line_markers
  71.     try:
  72.         time_line_markers.hide();time_line_markers.setParent(None)
  73.     except:
  74.         pass
  75.     timeline_layout = None
  76.     timeline_layout = maya_timeline.layout()
  77.     if timeline_layout == None:
  78.         timeline_layout = QtGui.QVBoxLayout(maya_timeline)
  79.         timeline_layout.setContentsMargins(0,0,0,0)
  80.         maya_timeline.setLayout(timeline_layout)
  81.    
  82.     timeline_markers = TimeLineMarkers()
  83.     timeline_layout.addWidget(timeline_markers)
  84.    
  85.     #install event filters to get a shift+click menu on the time line
  86.     global shift_key_filter
  87.     global right_click_filter
  88.     shift_key_filter = ShiftKeyFilter()
  89.     right_click_filter = RightClickFilter(shift_key_filter, timeline_markers.context_menu)
  90.     try:
  91.         maya_window.removeEventFilter(shift_key_filter)
  92.         maya_timeline.removeEventFilter(right_click_filter)
  93.     except:
  94.         pass
  95.     maya_window.installEventFilter(shift_key_filter)
  96.     maya_timeline.installEventFilter(right_click_filter)
  97.    
  98. main()
RAW Paste Data