277 lines
10 KiB
Plaintext
277 lines
10 KiB
Plaintext
// 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;
|
|
}
|
|
|
|
// Force create MCP menu
|
|
global proc forceMCPMenu() {
|
|
// Delete existing menu if it exists
|
|
if (`menu -exists "MCPMenu"`) {
|
|
deleteUI -menu "MCPMenu";
|
|
print("Removed existing MCP menu\n");
|
|
}
|
|
|
|
// Create new menu
|
|
string $mainWindow = "MayaWindow";
|
|
string $mcpMenu = `menu -label "MCP" -parent $mainWindow "MCPMenu"`;
|
|
|
|
// Add menu items
|
|
menuItem -label "Start Server" -command "python(\"import maya_mcp_plugin; maya_mcp_plugin.start_server_cmd()\")";
|
|
menuItem -label "Stop Server" -command "python(\"import maya_mcp_plugin; maya_mcp_plugin.stop_server_cmd()\")";
|
|
menuItem -label "Restart Server" -command "python(\"import maya_mcp_plugin; maya_mcp_plugin.restart_server_cmd()\")";
|
|
menuItem -divider true;
|
|
menuItem -label "Configure Port" -command "python(\"import maya_mcp_plugin; maya_mcp_plugin.configure_port_cmd()\")";
|
|
menuItem -divider true;
|
|
menuItem -label "About" -command "python(\"import maya_mcp_plugin; maya_mcp_plugin.about_cmd()\")";
|
|
|
|
print("MCP menu created successfully\n");
|
|
return;
|
|
}
|
|
|
|
// 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`) {
|
|
// If plugin is already loaded, unload it first
|
|
print("Plugin already loaded, unloading first...\n");
|
|
evalEcho("unloadPlugin \"" + $pluginPath + "\"");
|
|
print("Plugin unloaded\n");
|
|
}
|
|
|
|
// Load the plugin
|
|
evalEcho("loadPlugin \"" + $pluginPath + "\"");
|
|
print("Plugin loaded successfully\n");
|
|
|
|
// Force create MCP menu
|
|
print("Creating MCP menu...\n");
|
|
evalDeferred("forceMCPMenu()");
|
|
|
|
// 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`) {
|
|
// Try to remove MCP menu before unloading plugin
|
|
print("Removing MCP menu...\n");
|
|
if (`menu -exists "MCPMenu"`) {
|
|
deleteUI -menu "MCPMenu";
|
|
print("MCPMenu removed successfully\n");
|
|
} else {
|
|
// Try to find any menu containing MCP in its label
|
|
string $allMenus[] = `lsUI -menus`;
|
|
int $menuFound = 0;
|
|
for ($menu in $allMenus) {
|
|
if (`menu -q -exists $menu`) {
|
|
string $label = `menu -q -label $menu`;
|
|
if ($label == "MCP") {
|
|
deleteUI -menu $menu;
|
|
print("MCP menu found and removed: " + $menu + "\n");
|
|
$menuFound = 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$menuFound) {
|
|
print("No MCP menu found to remove\n");
|
|
}
|
|
}
|
|
|
|
// Now unload the plugin
|
|
evalEcho("unloadPlugin \"" + $pluginPath + "\"");
|
|
print("Plugin unloaded successfully\n");
|
|
} else {
|
|
print("Plugin not loaded, no need to unload\n");
|
|
|
|
// Still try to remove any MCP menu that might exist
|
|
if (`menu -exists "MCPMenu"`) {
|
|
deleteUI -menu "MCPMenu";
|
|
print("MCPMenu removed successfully\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 disabled from auto loading.")
|
|
-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();
|