Skip to main content
Glama

execute_python_script

Run Python scripts within Cinema 4D to access the c4d API for 3D modeling, animation, and scene manipulation tasks directly through code execution.

Instructions

Execute a Python script in Cinema 4D's Python environment.

This is the most reliable tool for non-trivial operations — it gives full access
to the c4d API and avoids wrapper/schema mismatches that can affect other tools.

Args:
    script: Python code to execute in Cinema 4D. Has access to `c4d` and
        `c4d.modules.mograph` modules.

Important usage notes:
    - For animated/MoGraph data, always call doc.ExecutePasses() after SetTime():
        doc.SetTime(c4d.BaseTime(frame, fps))
        doc.ExecutePasses(None, True, True, True, c4d.BUILDFLAGS_NONE)
    - For MoGraph/effector data, iterate frames sequentially (0..N) rather than
      jumping directly to a later frame — sequential stepping produces more
      faithful results.
    - Security restrictions block certain keywords: import os, subprocess, exec(, eval(.
      Keep scripts within the c4d API surface.
    - For heavy operations (dense frame loops, complex MoGraph scenes), split work
      into multiple smaller scripts rather than one large monolith.
    - Use print() to return results — output is captured and returned.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYes

Implementation Reference

  • The handler implementation for the `execute_python_script` tool, which sends a script to Cinema 4D via a socket connection and formats the response.
    async def execute_python_script(script: str, ctx: Context) -> str:
        """
        Execute a Python script in Cinema 4D's Python environment.
    
        This is the most reliable tool for non-trivial operations — it gives full access
        to the c4d API and avoids wrapper/schema mismatches that can affect other tools.
    
        Args:
            script: Python code to execute in Cinema 4D. Has access to `c4d` and
                `c4d.modules.mograph` modules.
    
        Important usage notes:
            - For animated/MoGraph data, always call doc.ExecutePasses() after SetTime():
                doc.SetTime(c4d.BaseTime(frame, fps))
                doc.ExecutePasses(None, True, True, True, c4d.BUILDFLAGS_NONE)
            - For MoGraph/effector data, iterate frames sequentially (0..N) rather than
              jumping directly to a later frame — sequential stepping produces more
              faithful results.
            - Security restrictions block certain keywords: import os, subprocess, exec(, eval(.
              Keep scripts within the c4d API surface.
            - For heavy operations (dense frame loops, complex MoGraph scenes), split work
              into multiple smaller scripts rather than one large monolith.
            - Use print() to return results — output is captured and returned.
        """
        async with c4d_connection_context() as connection:
            if not connection.connected:
                return "❌ Not connected to Cinema 4D"
    
            # Send command to Cinema 4D
            response = send_to_c4d(
                connection, {"command": "execute_python", "script": script}
            )
            return format_c4d_response(response, "execute_python")

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/ttiimmaacc/cinema4d-mcp'

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