Skip to main content
Glama

Unreal Engine MCP Bridge

by gingerol
add_cube.py3.67 kB
#!/usr/bin/env python """ Example script to add a cube to the Unreal Engine scene using MCP. This script demonstrates how to use the Unreal MCP to spawn a simple cube actor in the current level. """ import logging import sys import os # Add the parent directory to the path so we can import the unreal_mcp_server module sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from unreal_mcp_server import get_unreal_connection # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.StreamHandler(sys.stdout) ] ) logger = logging.getLogger("AddCubeExample") def add_cube(name="MCP_Cube", location=None, color=None): """Add a cube to the Unreal Engine scene. Args: name: The name to give the cube actor. location: Optional [X, Y, Z] location for the cube. Defaults to [0, 0, 100]. color: Optional [R, G, B, A] color for the cube. Defaults to red [1, 0, 0, 1]. Returns: The response from Unreal Engine, or None if there was an error. """ # Default values if location is None: location = [0, 0, 100] # 100 units above the origin if color is None: color = [1, 0, 0, 1] # Red # Get the connection to Unreal Engine conn = get_unreal_connection() if not conn: logger.error("Failed to connect to Unreal Engine") return None # First, spawn a StaticMeshActor logger.info(f"Spawning cube actor '{name}' at location {location}") response = conn.send_command("spawn_actor", { "name": name, "type": "StaticMeshActor", "location": location, "rotation": [0, 0, 0], "scale": [1, 1, 1] }) if response.get("status") == "error": logger.error(f"Error spawning actor: {response.get('error')}") return response # Set the static mesh to a cube logger.info("Setting static mesh to cube") response = conn.send_command("set_actor_property", { "name": name, "property": "StaticMeshComponent.StaticMesh", "value": "/Engine/BasicShapes/Cube.Cube" }) if response.get("status") == "error": logger.error(f"Error setting static mesh: {response.get('error')}") return response # Create a dynamic material instance and set its color logger.info(f"Setting cube color to {color}") response = conn.send_command("set_actor_property", { "name": name, "property": "StaticMeshComponent.Material", "value": { "type": "MaterialInstanceDynamic", "parent": "/Engine/BasicShapes/BasicShapeMaterial.BasicShapeMaterial", "parameters": { "Color": color } } }) if response.get("status") == "error": logger.error(f"Error setting material: {response.get('error')}") return response logger.info(f"Successfully created cube '{name}'") return response if __name__ == "__main__": # Example usage result = add_cube("MCP_RedCube", [0, 0, 100], [1, 0, 0, 1]) # Red cube if result and result.get("status") != "error": print("Successfully added a red cube to the scene!") else: print("Failed to add cube. Make sure Unreal Engine is running.") # Add a few more cubes with different colors and positions add_cube("MCP_GreenCube", [200, 0, 100], [0, 1, 0, 1]) # Green cube add_cube("MCP_BlueCube", [0, 200, 100], [0, 0, 1, 1]) # Blue cube add_cube("MCP_YellowCube", [200, 200, 100], [1, 1, 0, 1]) # Yellow cube

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/gingerol/UnrealEngine-ai-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server