添加 maya_mcp_plugin.py

This commit is contained in:
2025-04-16 19:50:49 +08:00
parent d1aae4273b
commit 54f2c8706f

119
maya_mcp_plugin.py Normal file
View File

@@ -0,0 +1,119 @@
"""
Maya MCP Plugin
This plugin provides integration with the Model Context Protocol for Maya.
To load this plugin:
1. Open Maya's Plugin Manager (Windows > Settings/Preferences > Plug-in Manager)
2. Click "Browse" and select this file
3. Check "Loaded" and "Auto load"
Version: 1.0.0
Author: Virtuos Games
"""
import sys
import os
import maya.api.OpenMaya as om
import maya.cmds as cmds
import traceback
# Plugin information
def maya_useNewAPI():
"""
Tell Maya this plugin uses the Python API 2.0
"""
pass
# Plugin initialization
def initializePlugin(plugin):
"""
Initialize the plugin
Args:
plugin: MObject used to register commands
"""
vendor = "Virtuos Games"
version = "1.0.0"
plugin_fn = om.MFnPlugin(plugin, vendor, version)
try:
# Add the plugin directory to the Python path
# Use plugin_fn.loadPath() instead of __file__ to get the plugin path
plugin_path = os.path.dirname(plugin_fn.loadPath())
om.MGlobal.displayInfo(f"Plugin path: {plugin_path}")
# Make sure the path is added to sys.path
if plugin_path not in sys.path:
sys.path.append(plugin_path)
om.MGlobal.displayInfo(f"Added to sys.path: {plugin_path}")
# Print current sys.path for debugging
om.MGlobal.displayInfo(f"Current sys.path: {sys.path}")
# Check if loader.py exists in the plugin directory
loader_path = os.path.join(plugin_path, "loader.py")
if os.path.exists(loader_path):
om.MGlobal.displayInfo(f"loader.py found at: {loader_path}")
else:
om.MGlobal.displayError(f"loader.py not found at: {loader_path}")
# Import and start MCP server
try:
# Try to import directly from the plugin path
sys.path.insert(0, plugin_path) # Add to the beginning of sys.path
# Try to import the module
om.MGlobal.displayInfo("Attempting to import loader module...")
from loader import auto_load
om.MGlobal.displayInfo("Successfully imported loader module")
# Start the server
auto_load()
om.MGlobal.displayInfo("MCP server loaded successfully")
except Exception as e:
om.MGlobal.displayError(f"Failed to load MCP server: {e}")
om.MGlobal.displayError(f"Traceback: {traceback.format_exc()}")
except Exception as e:
om.MGlobal.displayError(f"Error initializing MCP plugin: {e}")
raise
# Plugin cleanup
def uninitializePlugin(plugin):
"""
Uninitialize the plugin
Args:
plugin: MObject used to deregister commands
"""
try:
# Stop MCP server
try:
from server import stop_server
stop_server()
om.MGlobal.displayInfo("MCP server stopped")
except Exception as e:
om.MGlobal.displayWarning(f"Failed to stop MCP server: {e}")
# Remove menu - Try to use a more forceful way to delete menus
try:
# Try multiple possible menu names
for menu_name in ["MCPMenu", "MCP", "MCPMainMenu"]:
if cmds.menu(menu_name, exists=True):
cmds.deleteUI(menu_name, menu=True)
om.MGlobal.displayInfo(f"MCP menu '{menu_name}' removed")
# Check all menus in the main window, find menus containing MCP
main_window = "MayaWindow"
if cmds.window(main_window, exists=True):
menus = cmds.window(main_window, query=True, menuArray=True) or []
for menu in menus:
menu_label = cmds.menu(menu, query=True, label=True)
if menu_label and "MCP" in menu_label:
cmds.deleteUI(menu, menu=True)
om.MGlobal.displayInfo(f"Found and removed MCP menu with label: {menu_label}")
except Exception as e:
om.MGlobal.displayWarning(f"Failed to remove MCP menu: {e}")
except Exception as e:
om.MGlobal.displayError(f"Error uninitializing MCP plugin: {e}")
raise