Files
Nexus/2024/scripts/userSetup.py
2025-11-23 20:54:53 +08:00

201 lines
7.2 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Nexus Maya 2023 - User Setup Script
Automatically executed when Maya starts
"""
import maya.cmds as cmds
import maya.mel as mel
import maya.utils
import os
import sys
SHELF_NAMES = ["Nexus_Modeling", "Nexus_Rigging", "Nexus_Animation"]
def load_nexus_shelves():
"""Load all Nexus shelves (force refresh)"""
try:
shelf_paths = os.environ.get('MAYA_SHELF_PATH', '')
if not shelf_paths:
print("[Nexus] MAYA_SHELF_PATH not set, trying alternative method...")
try:
script_dir = os.path.dirname(os.path.abspath(__file__))
shelf_paths = os.path.join(os.path.dirname(script_dir), "shelves")
except:
print("[Nexus] Could not determine shelf path, skipping shelf load")
return
path_separator = ';' if os.name == 'nt' else ':'
shelf_path_list = shelf_paths.split(path_separator)
for shelf_name in SHELF_NAMES:
shelf_file_found = None
for shelf_path in shelf_path_list:
shelf_path = shelf_path.strip()
if not shelf_path:
continue
shelf_file = os.path.join(shelf_path, f"shelf_{shelf_name}.mel")
shelf_file = shelf_file.replace("\\", "/")
if os.path.exists(shelf_file):
shelf_file_found = shelf_file
print(f"[Nexus] Found shelf file: {shelf_file}")
break
if not shelf_file_found:
print(f"[Nexus] Could not find shelf_{shelf_name}.mel")
continue
# Delete old shelf if exists
if cmds.shelfLayout(shelf_name, exists=True):
print(f"[Nexus] Deleting old shelf: {shelf_name}")
try:
cmds.deleteUI(shelf_name, layout=True)
except Exception as e:
print(f"[Nexus] Warning: Could not delete old shelf: {e}")
# Load shelf using proper MEL method
print(f"[Nexus] Loading shelf: {shelf_name}")
try:
# Disable auto-save
mel.eval('optionVar -intValue "saveLastLoadedShelf" 0;')
# Create shelf layout
mel.eval(f'''
global string $gShelfTopLevel;
if (`shelfLayout -exists {shelf_name}`) {{
deleteUI -layout {shelf_name};
}}
setParent $gShelfTopLevel;
shelfLayout -cellWidth 35 -cellHeight 34 {shelf_name};
''')
print(f"[Nexus] ✓ Created shelf layout: {shelf_name}")
# Set parent and execute shelf script
mel.eval(f'setParent {shelf_name};')
mel.eval(f'source "{shelf_file_found}";')
mel.eval(f'shelf_{shelf_name}();')
print(f"[Nexus] ✓ Executed shelf script: shelf_{shelf_name}()")
# Verify shelf
if cmds.shelfLayout(shelf_name, exists=True):
buttons = cmds.shelfLayout(shelf_name, query=True, childArray=True) or []
if buttons:
print(f"[Nexus] ✓ Shelf loaded with {len(buttons)} button(s): {shelf_name}")
else:
print(f"[Nexus] ⚠ Shelf created but no buttons: {shelf_name}")
# Remove auto-saved config
try:
maya_version = cmds.about(version=True).split()[0]
maya_app_dir = os.environ.get('MAYA_APP_DIR', '')
if maya_app_dir:
shelf_config = os.path.join(maya_app_dir, maya_version, "prefs", "shelves", f"shelf_{shelf_name}.mel")
if os.path.exists(shelf_config):
os.remove(shelf_config)
print(f"[Nexus] ✓ Removed auto-saved config: {shelf_name}")
except Exception as e:
print(f"[Nexus] Warning: {e}")
else:
print(f"[Nexus] ✗ Shelf layout not created: {shelf_name}")
except Exception as e:
print(f"[Nexus] Error loading shelf {shelf_name}: {e}")
import traceback
traceback.print_exc()
continue
except Exception as e:
print(f"[Nexus] Error in load_nexus_shelves: {e}")
import traceback
traceback.print_exc()
def load_nexus_plugins():
"""Load Nexus plugins"""
try:
plugin_paths = os.environ.get('MAYA_PLUG_IN_PATH', '')
if not plugin_paths:
print("[Nexus] MAYA_PLUG_IN_PATH not set")
return
path_separator = ';' if os.name == 'nt' else ':'
plugin_path_list = plugin_paths.split(path_separator)
plugins_to_load = ["nexus_plugin.py"]
for plugin_name in plugins_to_load:
plugin_found = False
for plugin_path in plugin_path_list:
plugin_path = plugin_path.strip()
if not plugin_path:
continue
plugin_file = os.path.join(plugin_path, plugin_name)
if os.path.exists(plugin_file):
plugin_found = True
break
if not plugin_found:
print(f"[Nexus] Plugin not found: {plugin_name}")
continue
# Load plugin
try:
if not cmds.pluginInfo(plugin_name, query=True, loaded=True):
cmds.loadPlugin(plugin_name)
print(f"[Nexus] ✓ Plugin loaded: {plugin_name}")
else:
print(f"[Nexus] Plugin already loaded: {plugin_name}")
except Exception as e:
print(f"[Nexus] Error loading plugin {plugin_name}: {e}")
except Exception as e:
print(f"[Nexus] Error in load_nexus_plugins: {e}")
import traceback
traceback.print_exc()
# Deferred execution
def initialize_nexus():
"""Initialize Nexus plugin system"""
print("=" * 80)
print("[Nexus] Initializing Nexus Maya 2023 Plugin System...")
print("=" * 80)
load_nexus_shelves()
load_nexus_plugins()
print("=" * 80)
print("[Nexus] Nexus Plugin System Initialized!")
print("=" * 80)
# Execute after Maya is fully loaded
maya.utils.executeDeferred(initialize_nexus)
# Cleanup on exit
def cleanup_on_exit():
"""Cleanup when Maya exits"""
print("[Nexus] Cleaning up...")
# Unload plugins
plugins_to_unload = ["nexus_plugin.py"]
for plugin_name in plugins_to_unload:
if cmds.pluginInfo(plugin_name, query=True, loaded=True):
try:
cmds.unloadPlugin(plugin_name)
print(f"[Nexus] Plugin unloaded: {plugin_name}")
except:
pass
import atexit
atexit.register(cleanup_on_exit)