233 lines
8.8 KiB
Python
233 lines
8.8 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
Toolbar UI Module for Plugin
|
||
工具栏UI模块 - 负责显示工具栏界面和基础操作
|
||
基本功能:
|
||
- 加载预设
|
||
- 保存预设
|
||
- 导入DNA
|
||
- 导出DNA
|
||
- 创建RL4节点(用于切换DNA编辑的状态)
|
||
- 删除RL4节点(用于切换DNA编辑的状态)
|
||
"""
|
||
#========================================= IMPORT =========================================
|
||
from Qt import QtWidgets, QtCore, QtGui
|
||
from Qt.QtCompat import wrapInstance
|
||
from maya import OpenMayaUI as omui
|
||
import maya.cmds as cmds
|
||
import maya.mel as mel
|
||
import maya.utils as utils
|
||
import webbrowser
|
||
import subprocess
|
||
import importlib
|
||
import traceback
|
||
import locale
|
||
import sys
|
||
import os
|
||
from scripts.ui import ui_utils
|
||
from scripts.utils import utils_toolbar
|
||
from scripts.ui.localization import get_text
|
||
#========================================== CONFIG ========================================
|
||
import config
|
||
TOOL_NAME = config.TOOL_NAME
|
||
TOOL_VERSION = config.TOOL_VERSION
|
||
TOOL_AUTHOR = config.TOOL_AUTHOR
|
||
TOOL_YEAR = config.TOOL_YEAR
|
||
TOOL_MOD_FILENAME = config.TOOL_MOD_FILENAME
|
||
TOOL_LANG = config.TOOL_LANG
|
||
TOOL_WSCL_NAME = config.TOOL_WSCL_NAME
|
||
TOOL_HELP_URL = config.TOOL_HELP_URL
|
||
TOOL_PATH = config.TOOL_PATH
|
||
SCRIPTS_PATH = config.SCRIPTS_PATH
|
||
TOOL_MAIN_SCRIPT = config.TOOL_MAIN_SCRIPT
|
||
UI_PATH = config.UI_PATH
|
||
STYLE_FILE = config.STYLE_FILE
|
||
ICONS_PATH = config.ICONS_PATH
|
||
TOOL_ICON = config.TOOL_ICON
|
||
ASSETS_PATH = config.ASSETS_PATH
|
||
DNA_FILE_PATH = config.DNA_FILE_PATH
|
||
DNA_IMG_PATH = config.DNA_IMG_PATH
|
||
TOOL_COMMAND_ICON = config.TOOL_COMMAND_ICON
|
||
TOOL_WIDTH = config.TOOL_WIDTH
|
||
TOOL_HEIGHT = config.TOOL_HEIGHT
|
||
#========================================= LOCATION =======================================
|
||
from scripts.ui import localization
|
||
LANG = localization.LANG
|
||
|
||
class ToolbarUI(ui_utils.BaseUI):
|
||
"""
|
||
工具栏UI类 - 负责显示工具栏界面和基础操作
|
||
继承自BaseUI类,实现工具栏相关的UI功能
|
||
"""
|
||
# 类变量,存储单例实例
|
||
_instance = None
|
||
|
||
@classmethod
|
||
def get_instance(cls):
|
||
"""
|
||
获取ToolbarUI的单例实例
|
||
|
||
Returns:
|
||
ToolbarUI: 单例实例,如果不存在则返回None
|
||
"""
|
||
return cls._instance
|
||
|
||
#========================================== INIT ========================================
|
||
def __init__(self):
|
||
"""
|
||
初始化工具栏UI
|
||
创建主控件和布局,并连接信号和槽
|
||
"""
|
||
super(ToolbarUI, self).__init__()
|
||
|
||
# 设置单例实例
|
||
ToolbarUI._instance = self
|
||
|
||
# 创建主控件
|
||
self.main_widget = QtWidgets.QWidget()
|
||
self.main_widget.setObjectName("toolbarMainWidget")
|
||
|
||
# 初始化UI
|
||
self.create_widgets()
|
||
self.create_layouts()
|
||
self.create_connections()
|
||
|
||
#========================================= WIDGET =======================================
|
||
def create_widgets(self):
|
||
"""
|
||
创建工具栏UI控件
|
||
包括按钮、标签、输入框等
|
||
"""
|
||
# 创建顶部工具栏
|
||
self.controls["toolbar_frame"] = QtWidgets.QFrame()
|
||
self.controls["toolbar_frame"].setObjectName("toolbarFrame")
|
||
self.controls["toolbar_frame"].setFrameShape(QtWidgets.QFrame.StyledPanel)
|
||
self.controls["toolbar_frame"].setFrameShadow(QtWidgets.QFrame.Raised)
|
||
|
||
# 创建工具栏按钮
|
||
# DNA相关按钮
|
||
self.buttons["Save DNA"] = self._create_tool_button("保存DNA", "save.png")
|
||
self.buttons["Open DNA"] = self._create_tool_button("打开DNA", "open.png")
|
||
|
||
# RL4节点相关按钮
|
||
self.buttons["Create RL4 node"] = self._create_tool_button("创建RL4节点", "create_rl4_node.png")
|
||
self.buttons["Delete RL4 node"] = self._create_tool_button("删除RL4节点", "delete_rl4_node.png")
|
||
|
||
# 蒙皮相关按钮
|
||
self.buttons["Import skin"] = self._create_tool_button("导入蒙皮", "import_skin.png")
|
||
self.buttons["Export skin"] = self._create_tool_button("导出蒙皮", "export_skin.png")
|
||
self.buttons["Copy skin"] = self._create_tool_button("复制蒙皮", "copy_skin.png")
|
||
|
||
# 翻译按钮
|
||
self.buttons["Translate"] = self._create_tool_button("切换语言", "translate.png")
|
||
|
||
# 帮助按钮
|
||
self.buttons["Help"] = self._create_tool_button("帮助", "help.png")
|
||
|
||
def _create_tool_button(self, tooltip, icon_name, checkable=False):
|
||
"""
|
||
创建工具栏按钮
|
||
参数:
|
||
tooltip: 按钮提示文字
|
||
icon_name: 图标文件名,不包含路径
|
||
checkable: 是否可选中
|
||
"""
|
||
button = QtWidgets.QPushButton()
|
||
button.setToolTip(get_text(tooltip, tooltip))
|
||
|
||
# 构建图标完整路径
|
||
icon_path = os.path.join(ICONS_PATH, icon_name)
|
||
|
||
# 尝试设置图标,如果图标文件存在
|
||
if os.path.exists(icon_path):
|
||
button.setIcon(QtGui.QIcon(icon_path))
|
||
button.setIconSize(QtCore.QSize(24, 24))
|
||
else:
|
||
# 如果图标不存在,使用文字
|
||
button.setText(get_text(tooltip, tooltip))
|
||
|
||
button.setObjectName(f"{tooltip.replace(' ', '_').lower()}_button")
|
||
button.setFixedSize(32, 32)
|
||
button.setCheckable(checkable)
|
||
return button
|
||
|
||
#========================================= LAYOUT =======================================
|
||
def create_layouts(self):
|
||
"""
|
||
创建工具栏UI布局
|
||
组织控件的排列和层次结构
|
||
"""
|
||
# 主布局
|
||
self.layouts["main_layout"] = QtWidgets.QVBoxLayout(self.main_widget)
|
||
self.layouts["main_layout"].setContentsMargins(0, 0, 0, 0)
|
||
self.layouts["main_layout"].setSpacing(0)
|
||
|
||
# 添加工具栏框架
|
||
self.layouts["main_layout"].addWidget(self.controls["toolbar_frame"])
|
||
|
||
# 工具栏框架布局
|
||
self.layouts["toolbar_layout"] = QtWidgets.QVBoxLayout(self.controls["toolbar_frame"])
|
||
self.layouts["toolbar_layout"].setContentsMargins(5, 5, 5, 5)
|
||
self.layouts["toolbar_layout"].setSpacing(2)
|
||
|
||
# 按钮布局
|
||
self.layouts["toolbar"] = QtWidgets.QHBoxLayout()
|
||
self.layouts["toolbar"].setSpacing(2)
|
||
self.layouts["toolbar"].addWidget(self.buttons["Save DNA"])
|
||
self.layouts["toolbar"].addWidget(self.buttons["Open DNA"])
|
||
self.layouts["toolbar"].addWidget(self._create_separator())
|
||
self.layouts["toolbar"].addWidget(self.buttons["Create RL4 node"])
|
||
self.layouts["toolbar"].addWidget(self.buttons["Delete RL4 node"])
|
||
self.layouts["toolbar"].addWidget(self._create_separator())
|
||
self.layouts["toolbar"].addWidget(self.buttons["Import skin"])
|
||
self.layouts["toolbar"].addWidget(self.buttons["Export skin"])
|
||
self.layouts["toolbar"].addWidget(self.buttons["Copy skin"])
|
||
self.layouts["toolbar"].addWidget(self._create_separator())
|
||
self.layouts["toolbar"].addWidget(self.buttons["Translate"])
|
||
self.layouts["toolbar"].addWidget(self._create_separator())
|
||
self.layouts["toolbar"].addWidget(self.buttons["Help"])
|
||
self.layouts["toolbar"].addStretch()
|
||
|
||
# 添加行到工具栏布局
|
||
self.layouts["toolbar_layout"].addLayout(self.layouts["toolbar"])
|
||
|
||
#======================================= CONNECTION =====================================
|
||
def create_connections(self):
|
||
"""
|
||
连接信号和槽
|
||
设置UI控件的交互行为
|
||
"""
|
||
# DNA相关按钮连接
|
||
self.buttons["Save DNA"].clicked.connect(utils_toolbar.save_dna)
|
||
self.buttons["Open DNA"].clicked.connect(utils_toolbar.open_dna)
|
||
|
||
# RL4节点相关按钮连接
|
||
self.buttons["Create RL4 node"].clicked.connect(utils_toolbar.create_rl4_node)
|
||
self.buttons["Delete RL4 node"].clicked.connect(utils_toolbar.delete_rl4_node)
|
||
|
||
# 蒙皮相关按钮连接
|
||
self.buttons["Import skin"].clicked.connect(utils_toolbar.import_skin)
|
||
self.buttons["Export skin"].clicked.connect(utils_toolbar.export_skin)
|
||
self.buttons["Copy skin"].clicked.connect(utils_toolbar.copy_skin)
|
||
|
||
# 翻译按钮连接
|
||
self.buttons["Translate"].clicked.connect(utils_toolbar.toggle_language)
|
||
|
||
# 帮助按钮连接
|
||
self.buttons["Help"].clicked.connect(utils_toolbar.show_help)
|
||
|
||
#======================================== FUNCTIONS ======================================
|
||
def _create_separator(self):
|
||
"""
|
||
创建分隔线
|
||
"""
|
||
separator = QtWidgets.QFrame()
|
||
separator.setFrameShape(QtWidgets.QFrame.VLine)
|
||
separator.setFrameShadow(QtWidgets.QFrame.Sunken)
|
||
separator.setFixedWidth(2)
|
||
separator.setFixedHeight(24)
|
||
return separator
|
||
|
||
|