一个美观的maya插件UI- 图标
每行五个图标,界面下面三个按钮
大小自适应
import maya.cmds as cmds
from PySide2 import QtCore, QtGui, QtWidgets
import shiboken2
import maya.OpenMayaUI as omui
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
# 获取Maya主窗口
maya_main_window_ptr = omui.MQtUtil.mainWindow()
maya_main_window = shiboken2.wrapInstance(long(maya_main_window_ptr), QtWidgets.QWidget)
# 将自定义窗口设置为Maya主窗口的子窗口,保证失去焦点时仍显示在前面不被遮挡
super(MyWindow, self).__init__(maya_main_window)
# 创建窗口布局
self.main_layout = QtWidgets.QVBoxLayout()
# 创建图标
self.icons = []
# 十二生肖图标文件名列表
zodiac_icons = ["1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png", "8.png", "9.png", "10.png",
"11.png", "12.png"]
for icon_name in zodiac_icons:
icon = QtWidgets.QLabel()
icon.setPixmap(QtGui.QPixmap("C:\\mayap\\pControllers1.0\\pControllers\\Lib\\{}".format(icon_name)))
# 为每个图标添加鼠标点击事件
icon.mousePressEvent = self.create_icon_click_event(icon_name)
self.icons.append(icon)
# 创建图标布局,每行最多5个图标
self.icon_layout = QtWidgets.QGridLayout()
for i, icon in enumerate(self.icons):
row = i // 5
column = i % 5
self.icon_layout.addWidget(icon, row, column)
# 创建按钮
self.buttons = []
for i in range(3):
button = QtWidgets.QPushButton()
self.buttons.append(button)
# 设置按钮文本
self.buttons[0].setText("左")
self.buttons[1].setText("中")
self.buttons[2].setText("右")
# 创建按钮布局
self.button_layout = QtWidgets.QHBoxLayout()
for button in self.buttons:
self.button_layout.addWidget(button)
# 添加布局到窗口
self.main_layout.addLayout(self.icon_layout)
self.main_layout.addLayout(self.button_layout)
self.central_widget = QtWidgets.QWidget()
self.central_widget.setLayout(self.main_layout)
self.setCentralWidget(self.central_widget)
# 设置窗口最小大小为(10, 30)让它自适应元素大小
self.setMinimumSize(10, 30)
# 连接按钮的点击事件到槽函数
self.buttons[0].clicked.connect(self.print_tian)
self.buttons[1].clicked.connect(self.print_di)
self.buttons[2].clicked.connect(self.print_ren)
# 显示窗口
self.show()
# 创建图标点击事件处理函数
def create_icon_click_event(self, icon_name):
def event(event):
print(self.get_zodiac_animal(icon_name))
return event
# 获取生肖动物名称
def get_zodiac_animal(self, icon_name):
zodiac_animals = {
"1.png": "鼠",
"2.png": "牛",
"3.png": "虎",
"4.png": "兔",
"5.png": "龙",
"6.png": "蛇",
"7.png": "马",
"8.png": "羊",
"9.png": "猴",
"10.png": "鸡",
"11.png": "狗",
"12.png": "猪",
}
return zodiac_animals.get(icon_name, "未知")
# 槽函数:打印"天"
def print_tian(self):
print("天")
# 槽函数:打印"地"
def print_di(self):
print("地")
# 槽函数:打印"人"
def print_ren(self):
print("人")
# 创建窗口
window = MyWindow()
# 创建一个函数来显示窗口
def show_window():
window.show()
# 运行 Maya 命令,传递函数名而不是直接的代码字符串
cmds.scriptJob(runOnce=True, event=["SceneOpened", show_window])