Native Maya Widgets(1)

# -*- coding: utf-8 -*-
"""
Author: Virgil
Date: 2020/6/7 13:18
"""

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

import maya.OpenMayaUI as omui
import maya.cmds as cmds
import maya.mel as mel

def maya_main_window():
    """
    Return the Maya main window widget as a Python object
    """
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtWidgets.QWidget)

class NativeMayaWidgets(QtWidgets.QDialog):
    WINDOW_TITLE = "Native Maya Widgets"

    dlg_instance = None
    def __init__(self, parent=maya_main_window()):
        super(NativeMayaWidgets, self).__init__(parent)

        self.setWindowTitle(self.WINDOW_TITLE)
        self.setMinimumSize(600, 400)

        self.create_widgets()
        self.create_layout()
        self.create_connections()

        self.timer = QtCore.QTimer()
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.on_timer_fired)
        # self.timer.start()

    def create_widgets(self):
        pass

    def create_layout(self):
        main_layout = QtWidgets.QVBoxLayout(self)
        main_layout.setContentsMargins(5, 5, 5, 5)

    def create_connections(self):
        app = QtWidgets.QApplication.instance()
        app.focusChanged.connect(self.on_focus_changed)

    def print_hierarchy(self, widget):
        if widget:
            output = []

            name = widget.objectName()
            if not name:
                name = "<Object name not set>"

            output.append("Widget: {0}".format(name))

            parent_widget = widget.parentWidget()
            while parent_widget:
                parent_name = parent_widget.objectName()

                output.append("--> Parent: {0}".format(parent_name))
                parent_widget = parent_widget.parentWidget()
            output.append("---")
            print("\n".join(output))

    def on_timer_fired(self):
        focus_widget = QtWidgets.QApplication.focusWidget()
        if focus_widget:
            print("Widget with focus (object name): {0}".format(focus_widget.objectName()))

    def on_focus_changed(self, old_widget, new_widget):
        if self.isVisible():
            if new_widget:
                # print("Widget with focus (object name): {0}".format(new_widget.objectName()))
                self.print_hierarchy(new_widget)

    def closeEvent(self, event):
        self.timer.stop()

def showWindow():
    global dialog
    try:
        dialog.close()
        dialog.deleteLater()
    except:
        pass

    dialog = NativeMayaWidgets()
    dialog.show()

if __name__ == "__main__":
    try:
        dialog.close()
        dialog.deleteLater()
    except:
        pass

    dialog = NativeMayaWidgets()
    dialog.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章