Skip to main content
Glama
mikeysrecipes

BlenderMCP

execute_blender_code

Execute Python code directly in Blender to automate 3D modeling tasks, create scenes, and manipulate objects through code-driven commands.

Instructions

Execute arbitrary Python code in Blender. Make sure to do it step-by-step by breaking it into smaller chunks.

Parameters:

  • code: The Python code to execute

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYes

Implementation Reference

  • The handler function for the 'execute_blender_code' tool. It sends the provided Python code to the Blender addon via socket connection for execution and returns the result or error message.
    @mcp.tool()
    def execute_blender_code(ctx: Context, code: str) -> str:
        """
        Execute arbitrary Python code in Blender. Make sure to do it step-by-step by breaking it into smaller chunks.
        
        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)}"
  • The @mcp.tool() decorator registers the execute_blender_code function as an MCP tool.
    @mcp.tool()
  • The send_command method of BlenderConnection class, used by the handler to communicate the code to Blender.
    def send_command(self, command_type: str, params: Dict[str, Any] = None) -> Dict[str, Any]:
        """Send a command to Blender and return the response"""
        if not self.sock and not self.connect():
            raise ConnectionError("Not connected to Blender")
        
        command = {
            "type": command_type,
            "params": params or {}
        }
        
        try:
            # Log the command being sent
            logger.info(f"Sending command: {command_type} with params: {params}")
            
            # Send the command
            self.sock.sendall(json.dumps(command).encode('utf-8'))
            logger.info(f"Command sent, waiting for response...")
            
            # Set a timeout for receiving - use the same timeout as in receive_full_response
            self.sock.settimeout(15.0)  # Match the addon's timeout
            
            # Receive the response using the improved receive_full_response method
            response_data = self.receive_full_response(self.sock)
            logger.info(f"Received {len(response_data)} bytes of data")
            
            response = json.loads(response_data.decode('utf-8'))
            logger.info(f"Response parsed, status: {response.get('status', 'unknown')}")
            
            if response.get("status") == "error":
                logger.error(f"Blender error: {response.get('message')}")
                raise Exception(response.get("message", "Unknown error from Blender"))
            
            return response.get("result", {})
        except socket.timeout:
            logger.error("Socket timeout while waiting for response from Blender")
            # Don't try to reconnect here - let the get_blender_connection handle reconnection
            # Just invalidate the current socket so it will be recreated next time
            self.sock = None
            raise Exception("Timeout waiting for Blender response - try simplifying your request")
        except (ConnectionError, BrokenPipeError, ConnectionResetError) as e:
            logger.error(f"Socket connection error: {str(e)}")
            self.sock = None
            raise Exception(f"Connection to Blender lost: {str(e)}")
        except json.JSONDecodeError as e:
            logger.error(f"Invalid JSON response from Blender: {str(e)}")
            # Try to log what was received
            if 'response_data' in locals() and response_data:
                logger.error(f"Raw response (first 200 bytes): {response_data[:200]}")
            raise Exception(f"Invalid response from Blender: {str(e)}")
        except Exception as e:
            logger.error(f"Error communicating with Blender: {str(e)}")
            # Don't try to reconnect here - let the get_blender_connection handle reconnection
            self.sock = None
            raise Exception(f"Communication error with Blender: {str(e)}")
  • Helper function to get or create the persistent socket connection to Blender, called by the handler.
    def get_blender_connection():
        """Get or create a persistent Blender connection"""
        global _blender_connection, _polyhaven_enabled  # Add _polyhaven_enabled to globals
        
        # If we have an existing connection, check if it's still valid
        if _blender_connection is not None:
            try:
                # First check if PolyHaven is enabled by sending a ping command
                result = _blender_connection.send_command("get_polyhaven_status")
                # Store the PolyHaven status globally
                _polyhaven_enabled = result.get("enabled", False)
                return _blender_connection
            except Exception as e:
                # Connection is dead, close it and create a new one
                logger.warning(f"Existing connection is no longer valid: {str(e)}")
                try:
                    _blender_connection.disconnect()
                except:
                    pass
                _blender_connection = None
        
        # Create a new connection if needed
        if _blender_connection is None:
            _blender_connection = BlenderConnection(host="localhost", port=9876)
            if not _blender_connection.connect():
                logger.error("Failed to connect to Blender")
                _blender_connection = None
                raise Exception("Could not connect to Blender. Make sure the Blender addon is running.")
            logger.info("Created new persistent connection to Blender")
        
        return _blender_connection
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It mentions executing code 'step-by-step' and 'breaking it into smaller chunks', which adds some behavioral context about execution style. However, it fails to disclose critical traits like whether this is a read-only or destructive operation, potential security implications, error handling, or output format, leaving significant gaps for a tool that executes arbitrary code.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized with two sentences and a parameter list, front-loaded with the main purpose. Every sentence earns its place, but the structure could be slightly improved by integrating the parameter note more seamlessly or adding brief formatting hints.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of executing arbitrary code, no annotations, no output schema, and low schema coverage, the description is incomplete. It lacks details on safety, permissions, return values, or error cases, making it inadequate for such a powerful and potentially risky tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the schema provides no parameter details. The description adds minimal semantics by stating 'code: The Python code to execute', which clarifies the parameter's purpose. However, it doesn't compensate fully for the coverage gap—e.g., by explaining code format, constraints, or examples—resulting in a baseline score due to the single parameter.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb 'Execute' and the resource 'arbitrary Python code in Blender', making the purpose specific and understandable. However, it doesn't explicitly differentiate from sibling tools like 'get_object_info' or 'set_texture', which are more specific operations rather than general code execution.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides implied usage guidance by suggesting to 'break it into smaller chunks', which hints at when to use this tool for complex operations. However, it lacks explicit when-to-use vs. alternatives, such as when to use this versus more specific tools like 'import_generated_asset' for asset handling.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

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/mikeysrecipes/blender-mcp'

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