This commit is contained in:
Jeffreytsai1004 2025-02-07 11:44:01 +08:00
parent d5bbe481f7
commit df54452607
3 changed files with 116 additions and 81 deletions

View File

@ -30,13 +30,49 @@ except ImportError:
QtCore = QtGui = QtWidgets = None
wrapInstance = None
TOOL_NAME = data.TOOL_NAME
TOOL_ICON = data.TOOL_ICON
TOOL_COMMAND_ICON = data.TOOL_COMMAND_ICON
TOOL_VERSION = data.TOOL_VERSION
TOOL_AUTHOR = data.TOOL_AUTHOR
TOOL_LANG = data.TOOL_LANG
TOOL_WSCL_NAME = data.TOOL_WSCL_NAME
TOOL_YEAR = data.TOOL_YEAR
TOOL_MOD_FILENAME = data.TOOL_MOD_FILENAME
SYSTEM_OS = data.SYSTEM_OS
TOOL_PATH = data.TOOL_PATH
ROOT_DIR = data.TOOL_PATH
ICONS_PATH = data.ICONS_PATH
SCRIPTS_PATH = data.SCRIPTS_PATH
TOOL_STYLE_FILE = data.TOOL_STYLE_FILE
MAYA_VERSION = data.MAYA_VERSION
PYTHON_VERSION_DIR = data.PYTHON_VERSION_DIR
TOOL_HELP_URL = data.TOOL_HELP_URL
PLUGIN_PATH = data.PLUGIN_PATH
PYDNA_PATH = data.PYDNA_PATH
print(f"ROOT_DIR: {ROOT_DIR}")
TOOL_MAIN_SCRIPT = data.TOOL_MAIN_SCRIPT
print(f"TOOL_NAME: {TOOL_NAME}")
print(f"TOOL_VERSION: {TOOL_VERSION}")
print(f"TOOL_AUTHOR: {TOOL_AUTHOR}")
print(f"TOOL_LANG: {TOOL_LANG}")
print(f"TOOL_WSCL_NAME: {TOOL_WSCL_NAME}")
print(f"TOOL_YEAR: {TOOL_YEAR}")
print(f"TOOL_MOD_FILENAME: {TOOL_MOD_FILENAME}")
print(f"TOOL_HELP_URL: {TOOL_HELP_URL}")
print(f"SYSTEM_OS: {SYSTEM_OS}")
print(f"MAYA_VERSION: {MAYA_VERSION}")
print(f"PYTHON: {PYTHON_VERSION_DIR}")
print(f"TOOL_PATH: {TOOL_PATH}")
print(f"ROOT_DIR: {ROOT_DIR}")
print(f"SCRIPTS_PATH: {SCRIPTS_PATH}")
print(f"TOOL_STYLE_FILE: {TOOL_STYLE_FILE}")
print(f"PLUGIN_PATH: {PLUGIN_PATH}")
print(f"PYDNA_PATH: {PYDNA_PATH}")
#===================================== 3. Utility Functions =====================================
def maya_main_window():
"""获取Maya主窗口"""
@ -74,13 +110,13 @@ class InstallDialog(QtWidgets.QDialog):
self.setup_ui()
def load_stylesheet(self):
with open(data.TOOL_STYLE_FILE, 'r', encoding='utf-8') as f:
with open(TOOL_STYLE_FILE, 'r', encoding='utf-8') as f:
style = f.read()
self.setStyleSheet(style)
def setup_ui(self):
"""Initialize and setup UI components"""
self.setWindowTitle(f"{data.TOOL_NAME} Installation")
self.setWindowTitle(f"{TOOL_NAME} Installation")
self.setFixedSize(220, 120)
self.setup_window_icon()
self.create_widgets()
@ -89,16 +125,16 @@ class InstallDialog(QtWidgets.QDialog):
def setup_window_icon(self):
"""Setup window icon if available"""
if os.path.exists(data.TOOL_ICON):
self.setWindowIcon(QtGui.QIcon(data.TOOL_ICON))
if os.path.exists(TOOL_ICON):
self.setWindowIcon(QtGui.QIcon(TOOL_ICON))
else:
print(f"Warning: Icon file not found: {data.TOOL_ICON}")
print(f"Warning: Icon file not found: {TOOL_ICON}")
#----------------- 5.1 UI Methods -----------------
def create_widgets(self):
self.new_shelf_toggle = QtWidgets.QCheckBox(f"{data.TOOL_NAME} Installation")
self.install_button = SetButton("Install " + data.TOOL_NAME)
self.uninstall_button = SetButton("Uninstall " + data.TOOL_NAME)
self.new_shelf_toggle = QtWidgets.QCheckBox(f"{TOOL_NAME} Installation")
self.install_button = SetButton("Install " + TOOL_NAME)
self.uninstall_button = SetButton("Uninstall " + TOOL_NAME)
def create_layouts(self):
main_layout = QtWidgets.QVBoxLayout(self)
@ -108,7 +144,7 @@ class InstallDialog(QtWidgets.QDialog):
header_layout = QtWidgets.QHBoxLayout()
header_layout.setSpacing(5)
welcome_label = QtWidgets.QLabel("Welcome to " + data.TOOL_NAME + "!")
welcome_label = QtWidgets.QLabel("Welcome to " + TOOL_NAME + "!")
welcome_label.setStyleSheet("font-size: 11px; padding: 0px; margin: 0px;")
header_layout.addWidget(welcome_label)
header_layout.addStretch()
@ -154,7 +190,7 @@ class InstallDialog(QtWidgets.QDialog):
#----------------- 5.3 Utility Methods -----------------
def open_help_url(self):
webbrowser.open(data.TOOL_HELP_URL)
webbrowser.open(TOOL_HELP_URL)
QtWidgets.QApplication.restoreOverrideCursor()
def get_script_path():
@ -177,7 +213,7 @@ class InstallDialog(QtWidgets.QDialog):
msg_box = self.create_styled_message_box(
"Confirm Installation",
f"Are you sure you want to install {data.TOOL_NAME}?"
f"Are you sure you want to install {TOOL_NAME}?"
)
if msg_box.exec_() == QtWidgets.QMessageBox.Yes:
try:
@ -192,7 +228,7 @@ class InstallDialog(QtWidgets.QDialog):
"""Handle uninstall request"""
msg_box = self.create_styled_message_box(
"Confirm Uninstallation",
f"Are you sure you want to uninstall {data.TOOL_NAME}?"
f"Are you sure you want to uninstall {TOOL_NAME}?"
)
if msg_box.exec_() == QtWidgets.QMessageBox.Yes:
@ -209,15 +245,15 @@ class InstallDialog(QtWidgets.QDialog):
def create_mod_file(self):
"""Create or update the .mod file for Maya"""
modules_dir = get_maya_modules_dir()
mod_content = f"""+ {data.TOOL_NAME} {data.TOOL_VERSION} {data.TOOL_PATH}
scripts: {data.SCRIPTS_PATH}
plug-ins: plugins/{data.SYSTEM_OS}/{data.MAYA_VERSION}
mod_content = f"""+ {TOOL_NAME} {TOOL_VERSION} {TOOL_PATH}
scripts: {SCRIPTS_PATH}
plug-ins: plugins/{SYSTEM_OS}/{MAYA_VERSION}
XBMLANGPATH+:=resources/icons
PYTHONPATH+:=plugins/{data.SYSTEM_OS}/{data.MAYA_VERSION}
PYTHONPATH+:=plugins/{data.SYSTEM_OS}/{data.MAYA_VERSION}/pydna/{data.PYTHON_VERSION_DIR}
TOOL_PATH+:={data.TOOL_PATH}
PYTHONPATH+:=plugins/{SYSTEM_OS}/{MAYA_VERSION}
PYTHONPATH+:=plugins/{SYSTEM_OS}/{MAYA_VERSION}/pydna/{PYTHON_VERSION_DIR}
TOOL_PATH+:={TOOL_PATH}
"""
mod_file_path = os.path.join(modules_dir, data.TOOL_MOD_FILENAME)
mod_file_path = os.path.join(modules_dir, TOOL_MOD_FILENAME)
self._write_mod_file(mod_file_path, mod_content)
def _write_mod_file(self, file_path, content):
@ -233,37 +269,37 @@ TOOL_PATH+:={data.TOOL_PATH}
def uninstall_mod_file(self):
modules_dir = get_maya_modules_dir()
mod_file_path = os.path.join(modules_dir, data.TOOL_MOD_FILENAME)
mod_file_path = os.path.join(modules_dir, TOOL_MOD_FILENAME)
if os.path.exists(mod_file_path):
try:
os.remove(mod_file_path)
print(f"{data.TOOL_NAME}.mod file deleted")
print(f"{TOOL_NAME}.mod file deleted")
except Exception as e:
print(f"Error deleting {data.TOOL_NAME}.mod file: {e}")
print(f"Error deleting {TOOL_NAME}.mod file: {e}")
def clean_existing_buttons(self):
if cmds.shelfLayout(data.TOOL_NAME, exists=True):
buttons = cmds.shelfLayout(data.TOOL_NAME, query=True, childArray=True) or []
if cmds.shelfLayout(TOOL_NAME, exists=True):
buttons = cmds.shelfLayout(TOOL_NAME, query=True, childArray=True) or []
for btn in buttons:
if cmds.shelfButton(btn, query=True, exists=True):
label = cmds.shelfButton(btn, query=True, label=True)
if label == data.TOOL_NAME:
if label == TOOL_NAME:
cmds.deleteUI(btn)
print(f"Deleted existing {data.TOOL_NAME} button: {btn}")
print(f"Deleted existing {TOOL_NAME} button: {btn}")
def install_tool(self):
"""Install the tool to Maya"""
if not os.path.exists(data.SCRIPTS_PATH):
print(f"Error: Scripts path does not exist: {data.SCRIPTS_PATH}")
if not os.path.exists(SCRIPTS_PATH):
print(f"Error: Scripts path does not exist: {SCRIPTS_PATH}")
return
if not os.path.exists(data.TOOL_MAIN_SCRIPT):
print(f"Error: Main script file not found: {data.TOOL_MAIN_SCRIPT}")
if not os.path.exists(TOOL_MAIN_SCRIPT):
print(f"Error: Main script file not found: {TOOL_MAIN_SCRIPT}")
return
# Add scripts path to Python path
if data.SCRIPTS_PATH not in sys.path:
sys.path.insert(0, data.SCRIPTS_PATH)
if SCRIPTS_PATH not in sys.path:
sys.path.insert(0, SCRIPTS_PATH)
# Create shelf and button
self._create_shelf_button()
@ -271,10 +307,10 @@ TOOL_PATH+:={data.TOOL_PATH}
# Switch to the newly created shelf
try:
cmds.shelfTabLayout("ShelfLayout", edit=True, selectTab=data.TOOL_NAME)
print(f"Switched to {data.TOOL_NAME} shelf")
cmds.shelfTabLayout("ShelfLayout", edit=True, selectTab=TOOL_NAME)
print(f"Switched to {TOOL_NAME} shelf")
except Exception as e:
print(f"Error switching to {data.TOOL_NAME} shelf: {e}")
print(f"Error switching to {TOOL_NAME} shelf: {e}")
self._show_install_success_message()
@ -283,24 +319,24 @@ TOOL_PATH+:={data.TOOL_PATH}
shelf_layout = mel.eval('$tmpVar=$gShelfTopLevel')
# Create shelf if not exists
if not cmds.shelfLayout(data.TOOL_NAME, exists=True):
cmds.shelfLayout(data.TOOL_NAME, parent=shelf_layout)
if not cmds.shelfLayout(TOOL_NAME, exists=True):
cmds.shelfLayout(TOOL_NAME, parent=shelf_layout)
# Clean existing buttons
self.clean_existing_buttons()
# Create new button
icon_path = data.TOOL_ICON if os.path.exists(data.TOOL_ICON) else data.TOOL_COMMAND_ICON
icon_path = TOOL_ICON if os.path.exists(TOOL_ICON) else TOOL_COMMAND_ICON
command = self._get_shelf_button_command()
cmds.shelfButton(
parent=data.TOOL_NAME,
parent=TOOL_NAME,
image1=icon_path,
label=data.TOOL_NAME,
label=TOOL_NAME,
command=command,
sourceType="python",
annotation=f"{data.TOOL_NAME} {data.TOOL_VERSION}",
annotation=f"{TOOL_NAME} {TOOL_VERSION}",
noDefaultPopup=True,
style="iconOnly"
)
@ -310,10 +346,10 @@ TOOL_PATH+:={data.TOOL_PATH}
return f"""
import sys
import os
TOOL_PATH = r'{data.TOOL_PATH}'
TOOL_PATH = r'{TOOL_PATH}'
if TOOL_PATH not in sys.path:
sys.path.insert(0, TOOL_PATH)
SCRIPTS_PATH = r'{data.SCRIPTS_PATH}'
SCRIPTS_PATH = r'{SCRIPTS_PATH}'
if SCRIPTS_PATH not in sys.path:
sys.path.insert(0, SCRIPTS_PATH)
os.chdir(SCRIPTS_PATH)
@ -322,28 +358,28 @@ try:
MetaFusion.show()
except ImportError as e:
print("Error importing MetaFusion:", str(e))
print(f"Scripts path: {data.SCRIPTS_PATH}")
print(f"Scripts path: {SCRIPTS_PATH}")
print("sys.path:", sys.path)
print(f"Contents of Scripts folder: {os.listdir(data.SCRIPTS_PATH)}")
print(f"Contents of Scripts folder: {os.listdir(SCRIPTS_PATH)}")
"""
def uninstall_tool(self):
"""Uninstall the tool from Maya"""
window_name = f"{data.TOOL_NAME}Window"
dock_name = f"{data.TOOL_NAME}WindowDock"
shelf_file = f"shelf_{data.TOOL_NAME}.mel"
window_name = f"{TOOL_NAME}Window"
dock_name = f"{TOOL_NAME}WindowDock"
shelf_file = f"shelf_{TOOL_NAME}.mel"
if cmds.window(window_name, exists=True):
try:
cmds.deleteUI(window_name)
except Exception as e:
print(f"Error closing {data.TOOL_NAME} window: {e}")
print(f"Error closing {TOOL_NAME} window: {e}")
if cmds.dockControl(dock_name, exists=True):
try:
cmds.deleteUI(dock_name)
except Exception as e:
print(f"Error closing docked {data.TOOL_NAME} window: {e}")
print(f"Error closing docked {TOOL_NAME} window: {e}")
self.uninstall_mod_file()
@ -351,17 +387,17 @@ except ImportError as e:
current_shelf = cmds.shelfTabLayout("ShelfLayout", query=True, selectTab=True)
# Delete Shelves and Buttons
if cmds.shelfLayout(data.TOOL_NAME, exists=True):
if cmds.shelfLayout(TOOL_NAME, exists=True):
try:
cmds.deleteUI(data.TOOL_NAME, layout=True)
cmds.deleteUI(TOOL_NAME, layout=True)
except Exception as e:
print(f"Error deleting {data.TOOL_NAME} shelf: {e}")
print(f"Error deleting {TOOL_NAME} shelf: {e}")
self._clean_all_shelf_buttons()
# Remove from Python path
if data.SCRIPTS_PATH in sys.path:
sys.path.remove(data.SCRIPTS_PATH)
if SCRIPTS_PATH in sys.path:
sys.path.remove(SCRIPTS_PATH)
# Deleting Shelf Files
shelf_path = os.path.join(
@ -369,7 +405,7 @@ except ImportError as e:
cmds.about(version=True),
"prefs",
"shelves",
f"shelf_{data.TOOL_NAME}.mel"
f"shelf_{TOOL_NAME}.mel"
)
if os.path.exists(shelf_path):
@ -379,7 +415,7 @@ except ImportError as e:
print(f"Error deleting shelf file: {e}")
# If the current tool shelf is a deleted tool shelf, switch to another tool shelf
if current_shelf == data.TOOL_NAME:
if current_shelf == TOOL_NAME:
shelves = cmds.shelfTabLayout("ShelfLayout", query=True, childArray=True)
if shelves and len(shelves) > 0:
cmds.shelfTabLayout("ShelfLayout", edit=True, selectTab=shelves[0])
@ -393,34 +429,34 @@ except ImportError as e:
shelf_buttons = cmds.shelfLayout(shelf, query=True, childArray=True) or []
for btn in shelf_buttons:
if cmds.shelfButton(btn, query=True, exists=True):
if cmds.shelfButton(btn, query=True, label=True) == data.TOOL_NAME:
if cmds.shelfButton(btn, query=True, label=True) == TOOL_NAME:
cmds.deleteUI(btn)
def _show_uninstall_success_message(self):
"""Show uninstallation success message"""
msg_box = QtWidgets.QMessageBox(self)
msg_box.setWindowTitle("Uninstallation Successful")
msg_box.setText(f"{data.TOOL_NAME} has been successfully uninstalled!")
msg_box.setText(f"{TOOL_NAME} has been successfully uninstalled!")
msg_box.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg_box.setWindowIcon(QtGui.QIcon(data.TOOL_ICON))
msg_box.setWindowIcon(QtGui.QIcon(TOOL_ICON))
msg_box.setStyleSheet(self.styleSheet())
msg_box.exec_()
def _show_install_success_message(self):
msg_box = QtWidgets.QMessageBox(self)
msg_box.setWindowTitle("Installation Successful")
msg_box.setText(f"{data.TOOL_NAME} has been successfully installed!")
msg_box.setText(f"{TOOL_NAME} has been successfully installed!")
msg_box.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg_box.setWindowIcon(QtGui.QIcon(data.TOOL_ICON))
msg_box.setWindowIcon(QtGui.QIcon(TOOL_ICON))
msg_box.setStyleSheet(self.styleSheet())
msg_box.exec_()
def _validate_paths(self):
"""Validate all required paths exist"""
paths = {
"Root": data.TOOL_PATH,
"Scripts": data.SCRIPTS_PATH,
"Icons": data.ICONS_PATH
"Root": TOOL_PATH,
"Scripts": SCRIPTS_PATH,
"Icons": ICONS_PATH
}
for name, path in paths.items():
@ -443,7 +479,7 @@ except ImportError as e:
def _load_mel_shelf(self):
"""Load mel shelf file with error handling"""
try:
mel.eval(f'loadNewShelf "shelf_{data.TOOL_NAME}.mel"')
mel.eval(f'loadNewShelf "shelf_{TOOL_NAME}.mel"')
except Exception as e:
self._log(f"Error loading shelf file: {e}", error=True)

View File

@ -26,7 +26,6 @@ try:
except ImportError as e:
print(f"无法导入dna模块: {e}")
print(f"PYDNA_PATH: {PYDNA_PATH}")
print(f"sys.path: {sys.path}")
raise
# 现在导入其他模块

View File

@ -38,21 +38,21 @@ SYSTEM_OS = str("Windows" if cmds.about(os=True).lower().startswith("win") else
MAYA_VERSION = str(int(cmds.about(version=True).split('.')[0]))
# PYTHON_VERSION
PYTHON_VERSION = str(sys.version.replace(".", ""))
major_version = int(PYTHON_VERSION[0])
minor_version = int(PYTHON_VERSION[1:3]) if len(PYTHON_VERSION) > 1 else None
version_tuple = (major_version,) if minor_version is None else (major_version, minor_version)
PYTHON_VERSION = str(f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}")
# 版本映射表
PYTHON_VERSION_MAP = {
(3,): "python3", # 所有Python3主版本
(3, 9): "python397", # 3.9.x → python397
(3, 10): "python3108", # 3.10.x → python3108
(3, 11): "python311" # 3.11.x → python311
}
def get_python_version():
"""获取Python版本目录名称"""
version = sys.version_info
if version.major == 3:
if version.minor == 9 and version.micro == 7:
return "python397"
elif version.minor == 10 and version.micro == 8:
return "python3108"
elif version.minor == 11:
return "python311"
return "python3"
# 获取Python版本目录
PYTHON_VERSION_DIR = str(PYTHON_VERSION_MAP.get(version_tuple, "python397"))
PYTHON_VERSION_DIR = get_python_version()
# PATHS
PLUGIN_PATH = str(os.path.join(TOOL_PATH, "plugins", SYSTEM_OS, MAYA_VERSION).replace("\\", "/"))