Repository navigation
Studio: install a level-save toolset into Unreal projects - #18331
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
Fixed in 51867ef: save_current_level_as now raises when save_dirty_packages returns false, naming the saved level.
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…iginal level is not overwritten
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
## 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.
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() == DestLevelin 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 registersunsloth_level_tools.LevelSaveToolsthrough 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 intoinit_unreal.py. An existinginit_unreal.pykeeps 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 (notoolset_registry), it logs a warning instead of raising, so anything after it ininit_unreal.pystill runs. A path pasted with Windows Explorer's "Copy as path" quotes is accepted.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.pyproject.toml: shipbackend/integrations/unreal/*.py, as for the Blender integration. A wheel built from this branch containsstudio/backend/integrations/unreal/__init__.pyandunsloth_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::SaveMapcallsFEditorFileUtils::SaveWorld(Engine/Source/Editor/UnrealEd/Private/FileHelpers.cpp, around line 1127), which copies the world into the new package withObjectTools::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 calledsave_current_level_aswith a new name, and the emptied level was saved over the city.save_current_level_asnow 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_levelruns before anysave_dirty_packagesand the original is never saved, and a failed switch raises with nosave_dirty_packagescall.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:save_current_level_as("/Game/T/LevelB")returns/Game/T/LevelA/Game/T/LevelBTests
tests/test_unreal_level_tools.py(14 tests):unrealmodule: path validation,save_mapthensave_dirty_packages, refusingsave_current_levelon a /Temp level, an error when other assets fail to save after the map did, and tick-retried registration.tsc --noEmitclean; biome and eslint report the same counts on the touched files as on main.-RenderOffscreen) on a B200, Unreal MCP over HTTP:unsloth_level_tools.LevelSaveTools(56 toolsets, 55 before).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.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 writesinit_unreal.pyandunsloth_level_tools.pyinto itsContent/Python. Before: no such control.