Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
Mod RxPy timeflies example to work with Qt(PySide) in Autodesk Maya
"""
from rx.subjects import Subject
from rx.concurrency import QtScheduler
import maya.OpenMayaUI as omui
from PySide import QtCore
from PySide.QtGui import QWidget, QLabel, QDialog
# Shiboken generates C++ bindings, use to get pointer
from shiboken import wrapInstance
# Default level logs too many events to Maya's Script Editor slowing things down quite a bit
import logging
logging.getLogger("Rx").setLevel(logging.WARN)
def maya_main_window():
main_window_ptr = omui.MQtUtil.mainWindow()
# wrapInstance takes pointer and type to return python object of given type
return wrapInstance(long(main_window_ptr), QWidget)
class PrimitiveUi(QDialog):
def __init__(self, parent=maya_main_window()):
super(PrimitiveUi, self).__init__(parent)
self.setWindowTitle("RxPy In PySide Maya!")
self.setWindowFlags(QtCore.Qt.Tool) # Use .Tool instead of .Window to keep panel above Maya on OSX
# self.setAttribute(QtCore.Qt.WA_DeleteOnClose) # DeleteOnClose has issues with Rx?
self.setMouseTracking(True)
# This Subject is used to transmit mouse moves to labels
self.mousemove = Subject()
def mouseMoveEvent(self, event):
self.mousemove.on_next((event.x(), event.y()))
def closeEvent(self, event):
# TODO: better way to unsubscribe/dispose?
self.mousemove.dispose()
def showWindow():
scheduler = QtScheduler(QtCore)
# clean up previous window/dialog
global RX_DIALOG
try:
RX_DIALOG.close()
# manually delete here or use WA_DeleteOnClose in setAttribute (but which seems to have issues with Rx?)
del RX_DIALOG
except StandardError:
pass
RX_DIALOG = PrimitiveUi()
RX_DIALOG.show()
text = 'TIME FLIES LIKE AN ARROW'
labels = [QLabel(char, RX_DIALOG) for char in text]
def handle_label(i, label):
def on_next(pos):
x, y = pos
label.move(x + i*12 + 15, y)
label.show()
RX_DIALOG.mousemove.delay(i*100, scheduler=scheduler).subscribe(on_next)
for i, label in enumerate(labels):
handle_label(i, label)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.