execute_python
Run Python code against a live Autodesk Inventor session to build parametric 3D parts, sheet metal, and assemblies via the COM API, with persistent variables and undo-on-error rollback.
Instructions
Execute Python code against the live Inventor connection.
Pre-bound objects (re-resolved fresh on every call): inv — InventorConnection wrapper (all high-level methods, mm units) app — Inventor.Application COM object doc — active document comp — its ComponentDefinition body — comp.SurfaceBodies.Item(1) (or None) tg — TransientGeometry math — math module progress(msg) — status line call_status shows while a long job runs cancelled() — True once cancel_call asked to stop: poll it in long loops Variables you assign persist between calls until the server restarts or reload_api runs — status shows the namespace's age and names. Each connected Inventor has its own namespace. Helpers you need every time: put them in a file and load_library it (or in ~/.inventor-mcp/startup.py) — they come back by themselves in a new namespace. Internal COM units are CENTIMETERS — divide mm by 10. Objects are late-bound: a Document from Documents.Open exposes ComponentDefinition directly. Don't use CastTo / gencache.EnsureDispatch (they create a gen_py cache). Runs with SilentOperation on, so Inventor answers its own prompts instead of opening a dialog. A call longer than the wait limit keeps running; follow it with call_status(wait_seconds=30), which shows the prints so far; cancel_call stops it. Returns captured stdout + stderr (print your results!), or the traceback from your own code down on error (COM errors named: E_INVALIDARG, RPC_E_DISCONNECTED …).
Args: code: Python source. undo_on_error: Run inside one undo transaction on the active document and roll it back if the code raises — no half-built sketches or work planes left behind. timeout: Seconds; then cancelled() turns True, and the run is interrupted CANCEL_GRACE s later if it hasn't stopped — as cancel_call does (0 = none).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| timeout | No | ||
| instance | No | Which connected Inventor to use (list_instances). Leave empty when only one is connected. | |
| undo_on_error | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |