execute_blender_code
Run custom Python scripts directly in Blender to manipulate models, automate tasks, or integrate with IFC building data through the Bonsai-mcp server. Streamline 3D workflows by executing code with precision.
Instructions
Execute arbitrary Python code in Blender.
Parameters:
- code: The Python code to execute
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes |
Implementation Reference
- tools.py:242-259 (handler)The core handler function for the 'execute_blender_code' MCP tool. It accepts a string of Python code, connects to the Blender server via a persistent socket connection, sends an 'execute_code' command with the code as parameter, and returns the result or error message from Blender.@mcp.tool() def execute_blender_code(ctx: Context, code: str) -> str: """ Execute arbitrary Python code in Blender. Parameters: - code: The Python code to execute """ try: # Get the global connection blender = get_blender_connection() result = blender.send_command("execute_code", {"code": code}) return f"Code executed successfully: {result.get('result', '')}" except Exception as e: logger.error(f"Error executing code: {str(e)}") return f"Error executing code: {str(e)}"