Update
This commit is contained in:
222
template_plugins/maya/2023/scripts/userSetup.py
Normal file
222
template_plugins/maya/2023/scripts/userSetup.py
Normal file
@@ -0,0 +1,222 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Maya Startup Script
|
||||
Automatically executed when Maya starts
|
||||
"""
|
||||
import maya.cmds as cmds
|
||||
import maya.mel as mel
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def load_nexus_shelf():
|
||||
"""Load NexusLauncher shelf (force refresh)"""
|
||||
try:
|
||||
# Get shelf path from environment variable (may contain multiple paths separated by semicolons)
|
||||
shelf_paths = os.environ.get('MAYA_SHELF_PATH', '')
|
||||
|
||||
if not shelf_paths:
|
||||
print("[NexusLauncher] MAYA_SHELF_PATH not set, trying alternative method...")
|
||||
# Fallback method: infer from current script directory
|
||||
try:
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
shelf_paths = os.path.join(os.path.dirname(script_dir), "shelves")
|
||||
except:
|
||||
print("[NexusLauncher] Could not determine shelf path, skipping shelf load")
|
||||
return
|
||||
|
||||
# Split multiple paths (Windows uses semicolon, Unix uses colon)
|
||||
path_separator = ';' if os.name == 'nt' else ':'
|
||||
shelf_path_list = shelf_paths.split(path_separator)
|
||||
|
||||
# First check if shelf file exists
|
||||
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, "shelf_NexusLauncher.mel")
|
||||
shelf_file = shelf_file.replace("\\", "/")
|
||||
|
||||
print(f"[NexusLauncher] Checking: {shelf_file}")
|
||||
|
||||
if os.path.exists(shelf_file):
|
||||
shelf_file_found = shelf_file
|
||||
print(f"[NexusLauncher] ✓ Found shelf file: {shelf_file}")
|
||||
break
|
||||
|
||||
# If shelf file not found, skip loading
|
||||
if not shelf_file_found:
|
||||
print("[NexusLauncher] ✗ Could not find shelf_NexusLauncher.mel in any MAYA_SHELF_PATH")
|
||||
print("[NexusLauncher] Skipping shelf load (no file found)")
|
||||
return
|
||||
|
||||
# After finding shelf file, delete old one before loading new one
|
||||
# Note: loadNewShelf does not automatically delete shelf with same name, must delete manually
|
||||
existing_shelves = cmds.lsUI(type='shelfLayout') or []
|
||||
if 'NexusLauncher' in existing_shelves:
|
||||
print("[NexusLauncher] Removing existing shelf before reload...")
|
||||
try:
|
||||
cmds.deleteUI('NexusLauncher', layout=True)
|
||||
print("[NexusLauncher] ✓ Removed old shelf")
|
||||
except Exception as e:
|
||||
print(f"[NexusLauncher] Warning: Could not remove old shelf: {e}")
|
||||
|
||||
# Key: Use custom method to load shelf, avoid loadNewShelf auto-save
|
||||
print(f"[NexusLauncher] Loading shelf from: {shelf_file_found}")
|
||||
|
||||
# Method: Use mel.eval to execute shelf file, then immediately delete saved config
|
||||
try:
|
||||
# 1. First disable auto-save (attempt)
|
||||
mel.eval('optionVar -intValue "saveLastLoadedShelf" 0;')
|
||||
|
||||
# 2. Create shelf layout
|
||||
mel.eval('''
|
||||
global string $gShelfTopLevel;
|
||||
if (`shelfLayout -exists NexusLauncher`) {
|
||||
deleteUI -layout NexusLauncher;
|
||||
}
|
||||
setParent $gShelfTopLevel;
|
||||
shelfLayout -cellWidth 35 -cellHeight 34 NexusLauncher;
|
||||
''')
|
||||
print(f"[NexusLauncher] ✓ Created shelf layout")
|
||||
|
||||
# 3. Set parent to shelf, then execute shelf file to create buttons
|
||||
mel.eval('setParent NexusLauncher;')
|
||||
mel.eval(f'source "{shelf_file_found}";')
|
||||
mel.eval('shelf_NexusLauncher();')
|
||||
print(f"[NexusLauncher] ✓ Executed shelf script")
|
||||
|
||||
# 4. Verify shelf
|
||||
if cmds.shelfLayout('NexusLauncher', exists=True):
|
||||
new_buttons = cmds.shelfLayout('NexusLauncher', query=True, childArray=True) or []
|
||||
if new_buttons:
|
||||
print(f"[NexusLauncher] ✓ Shelf loaded successfully with {len(new_buttons)} button(s)")
|
||||
|
||||
# 5. Ensure no config file is saved
|
||||
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", "shelf_NexusLauncher.mel")
|
||||
if os.path.exists(shelf_config):
|
||||
os.remove(shelf_config)
|
||||
print(f"[NexusLauncher] ✓ Removed auto-saved config")
|
||||
except Exception as e:
|
||||
print(f"[NexusLauncher] Warning: {e}")
|
||||
|
||||
print(f"[NexusLauncher] ✓ Shelf is temporary (won't be saved)")
|
||||
else:
|
||||
print("[NexusLauncher] ⚠ Warning: Shelf has no buttons!")
|
||||
else:
|
||||
print("[NexusLauncher] ✗ Error: Shelf failed to load")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[NexusLauncher] Error loading shelf: {e}")
|
||||
import traceback
|
||||
print(traceback.format_exc())
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print(f"[NexusLauncher] Failed to load shelf: {e}")
|
||||
print(traceback.format_exc())
|
||||
|
||||
|
||||
def load_nexus_plugins():
|
||||
"""Load NexusLauncher plugins"""
|
||||
try:
|
||||
# Get plugin path
|
||||
plugin_path = os.environ.get('MAYA_PLUG_IN_PATH', '')
|
||||
|
||||
if not plugin_path:
|
||||
print("[NexusLauncher] MAYA_PLUG_IN_PATH not set, skipping plugin load")
|
||||
return
|
||||
|
||||
print(f"[NexusLauncher] MAYA_PLUG_IN_PATH: {plugin_path}")
|
||||
|
||||
# Load example plugin
|
||||
plugin_file = "nexus_example_plugin.py"
|
||||
|
||||
if cmds.pluginInfo(plugin_file, query=True, loaded=True):
|
||||
print(f"[NexusLauncher] Plugin already loaded: {plugin_file}")
|
||||
else:
|
||||
try:
|
||||
cmds.loadPlugin(plugin_file)
|
||||
print(f"[NexusLauncher] ✓ Loaded plugin: {plugin_file}")
|
||||
|
||||
# Set to auto-load
|
||||
cmds.pluginInfo(plugin_file, edit=True, autoload=True)
|
||||
print(f"[NexusLauncher] ✓ Set plugin to auto-load")
|
||||
except Exception as e:
|
||||
print(f"[NexusLauncher] Failed to load plugin {plugin_file}: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[NexusLauncher] Error loading plugins: {e}")
|
||||
|
||||
|
||||
def print_environment():
|
||||
"""Print environment variables (for debugging)"""
|
||||
print("=" * 60)
|
||||
print("[NexusLauncher] Environment Variables:")
|
||||
print(f" MAYA_SHELF_PATH: {os.environ.get('MAYA_SHELF_PATH', 'Not set')}")
|
||||
print(f" MAYA_SCRIPT_PATH: {os.environ.get('MAYA_SCRIPT_PATH', 'Not set')}")
|
||||
print(f" PYTHONPATH: {os.environ.get('PYTHONPATH', 'Not set')}")
|
||||
print(f" MAYA_PLUG_IN_PATH: {os.environ.get('MAYA_PLUG_IN_PATH', 'Not set')}")
|
||||
print(f" XBMLANGPATH: {os.environ.get('XBMLANGPATH', 'Not set')}")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
def cleanup_on_exit():
|
||||
"""Clean up NexusLauncher shelf config file when Maya exits"""
|
||||
try:
|
||||
# Check if launched by NexusLauncher
|
||||
shelf_paths = os.environ.get('MAYA_SHELF_PATH', '')
|
||||
|
||||
is_nexus_launcher = False
|
||||
if shelf_paths:
|
||||
path_separator = ';' if os.name == 'nt' else ':'
|
||||
for shelf_path in shelf_paths.split(path_separator):
|
||||
shelf_file = os.path.join(shelf_path.strip(), "shelf_NexusLauncher.mel")
|
||||
if os.path.exists(shelf_file):
|
||||
is_nexus_launcher = True
|
||||
break
|
||||
|
||||
if not is_nexus_launcher:
|
||||
return
|
||||
|
||||
# Delete config file (if exists)
|
||||
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", "shelf_NexusLauncher.mel")
|
||||
if os.path.exists(shelf_config):
|
||||
os.remove(shelf_config)
|
||||
print(f"[NexusLauncher] ✓ Cleaned up shelf config on exit")
|
||||
except Exception as e:
|
||||
print(f"[NexusLauncher] Warning: Could not clean up shelf config: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[NexusLauncher] Error during cleanup: {e}")
|
||||
|
||||
|
||||
def register_exit_callback():
|
||||
"""Register cleanup callback on exit"""
|
||||
try:
|
||||
# Use scriptJob to execute cleanup when Maya exits
|
||||
cmds.scriptJob(event=["quitApplication", cleanup_on_exit], protected=True)
|
||||
print("[NexusLauncher] ✓ Registered exit cleanup callback")
|
||||
except Exception as e:
|
||||
print(f"[NexusLauncher] Warning: Could not register exit callback: {e}")
|
||||
|
||||
|
||||
# Execute after Maya startup completes
|
||||
cmds.evalDeferred(load_nexus_shelf)
|
||||
cmds.evalDeferred(load_nexus_plugins)
|
||||
cmds.evalDeferred(print_environment)
|
||||
cmds.evalDeferred(register_exit_callback)
|
||||
|
||||
print("[NexusLauncher] userSetup.py executed")
|
||||
Reference in New Issue
Block a user