execute_python
Run Python code inside the running Unreal Editor using the unreal API to execute scripts, evaluate expressions, and inspect results, outputs, and engine logs for editor automation and debugging.
Instructions
Run Python in the running Unreal Editor, with Unreal's unreal API, for what no tool does: search find_tools first, since most tools are not in a client's list. A script that raises fails the call, with the traceback in command_result.
mode picks how command runs: ExecuteFile (the default) runs a script of any length, ExecuteStatement runs one statement and prints its result, and EvaluateStatement returns one expression's value in command_result, as in {"command": "len(unreal.get_editor_subsystem(unreal.EditorActorSubsystem).get_all_level_actors())", "mode": "EvaluateStatement"}. Scripts share the editor console's globals, so a variable one call defines is there for the next.
output and log_output hold what the script prints and logs through unreal.log; what the engine itself logs during the call is only in engine_log, warnings and errors by default. When a call reports success and nothing happened, read engine_log first, widen it with engine_log_level=Log, or pass log_cursor_before to get_output_log_lines for the unfiltered rest.
"Object has no attribute" is the commonest error, and the field usually exists elsewhere: HitResult's impact_point is only in to_dict(), while Transform.translation is an attribute. Ask the object (dir(x), x.to_dict(), x.get_editor_property(...)) instead of guessing.
During Play-in-Editor, a call in the script that compiles a Blueprint or saves (compile_blueprint, save_asset, save_loaded_asset, save_packages, save_current_level...) does not run: the script gets a RuntimeError there, and the call fails with pie_refused even if the script catches it. The guard does not see a Blueprint edited without a compile, so stop PIE before editing one. Reading the live world during PIE is safe.
One call often answers what a loop would: get_actors_by_class for the actors of a class, get_actor_properties_by_class for their properties with the world that answered, get_properties for one object, get_actor_components, raycast_scene, and watch_pie_property for a value over a PIE session. A script that only enumerates actors gets a hint naming the tool. Python stays the right answer for several classes in one round trip, values correlated in one tick, or anything that mutates.
Returns: { command, python_success, execution_mode, command_result, output, log_output, log_line_count, engine_log, engine_log_count, engine_log_truncated, log_cursor_before, pie_refused?, hint? }
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | No | Alias of `command` (same value, same meaning). Accepted because sibling tools spell this parameter differently; prefer `command`. | |
| mode | No | Execution mode: ExecuteFile, ExecuteStatement, or EvaluateStatement (default ExecuteFile). Any other value runs as ExecuteFile; execution_mode in the reply says which ran. | |
| script | No | Alias of `command` (same value, same meaning). Accepted because sibling tools spell this parameter differently; prefer `command`. | |
| command | No | Python code to run (single or multi-line). | |
| capture_output | No | Has no effect, and is accepted so older calls keep working: the output is always captured, and `mode` decides whether the code runs as a script or as one statement. | |
| engine_log_max | No | Max lines in `engine_log` (default 50, 0 disables, hard cap 4096). | |
| engine_log_level | No | Minimum severity of ENGINE log to return in `engine_log`: Error, Warning (default), Display, Log/All. |