添加 install.mel

This commit is contained in:
2025-04-16 19:56:42 +08:00
parent 13796de9cd
commit 559fbf0798

210
install.mel Normal file
View File

@@ -0,0 +1,210 @@
// Maya MCP Installation Script
// Drag this file into Maya viewport to install or uninstall the plugin
// Get plugin directory and paths
global proc string[] getMCPPaths() {
// Get Maya script directory
string $userScriptDir = `internalVar -userScriptDir`;
// Ensure path ends with slash
if (!endsWith($userScriptDir, "/") && !endsWith($userScriptDir, "\\")) {
$userScriptDir = $userScriptDir + "/";
}
// Get current script directory (plugin directory)
string $scriptPath = `whatIs "getMCPPaths"`;
string $pluginDir = `substring $scriptPath 25 (size($scriptPath) - 1)`;
$pluginDir = `dirname $pluginDir`;
// Normalize path format, use forward slash
$pluginDir = substituteAllString($pluginDir, "\\", "/");
// Get plugin path
string $pluginPath = $pluginDir + "/maya_mcp_plugin.py";
string $paths[3] = {$userScriptDir, $pluginDir, $pluginPath};
return $paths;
}
// Install MCP plugin
global proc installMCPPlugin() {
string $paths[] = `getMCPPaths`;
string $userScriptDir = $paths[0];
string $pluginDir = $paths[1];
string $pluginPath = $paths[2];
print("=== Maya MCP Installation Started ===\n");
print("Plugin directory: " + $pluginDir + "\n");
print("Maya script directory: " + $userScriptDir + "\n");
print("Plugin path: " + $pluginPath + "\n");
// Create userSetup.py file
string $userSetupContent = "#!/usr/bin/env python\n" +
"# -*- coding: utf-8 -*-\n\n" +
"# Maya MCP Auto-Load Script\n" +
"# This script automatically loads the MCP server when Maya starts.\n\n" +
"import os\n" +
"import sys\n" +
"import maya.utils as utils\n" +
"import logging\n" +
"import traceback\n" +
"import maya.cmds as cmds\n\n" +
"# Configure logging\n" +
"logging.basicConfig(level=logging.INFO, \n" +
" format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n" +
"logger = logging.getLogger(\"MayaMCP.AutoLoad\")\n\n" +
"def initialize_mcp():\n" +
" \"\"\"Initialize MCP plugin\"\"\"\n" +
" try:\n" +
" # Set MCP directory path\n" +
" mcp_dir = r\"" + encodeString($pluginDir) + "\"\n\n" +
" # Add the MCP directory to the Python path if not already there\n" +
" if mcp_dir not in sys.path:\n" +
" sys.path.append(mcp_dir)\n" +
" logger.info(f\"Added {mcp_dir} to Python path\")\n\n" +
" # Load the plugin\n" +
" plugin_path = os.path.join(mcp_dir, \"maya_mcp_plugin.py\")\n" +
" if not cmds.pluginInfo(plugin_path, query=True, loaded=True):\n" +
" try:\n" +
" cmds.loadPlugin(plugin_path)\n" +
" logger.info(f\"Loaded MCP plugin: {plugin_path}\")\n" +
" except Exception as e:\n" +
" logger.error(f\"Error loading MCP plugin: {e}\")\n" +
" logger.error(traceback.format_exc())\n" +
" except Exception as e:\n" +
" logger.error(f\"Error initializing MCP: {e}\")\n" +
" logger.error(traceback.format_exc())\n\n" +
"# Use Maya's deferred execution mechanism to ensure Maya is fully started\n" +
"utils.executeDeferred(initialize_mcp)";
// Write userSetup.py to Maya script directory
int $fileId = `fopen ($userScriptDir + "userSetup.py") "w"`;
if ($fileId == 0) {
error ("Cannot open file for writing: " + $userScriptDir + "userSetup.py\n");
} else {
fprint $fileId $userSetupContent;
fclose $fileId;
print("Created userSetup.py in " + $userScriptDir + "\n");
}
// Load the plugin
print("Loading plugin...\n");
if (!`pluginInfo -q -loaded $pluginPath`) {
evalEcho("loadPlugin \"" + $pluginPath + "\"");
print("Plugin loaded successfully\n");
} else {
print("Plugin already loaded\n");
}
// Set plugin to auto load
print("Setting plugin to auto load...\n");
evalEcho("pluginInfo -edit -autoload true \"" + $pluginPath + "\"");
print("Plugin set to auto load\n");
// Installation complete
print("=== Maya MCP Installation Completed ===\n");
// Show success dialog
confirmDialog -title "MCP Installation Successful"
-message ("Maya MCP has been successfully installed!\n\nThe plugin has been loaded and set to auto load on Maya startup.\n\nPlugin location:\n" + $pluginPath)
-button "OK"
-defaultButton "OK";
}
// Uninstall MCP plugin
global proc uninstallMCPPlugin() {
string $paths[] = `getMCPPaths`;
string $userScriptDir = $paths[0];
string $pluginDir = $paths[1];
string $pluginPath = $paths[2];
print("=== Maya MCP Uninstallation Started ===\n");
print("Plugin path: " + $pluginPath + "\n");
// Unload plugin
print("Unloading plugin...\n");
if (`pluginInfo -q -loaded $pluginPath`) {
evalEcho("unloadPlugin \"" + $pluginPath + "\"");
print("Plugin unloaded successfully\n");
} else {
print("Plugin not loaded, no need to unload\n");
}
// Disable auto load
print("Disabling plugin auto load...\n");
evalEcho("pluginInfo -edit -autoload false \"" + $pluginPath + "\"");
print("Plugin auto load disabled\n");
// Delete userSetup.py file
string $userSetupPath = $userScriptDir + "userSetup.py";
print("Checking userSetup.py file...\n");
// Read file content to check if it contains MCP related content
int $fileId = `fopen $userSetupPath "r"`;
string $fileContent = "";
if ($fileId != 0) {
string $line = `fgetline $fileId`;
while (size($line) > 0) {
$fileContent += $line;
$line = `fgetline $fileId`;
}
fclose $fileId;
// If file contains MCP related content, delete it
if (`gmatch $fileContent "*MayaMCP*"`) {
if (`sysFile -delete $userSetupPath`) {
print("Deleted userSetup.py file\n");
} else {
print("Cannot delete userSetup.py file\n");
}
} else {
print("userSetup.py file does not contain MCP related content, keeping the file\n");
}
} else {
print("userSetup.py file does not exist\n");
}
// Uninstallation complete
print("=== Maya MCP Uninstallation Completed ===\n");
// Show success dialog
confirmDialog -title "MCP Uninstallation Successful"
-message ("Maya MCP has been successfully uninstalled!\n\nThe plugin has been unloaded and auto load disabled.")
-button "OK"
-defaultButton "OK";
}
// Main function - Show install/uninstall selection dialog
global proc mcpInstallMain() {
string $paths[] = `getMCPPaths`;
string $pluginPath = $paths[2];
// Check if plugin is already installed
int $isInstalled = 0;
if (`pluginInfo -q -registered $pluginPath`) {
$isInstalled = 1;
}
// Show selection dialog
string $message = "Please select an operation:";
if ($isInstalled) {
$message = "MCP plugin is already installed. Please select an operation:";
} else {
$message = "MCP plugin is not installed. Please select an operation:";
}
string $result = `confirmDialog -title "Maya MCP Install/Uninstall"
-message $message
-button "Install"
-button "Uninstall"
-button "Cancel"
-defaultButton "Install"
-cancelButton "Cancel"`;
if ($result == "Install") {
installMCPPlugin();
} else if ($result == "Uninstall") {
uninstallMCPPlugin();
}
}
// Execute main function
mcpInstallMain();