Base
This commit is contained in:
62
scripts/ui/ui_utils.py
Normal file
62
scripts/ui/ui_utils.py
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
UI Utilities Module for Plugin
|
||||
UI工具模块 - 提供UI相关的通用函数
|
||||
"""
|
||||
|
||||
#===================================== IMPORT MODULES =====================================
|
||||
from Qt import QtWidgets, QtCore, QtGui
|
||||
from Qt.QtCompat import wrapInstance
|
||||
from maya import OpenMayaUI as omui
|
||||
import sys
|
||||
import os
|
||||
import config
|
||||
#===================================== UI UTILITY FUNCTIONS ===================================
|
||||
|
||||
TOOL_NAME = config.TOOL_NAME
|
||||
|
||||
def get_parent_widget(widget_name):
|
||||
"""
|
||||
根据控件名称查找父容器控件
|
||||
|
||||
Args:
|
||||
widget_name (str): 控件名称
|
||||
|
||||
Returns:
|
||||
QWidget: 找到的父容器控件,如果未找到则返回None
|
||||
"""
|
||||
# 查找主窗口中的所有控件
|
||||
main_window = None
|
||||
for widget in QtWidgets.QApplication.topLevelWidgets():
|
||||
if widget.objectName() == f"{TOOL_NAME}MainWindow" or widget.objectName().endswith("MainWindow"):
|
||||
main_window = widget
|
||||
break
|
||||
|
||||
if not main_window:
|
||||
print(f"无法找到主窗口,无法获取父容器: {widget_name}")
|
||||
return None
|
||||
|
||||
# 在主窗口中查找指定名称的控件
|
||||
found_widget = main_window.findChild(QtWidgets.QWidget, widget_name)
|
||||
if found_widget:
|
||||
return found_widget
|
||||
|
||||
# 如果未找到精确匹配,尝试模糊匹配
|
||||
for child in main_window.findChildren(QtWidgets.QWidget):
|
||||
if widget_name.lower() in child.objectName().lower():
|
||||
return child
|
||||
|
||||
print(f"无法找到控件: {widget_name}")
|
||||
return None
|
||||
|
||||
def get_maya_main_window():
|
||||
"""
|
||||
获取Maya主窗口
|
||||
|
||||
Returns:
|
||||
QWidget: Maya主窗口控件
|
||||
"""
|
||||
main_window_ptr = omui.MQtUtil.mainWindow()
|
||||
return wrapInstance(int(main_window_ptr), QtWidgets.QWidget)
|
Reference in New Issue
Block a user