itasca_execute_code
Execute Python code synchronously inside a running ITASCA engine to query model state, tune parameters, and export plots live while a simulation task cycles.
Instructions
Execute Python code synchronously in the running Itasca engine process.
Returns stdout and an optional result variable immediately. Code runs in the engine's main thread, sharing the same main namespace as any running task — side effects persist and are immediately visible to the task on its next cycle.
The tool stays responsive while a task submitted via itasca_execute_task is cycling (calls interleave at cycle gaps), so use it as a live REPL — nothing has to be pre-scripted into the task up front. Typical uses:
Query model state and read Itasca command output: itasca.command('ball list') etc. — table dumps, list output, and command summaries are captured and interleaved with Python prints in execution order
Live inspection and tuning during a running task: check forces or contact statistics, modify parameters, swap callbacks, set sentinel variables the task reads each cycle
Create and export plots: itasca.command('plot ...')
Environment: the engine's embedded Python interpreter (Itasca 9+ → Python 3.10, pre-9 → Python 3.6 or older; the product+version is encoded in sys.executable, e.g. PFC900). When unsure, check sys.version_info before relying on newer syntax.
Code-writing rules (shared with itasca_execute_task):
Multi-line itasca.command("""...""") batches are normalized to one engine call per command, keeping the bridge reachable mid-batch. Normalization recognizes itasca.command through its import name; call it through that name directly — rebinding via intermediate variables (
_it = itasca) bypasses it and the output carries a bridge warning.Pass each FISH definition block (
fish define/fish operator/ legacy baredefine, through its terminating standaloneend) whole, in ONE itasca.command() string; feeding one line-by-line leaves the engine blocked in interactive FISH mode until completed manually in the GUI console. Per-line loops are fine for ordinary commands.program call '<file>'(.p3dat / .p2dat / .dat / ...) is supported: the bridge runs the file inline, one command per engine call, so the bridge stays reachable, the run stays interruptible, and the file's output arrives command by command.
Synchronous semantics: the request blocks until the code finishes or hits the timeout (default 10s, max 20s), and output is returned in full. The call is not tracked by itasca_list_tasks and cannot be interrupted mid-execution. For cancellable, pollable, or background work, submit via itasca_execute_task instead.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Python code to execute in the Itasca engine user console | |
| timeout | No | Seconds before the bridge aborts the snippet; longer work belongs in itasca_execute_task. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||