create_rotating_cube_blueprint.py•12.5 kB
#!/usr/bin/env python
"""
Example script to create a rotating cube Blueprint using MCP.
This script demonstrates how to use the Unreal MCP to create a Blueprint class
with components and logic that makes a cube rotate continuously.
"""
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("RotatingCubeExample")
def create_rotating_cube_blueprint():
"""Create a Blueprint class for a rotating cube.
This function creates a Blueprint with:
1. A static mesh component set to a cube
2. A rotation speed variable
3. Logic to rotate the cube every frame
Returns:
The response from the final operation, or None if there was an error.
"""
# Get the connection to Unreal Engine
conn = get_unreal_connection()
if not conn:
logger.error("Failed to connect to Unreal Engine")
return None
blueprint_name = "BP_RotatingCube"
blueprint_path = "/Game/Blueprints/BP_RotatingCube"
# Step 1: Create the Blueprint class
logger.info(f"Creating Blueprint class '{blueprint_name}'")
response = conn.send_command("create_blueprint", {
"name": blueprint_name,
"parent_class": "Actor",
"path": "/Game/Blueprints"
})
if response.get("status") == "error":
logger.error(f"Error creating Blueprint: {response.get('error')}")
return response
# Step 2: Add a static mesh component
logger.info("Adding static mesh component")
response = conn.send_command("add_component_to_blueprint", {
"blueprint_name": blueprint_path,
"component_type": "StaticMeshComponent",
"component_name": "CubeMesh",
"location": [0, 0, 0],
"rotation": [0, 0, 0],
"scale": [1, 1, 1]
})
if response.get("status") == "error":
logger.error(f"Error adding component: {response.get('error')}")
return response
# Step 3: Set the static mesh to a cube
logger.info("Setting static mesh to cube")
response = conn.send_command("set_static_mesh_properties", {
"blueprint_name": blueprint_path,
"component_name": "CubeMesh",
"static_mesh": "/Engine/BasicShapes/Cube.Cube",
"materials": ["/Engine/BasicShapes/BasicShapeMaterial.BasicShapeMaterial"]
})
if response.get("status") == "error":
logger.error(f"Error setting static mesh: {response.get('error')}")
return response
# Step 4: Add a rotation speed variable
logger.info("Adding rotation speed variable")
response = conn.send_command("add_blueprint_variable", {
"blueprint_name": blueprint_path,
"variable_name": "RotationSpeed",
"variable_type": "Float",
"default_value": 50.0,
"is_exposed": True,
"category": "Rotation",
"tooltip": "Speed at which the cube rotates (degrees per second)"
})
if response.get("status") == "error":
logger.error(f"Error adding variable: {response.get('error')}")
return response
# Step 5: Add BeginPlay event
logger.info("Adding BeginPlay event")
response = conn.send_command("add_blueprint_event_node", {
"blueprint_name": blueprint_path,
"event_type": "BeginPlay"
})
if response.get("status") == "error":
logger.error(f"Error adding BeginPlay event: {response.get('error')}")
return response
begin_play_node_id = response.get("node_id")
# Step 6: Add Tick event
logger.info("Adding Tick event")
response = conn.send_command("add_blueprint_event_node", {
"blueprint_name": blueprint_path,
"event_type": "Tick"
})
if response.get("status") == "error":
logger.error(f"Error adding Tick event: {response.get('error')}")
return response
tick_node_id = response.get("node_id")
# Step 7: Add Get RotationSpeed node
logger.info("Adding Get RotationSpeed node")
response = conn.send_command("add_blueprint_get_variable_node", {
"blueprint_name": blueprint_path,
"variable_name": "RotationSpeed"
})
if response.get("status") == "error":
logger.error(f"Error adding Get RotationSpeed node: {response.get('error')}")
return response
get_speed_node_id = response.get("node_id")
# Step 8: Add Get Delta Seconds node
logger.info("Adding Get Delta Seconds node")
response = conn.send_command("add_blueprint_function_node", {
"blueprint_name": blueprint_path,
"function_name": "GetWorldDeltaSeconds",
"target": "self"
})
if response.get("status") == "error":
logger.error(f"Error adding Get Delta Seconds node: {response.get('error')}")
return response
get_delta_node_id = response.get("node_id")
# Step 9: Add Multiply node (RotationSpeed * DeltaSeconds)
logger.info("Adding Multiply node")
response = conn.send_command("add_blueprint_function_node", {
"blueprint_name": blueprint_path,
"function_name": "Multiply_FloatFloat",
"category": "Math"
})
if response.get("status") == "error":
logger.error(f"Error adding Multiply node: {response.get('error')}")
return response
multiply_node_id = response.get("node_id")
# Step 10: Add Make Rotator node (0, 0, RotationAmount)
logger.info("Adding Make Rotator node")
response = conn.send_command("add_blueprint_function_node", {
"blueprint_name": blueprint_path,
"function_name": "MakeRotator",
"category": "Math|Rotator",
"inputs": {
"Roll": 0.0,
"Pitch": 0.0
# Yaw will be connected from the multiply node
}
})
if response.get("status") == "error":
logger.error(f"Error adding Make Rotator node: {response.get('error')}")
return response
make_rotator_node_id = response.get("node_id")
# Step 11: Add Get CubeMesh node
logger.info("Adding Get CubeMesh node")
response = conn.send_command("add_blueprint_get_self_component_reference", {
"blueprint_name": blueprint_path,
"component_name": "CubeMesh"
})
if response.get("status") == "error":
logger.error(f"Error adding Get CubeMesh node: {response.get('error')}")
return response
get_cube_node_id = response.get("node_id")
# Step 12: Add Add Relative Rotation node
logger.info("Adding Add Relative Rotation node")
response = conn.send_command("add_blueprint_function_node", {
"blueprint_name": blueprint_path,
"function_name": "AddRelativeRotation",
"target": "CubeMesh"
})
if response.get("status") == "error":
logger.error(f"Error adding Add Relative Rotation node: {response.get('error')}")
return response
add_rotation_node_id = response.get("node_id")
# Step 13: Connect the nodes
# Connect Tick -> Get Delta Seconds
logger.info("Connecting nodes")
response = conn.send_command("connect_blueprint_nodes", {
"blueprint_name": blueprint_path,
"source_node_id": tick_node_id,
"source_pin": "OutputDelegate",
"target_node_id": get_delta_node_id,
"target_pin": "execute"
})
if response.get("status") == "error":
logger.error(f"Error connecting nodes: {response.get('error')}")
return response
# Connect Get Delta Seconds -> Multiply
response = conn.send_command("connect_blueprint_nodes", {
"blueprint_name": blueprint_path,
"source_node_id": get_delta_node_id,
"source_pin": "ReturnValue",
"target_node_id": multiply_node_id,
"target_pin": "A"
})
if response.get("status") == "error":
logger.error(f"Error connecting nodes: {response.get('error')}")
return response
# Connect Get RotationSpeed -> Multiply
response = conn.send_command("connect_blueprint_nodes", {
"blueprint_name": blueprint_path,
"source_node_id": get_speed_node_id,
"source_pin": "ReturnValue",
"target_node_id": multiply_node_id,
"target_pin": "B"
})
if response.get("status") == "error":
logger.error(f"Error connecting nodes: {response.get('error')}")
return response
# Connect Multiply -> Make Rotator (Yaw)
response = conn.send_command("connect_blueprint_nodes", {
"blueprint_name": blueprint_path,
"source_node_id": multiply_node_id,
"source_pin": "ReturnValue",
"target_node_id": make_rotator_node_id,
"target_pin": "Yaw"
})
if response.get("status") == "error":
logger.error(f"Error connecting nodes: {response.get('error')}")
return response
# Connect Get Delta Seconds -> Get CubeMesh
response = conn.send_command("connect_blueprint_nodes", {
"blueprint_name": blueprint_path,
"source_node_id": get_delta_node_id,
"source_pin": "then",
"target_node_id": get_cube_node_id,
"target_pin": "execute"
})
if response.get("status") == "error":
logger.error(f"Error connecting nodes: {response.get('error')}")
return response
# Connect Get CubeMesh -> Add Relative Rotation
response = conn.send_command("connect_blueprint_nodes", {
"blueprint_name": blueprint_path,
"source_node_id": get_cube_node_id,
"source_pin": "ReturnValue",
"target_node_id": add_rotation_node_id,
"target_pin": "Target"
})
if response.get("status") == "error":
logger.error(f"Error connecting nodes: {response.get('error')}")
return response
# Connect Make Rotator -> Add Relative Rotation
response = conn.send_command("connect_blueprint_nodes", {
"blueprint_name": blueprint_path,
"source_node_id": make_rotator_node_id,
"source_pin": "ReturnValue",
"target_node_id": add_rotation_node_id,
"target_pin": "DeltaRotation"
})
if response.get("status") == "error":
logger.error(f"Error connecting nodes: {response.get('error')}")
return response
# Connect Get CubeMesh -> Add Relative Rotation (execution)
response = conn.send_command("connect_blueprint_nodes", {
"blueprint_name": blueprint_path,
"source_node_id": get_cube_node_id,
"source_pin": "then",
"target_node_id": add_rotation_node_id,
"target_pin": "execute"
})
if response.get("status") == "error":
logger.error(f"Error connecting nodes: {response.get('error')}")
return response
# Step 14: Compile the Blueprint
logger.info("Compiling Blueprint")
response = conn.send_command("compile_blueprint", {
"blueprint_name": blueprint_path
})
if response.get("status") == "error":
logger.error(f"Error compiling Blueprint: {response.get('error')}")
return response
# Step 15: Spawn an instance of the Blueprint in the level
logger.info("Spawning Blueprint instance")
response = conn.send_command("spawn_blueprint_actor", {
"blueprint_path": blueprint_path,
"actor_name": "RotatingCube_1",
"location": [0, 0, 100],
"rotation": [0, 0, 0],
"scale": [1, 1, 1]
})
if response.get("status") == "error":
logger.error(f"Error spawning Blueprint instance: {response.get('error')}")
return response
logger.info("Successfully created and spawned rotating cube Blueprint!")
return response
if __name__ == "__main__":
# Create the rotating cube Blueprint
result = create_rotating_cube_blueprint()
if result and result.get("status") != "error":
print("Successfully created and spawned a rotating cube Blueprint!")
print("The cube should now be rotating in the level.")
else:
print("Failed to create rotating cube Blueprint. Make sure Unreal Engine is running.")