0
0

Maya CommandOutputをViewに表示

Last updated at Posted at 2024-08-19
try:
    from PySide6.QtWidgets import QApplication, QLabel, QStackedWidget, QWidget
    from PySide6.QtCore import Qt, QElapsedTimer, QEvent, QObject, QRect, QTimer
    from PySide6.QtGui import QRegion
    from shiboken6 import wrapInstance
except:
    from PySide2.QtWidgets import QApplication, QLabel, QStackedWidget, QWidget
    from PySide2.QtCore import Qt, QElapsedTimer, QEvent, QObject, QRect, QTimer
    from PySide2.QtGui import QRegion
    from shiboken2 import wrapInstance
from maya.OpenMaya import MCommandMessage, MMessage
from maya.OpenMayaUI import M3dView, MQtUtil

if(MQtUtil.findWindow("Kohaku_CommandOutput_Label") == None): # Toggle
    class Kohaku_CommandOutput_LabelClass(QLabel):
        marginWidth = 10
        marginHight = 5
        parentWidget = wrapInstance(int(MQtUtil.mainWindow()), QWidget)
        vewWidget = None
        elapsedTimer = QElapsedTimer()
        interval = 50
        callbackId = None
    
        class FocusInEventFilterClass(QObject):
            def eventFilter(self, watched: QObject, event: QEvent): 
                if event.type() == QEvent.FocusIn:
                    previosView = wrapInstance(int(M3dView.active3dView().widget()), QWidget)
                    previosView.removeEventFilter(Kohaku_CommandOutput_LabelClass.resizeEventFilter)
                    previosView.removeEventFilter(Kohaku_CommandOutput_LabelClass.windowActivateEventFilter)
                    Kohaku_CommandOutput_LabelClass.vewWidget = watched.findChild(QStackedWidget).findChild(QWidget)
                    Kohaku_CommandOutput_LabelClass.vewWidget.installEventFilter(Kohaku_CommandOutput_LabelClass.resizeEventFilter)
                    Kohaku_CommandOutput_LabelClass.vewWidget.installEventFilter(Kohaku_CommandOutput_LabelClass.windowActivateEventFilter)
                    Kohaku_CommandOutput_LabelClass.myMove()
                return False
        focusInEventFilter = FocusInEventFilterClass()
    
        class MoveEventFilterClass(QObject):
            def eventFilter(self, watched: QObject, event: QEvent):
                if event.type() == QEvent.Move:
                    Kohaku_CommandOutput_LabelClass.myMove()
                return False
        moveEventFilter = MoveEventFilterClass()
    
        class ResizeEventFilterClass(QObject):
            def eventFilter(self, watched: QObject, event: QEvent):
                if event.type() == QEvent.Resize:
                    Kohaku_CommandOutput_LabelClass.vewWidget = watched
                    Kohaku_CommandOutput_LabelClass.elapsedTimer.restart()
                    QTimer.singleShot(Kohaku_CommandOutput_LabelClass.interval + 50, Kohaku_CommandOutput_LabelClass.myMicrotremorCountermeasure)
                return False
        resizeEventFilter = ResizeEventFilterClass()
    
        class WindowActivateEventFilterClass(QObject):
            def eventFilter(self, watched: QObject, event: QEvent):
                if event.type() == QEvent.WindowActivate:
                    watched.removeEventFilter(Kohaku_CommandOutput_LabelClass.resizeEventFilter)
                    watched.removeEventFilter(Kohaku_CommandOutput_LabelClass.windowActivateEventFilter)
                    Kohaku_CommandOutput_LabelClass.vewWidget = wrapInstance(int(M3dView.active3dView().widget()), QWidget)
                    Kohaku_CommandOutput_LabelClass.vewWidget.installEventFilter(Kohaku_CommandOutput_LabelClass.resizeEventFilter)
                    Kohaku_CommandOutput_LabelClass.vewWidget.installEventFilter(Kohaku_CommandOutput_LabelClass.windowActivateEventFilter)
                    Kohaku_CommandOutput_LabelClass.myMove()
                return False
        windowActivateEventFilter = WindowActivateEventFilterClass()
        
        def __init__(self):
            self.messageList = []
            super().__init__(Kohaku_CommandOutput_LabelClass.parentWidget)
            self.setGeometry(
                0,
                0,
                QApplication.primaryScreen().size().width(),
                QApplication.primaryScreen().size().height()
            )
            self.setAlignment(Qt.AlignTop | Qt.AlignLeft)
            self.setWindowFlags(
                Qt.Window | 
                Qt.FramelessWindowHint | 
                Qt.WindowTransparentForInput
            )
            self.setAttribute(Qt.WA_TranslucentBackground)
            self.myInstallFocusInEventFilter() # Install focusIn event filter
            self.parentWidget.installEventFilter(Kohaku_CommandOutput_LabelClass.moveEventFilter) # Install move event filter
            Kohaku_CommandOutput_LabelClass.vewWidget = wrapInstance(int(M3dView.active3dView().widget()), QWidget)
            Kohaku_CommandOutput_LabelClass.vewWidget.installEventFilter( # Install resize event filter
                Kohaku_CommandOutput_LabelClass.resizeEventFilter
            )
            Kohaku_CommandOutput_LabelClass.vewWidget.installEventFilter( # Install windowActivate event filter
                Kohaku_CommandOutput_LabelClass.windowActivateEventFilter
            )
            self.setObjectName("Kohaku_CommandOutput_Label")
            Kohaku_CommandOutput_LabelClass.callbackId = MCommandMessage.addCommandOutputFilterCallback(self.myCallback)
            self.show()
    
        def myCallback(self, message, *args):
            self.messageList.extend(message.strip().splitlines())
            listLen = len(self.messageList)
            if listLen >= 10: index = - 10
            else: index = 0 - listLen
            self.setText(("\n").join(self.messageList[index : ]))

        def myInstallFocusInEventFilter(self):
            numberOf3dViews = M3dView.numberOf3dViews()
            m3dView = M3dView()
            for index in range(numberOf3dViews):
                M3dView.get3dView(index, m3dView)
                m3dViewWidget = wrapInstance(int(m3dView.widget()), QWidget)
                displayParentWidget = m3dViewWidget.parent()
                displayParentWidget = displayParentWidget.parent()
                displayParentWidget = displayParentWidget.parent()
                displayParentWidget.installEventFilter(Kohaku_CommandOutput_LabelClass.focusInEventFilter)
    
        @classmethod
        def myMicrotremorCountermeasure(cls):
            if cls.elapsedTimer.elapsed() >= cls.interval:
                cls.elapsedTimer.invalidate()
                Kohaku_CommandOutput_LabelClass.myMove()
    
        @classmethod
        def myMove(cls):
            global gKohaku_CommandOutput_label
            gKohaku_CommandOutput_label.setMask(QRegion(QRect(
                    cls.vewWidget.geometry().left(),
                    cls.vewWidget.geometry().top(),
                    cls.vewWidget.geometry().width() - Kohaku_CommandOutput_LabelClass.marginWidth * 2,
                    cls.vewWidget.geometry().height() - Kohaku_CommandOutput_LabelClass.marginHight * 2
                )))
            gKohaku_CommandOutput_label.move(
                Kohaku_CommandOutput_LabelClass.vewWidget.mapToGlobal(
                    Kohaku_CommandOutput_LabelClass.vewWidget.pos()
                ).x() + Kohaku_CommandOutput_LabelClass.marginWidth,
                Kohaku_CommandOutput_LabelClass.vewWidget.mapToGlobal(
                    Kohaku_CommandOutput_LabelClass.vewWidget.pos()
                ).y() + Kohaku_CommandOutput_LabelClass.marginHight
            )
    
        @classmethod
        def myRemoveAllEventFilter(cls): 
            numberOf3dViews = M3dView.numberOf3dViews()
            m3dView = M3dView()
            for index in range(numberOf3dViews):
                M3dView.get3dView(index, m3dView)
                m3dViewWidget = wrapInstance(int(m3dView.widget()), QWidget)
                m3dViewWidget.removeEventFilter(cls.resizeEventFilter)
                m3dViewWidget.removeEventFilter(cls.windowActivateEventFilter)
                displayParentWidget = m3dViewWidget.parent()
                displayParentWidget = displayParentWidget.parent()
                displayParentWidget = displayParentWidget.parent()
                displayParentWidget.removeEventFilter(cls.focusInEventFilter)
            cls.parentWidget.removeEventFilter(cls.moveEventFilter)

        @classmethod
        def myRemoveCallback(cls):
            MMessage.removeCallback(cls.callbackId)

    gKohaku_CommandOutput_label = Kohaku_CommandOutput_LabelClass()
    gKohaku_CommandOutput_label.myMove()
    print("Welcome to Kohaku Command Output.")
else:
    try: Kohaku_CommandOutput_LabelClass.myRemoveAllEventFilter()
    except: pass
    try: Kohaku_CommandOutput_LabelClass.myRemoveCallback()
    except: pass
    try: gKohaku_CommandOutput_label.deleteLater()
    except: pass
    if 'gKohaku_CommandOutput_label' in globals():
        del gKohaku_CommandOutput_label

農業と兼業で出来る在宅お仕事募集中。
作業できるのは、夜、雨の日、農閑期(冬)です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0