Skip to content

Studio: install a level-save toolset into Unreal projects - #18331

Merged
danielhanchen merged 4 commits into
mainfrom
studio-unreal-level-save-tools
Nov 18, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-unreal-level-save-tools

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Epic's Unreal MCP toolsets (Unreal Engine 5.8) can build a whole scene but have no tool to save a level. On a headless editor (a server or cloud box, where nobody can press Ctrl+S) everything an agent builds is lost when the editor stops. This adds a small level-save toolset that Studio ships and installs into an Unreal project.

What happened without it

A Studio chat driving Unreal through MCP built a city (107 buildings, roads, 42 neon bars, a drivable car Blueprint) over three turns. The model searched for a save tool (save_level, save_current_level, save_world, save_map, SaveCurrentLevel, SaveLevel) and found none. It then started Play In Editor on the never-saved level, which crashed UE 5.8 (Assertion failed: Actor->GetLevel() == DestLevel in WorldPartitionLevelHelper.cpp, a "Levels mismatch" on the Open World template's landscape proxy). The whole scene was lost.

Changes

  • studio/backend/integrations/unreal/unsloth_level_tools.py: runs inside the Unreal Editor, never imported by Studio. It registers unsloth_level_tools.LevelSaveTools through Epic's own toolset registry, so it shows up in Unreal MCP tool search next to Epic's toolsets. Tools: save_current_level_as(level_path) (saves a new or Untitled level and its actors under /Game, refuses to overwrite another asset, and reports it when other unsaved assets fail to save; when the open level is already on disk, the editor saves a copy and stays on the original, so the tool opens the copy before saving the remaining dirty packages and the original is never overwritten), save_current_level(), get_current_level_path(). The toolset description tells agents to save before Play In Editor. Registration retries on tick, because init_unreal.py order across plugins is not guaranteed.
  • integrations/unreal/__init__.py: install(project_path) takes a project folder or .uproject. It writes the module into <project>/Content/Python/ and hooks it into init_unreal.py. An existing init_unreal.py keeps its contents and gets the hook appended once; repeat installs are no-ops for the hook. The hook is guarded: if the Unreal MCP plugin is off (no toolset_registry), it logs a warning instead of raising, so anything after it in init_unreal.py still runs. A path pasted with Windows Explorer's "Copy as path" quotes is accepted.
  • Owner-only route POST /api/mcp/servers/builtins/unreal/install-tools. It follows the same gate as the editor MCP tools (_require_managed_access(..., executes = True)): refused for API keys, keyless access and while Remote / LAN access disables local commands, since the editor runs what lands in Content/Python.
  • Studio UI: an "Install level save tools" control (project folder + button) in the Unreal Engine setup help. It is a separate field from the EngineWire project folder, which switches Unreal to the EngineWire token mode.
  • pyproject.toml: ship backend/integrations/unreal/*.py, as for the Blender integration. A wheel built from this branch contains studio/backend/integrations/unreal/__init__.py and unsloth_level_tools.py.

Save-as from an already-saved level

UE 5.8 does not rename a level that already exists on disk when it is saved under a new name. UEditorLoadingAndSavingUtils::SaveMap calls FEditorFileUtils::SaveWorld (Engine/Source/Editor/UnrealEd/Private/FileHelpers.cpp, around line 1127), which copies the world into the new package with ObjectTools::DuplicateSingleObject. The editor stays on the original level, which still holds its unsaved changes. Only a never-saved /Temp level is renamed. The first version of this PR then saved dirty map packages, which wrote those unsaved changes over the original. In a live session an agent emptied a saved city and then called save_current_level_as with a new name, and the emptied level was saved over the city.

save_current_level_as now opens the copy (LevelEditorSubsystem.load_level) when the editor did not switch to it. Loading discards the original's unsaved changes, and the copy already has them. It raises without saving anything else if the switch fails, and saves the remaining dirty packages only once the editor is on the new level. Stand-in tests cover both cases: load_level runs before any save_dirty_packages and the original is never saved, and a failed switch raises with no save_dirty_packages call.

Live check on an isolated UE 5.8.4 editor (fresh project copy, Open World template). An Untitled level with 3 cubes is saved as /Game/T/LevelA, then the 3 cubes are removed and 1 is added, and it is saved as /Game/T/LevelB:

first version of this PR this PR
save_current_level_as("/Game/T/LevelB") returns /Game/T/LevelA /Game/T/LevelB
level open afterwards LevelA LevelB
LevelA actor files on disk (141 after the first save) 139, overwritten with the unsaved deletions 141, unchanged
LevelB actor files on disk 139 139

Tests

  • tests/test_unreal_level_tools.py (14 tests):
    • the installer writes and hooks once, keeps an existing init_unreal.py, needs a .uproject, and accepts a quoted path;
    • the generated hook, executed with and without the toolset importable: registers when it can, warns otherwise, and never stops code after it;
    • the route refuses API keys, keyless access and a disabled local-command policy, writes nothing then, and returns 400 on a bad path;
    • the toolset, run against a stand-in unreal module: path validation, save_map then save_dirty_packages, refusing save_current_level on a /Temp level, an error when other assets fail to save after the map did, and tick-retried registration.
  • With the existing editor MCP and MCP route tests: 196 passed, 1 skipped (Windows-only Roblox launcher). Frontend: tsc --noEmit clean; biome and eslint report the same counts on the touched files as on main.
  • Live, Unreal Engine 5.8.4 built from source on Linux, headless editor (-RenderOffscreen) on a B200, Unreal MCP over HTTP:
    • Installed with this installer; the editor listed unsloth_level_tools.LevelSaveTools (56 toolsets, 55 before).
    • Replayed the lost session's 33 scene scripts. Every call had the same outcome as in the original session, and the per-folder actor counts matched the pre-crash scene exactly.
    • save_current_level_as("/Game/GTA6/Maps/L_NeonCity") saved in about 13 s: the map, 388 external actor packages, and the two unsaved Blueprints.
    • After an editor restart the level reloaded with identical actor counts.

Screenshot

Unreal Engine MCP dialog, setup help, from two clean Studio installs (main vs this branch, no model, no Unreal). After: the new control, pointed at a throwaway folder holding only a .uproject, reports "Installed. Restart Unreal Editor to load the level save tools." and writes init_unreal.py and unsloth_level_tools.py into its Content/Python. Before: no such control.

Unreal level save tools installer before and after

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T14:42:23.705274Z 9e1ccbd Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3d8a5c5d53

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

world = unreal.get_editor_subsystem(unreal.UnrealEditorSubsystem).get_editor_world()
if not unreal.EditorLoadingAndSavingUtils.save_map(world, level_path):
raise RuntimeError(f"Saving the level as {level_path!r} failed; see the editor log")
unreal.EditorLoadingAndSavingUtils.save_dirty_packages(True, True)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check whether saving dirty packages succeeds

If the map saves successfully but a dependent asset cannot be saved (for example, a read-only Blueprint package), save_current_level_as still returns a successful level path. Epic's API documentation specifies that save_dirty_packages returns false on failure, but that result is ignored here. An agent can consequently report the scene as saved and restart the editor while some assets remain unsaved. Check this result and raise an error, as save_current_level already does.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 51867ef: save_current_level_as now raises when save_dirty_packages returns false, naming the saved level.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 51867efc95

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 18, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 9e1ccbde0e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit b3a68f1 into main Nov 18, 2026
@danielhanchen
danielhanchen deleted the studio-unreal-level-save-tools branch November 18, 2026 14:43
danielhanchen added a commit that referenced this pull request Nov 18, 2026
## Read the Unreal toolset as UTF-8 in its install test


`tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding`
fails on `main` since #18331. It shows up in `Repo tests (CPU, rest-2)`:

```
E   AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding ...
E     Left contains one more item: 'studio/backend/tests/test_unreal_level_tools.py:31: read_text()'
```

### Cause

`test_install_writes_the_toolset_and_hooks_it_once` compares the
installed `unsloth_level_tools.py` against the checked-in source,
`unreal_tools._SOURCE`, using bare `.read_text()` on both sides.

The checked-in file is then decoded with the platform default, which is
the ANSI codepage on Windows. That is exactly what this lint rejects.

`integrations/unreal/__init__.py` itself already reads `_SOURCE` and
writes the installed copy with `encoding = "utf-8"`. Only the test reads
them without an encoding.

### Fix

Both sides of that comparison now read with `encoding = "utf-8"`,
matching how `install()` reads and writes them. The assertion is
unchanged.

### Verification

- `tests/test_source_read_encoding.py`: fails on `main` on this line and
passes with this change (16 passed).
- `studio/backend/tests/test_unreal_level_tools.py`: 14 passed.
- ruff is clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant