# Porting from Unreal Engine 5

# Porting from Unreal Engine 5

Ferret's first task is porting games made in Unreal Engine 5. Its features follow what UE5 games use, and `tools/ue-export` carries a level across.

## The importer app (Caroline's tools)

When Unreal Engine is on the person's own Windows computer, nobody runs the scripts by hand: the Ferret UE importer/exporter, a tray app there, does the work, and Caroline in the editor drives it.

- `ue_places` lists the account's apps (online or not) with their UE versions, projects and maps.
- `ue_import {project, map, level?}` brings a map in as a new level: geometry with baked materials, environment, collision, physics, landscape. A level of that name must not exist yet.
- `ue_export {level, project, map?}` writes a level into a UE project as a new map under `/Game/Ferret/<level>/` (models and textures by Interchange, the actors, the lights, a `PlayerStart`); nothing else in the project changes.
- `ue_status` follows a job (a finished import brings its level into the project), `ue_cancel` stops it. A job that runs longer than about 3 minutes answers with an id; ask `ue_status` later.
- **The queue.** Jobs do not run one after another: each waits in the app's queue and starts when the computer has room, so several imports can run side by side. The next job starts when CPU, memory and GPU each have at least `minFree` percent free (default 20), measured as the mean of the last few samples and not sooner than 90 s after the previous start, so the job that has just started has loaded the machine. `minFree` and `priority` (higher goes first) are given with `ue_import` / `ue_export`; `minFree` is a number or `{cpu, mem, gpu}` (0 = not checked). Only the head of the queue is looked at, so a job that asks for more room is not starved by small ones. A job that waits with nothing running starts after 10 minutes whatever the load. `ue_status` of a waiting job says why it waits.
- The owner's own jobs (an Unreal Python run, a chain of exports) go through the same queue: `node tools/ue-bridge/agent/queue.mjs serve` (or the app itself, which polls the same folder), then `queue.mjs submit --min-free 20 -- <command> [args]`, `list`, `cancel <id>`. A queued job runs below normal priority (`--nice`: idle, belownormal, normal, abovenormal). `pause` holds the queue (no new start), `pause --running` also suspends the running jobs without losing memory or progress, `resume` goes on; `pause-job <id>` / `resume-job <id>` do the same for one job. The app does not kill its jobs when it stops: a local-queue job that is still running when the app starts again is adopted and counted as running.
- A job that must end well is described by its output: `--expect <file>` (repeatable; the file must be written during the run and not empty; with exit 0 too) and `--ok-exit 1` (exit codes besides 0 that count as success when every expected file is there; needs `--expect`). Unreal ends with 1 when its log holds one error, e.g. a shader that does not compile, though the export is complete: such a job is `done` with `exitCode` 1 and a line in its log, a crash that left no file is still `failed`. In a job file: `okExitCodes`, `expect`.
- The result's report names what was not mapped; the Blueprints and the parity check remain the work of the steps in the porting skill.

### Building the project into a Windows game (`ue_build`)

- `ue_build {name?, config?, engine?, minFree?, minDiskGb?, priority?, bridge?}` uploads the open project to the person's Cat Space (`.ferret/ue-build/<id>/src/`) and starts the `build` job in the app. `name` is the game's name (default `FerretGame`; letters, digits, `_`, `-`), `config` is `Development` (default) or `Shipping`, `engine` a UE version such as `5.8` that must be installed there (default: the newest installed).
- The job fetches the project into a folder of its own and builds it (the generated UE project (skill `ferret-unreal-build`)): the game code is translated to C++, a UE 5.8 C++ project is generated, compiled, its content imported (levels as `.umap` maps, models as Blueprint actor classes), cooked and packaged for Win64. It uploads two zips to `.ferret/ue-build/<id>/out/`: `<name>-<yyyymmddHHMM>.zip`, the game (its `<name>.exe`), and `<name>-<yyyymmddHHMM>-project.zip`, the UE project a person can keep editing in Unreal (the stamp is the local time of the computer). It reports its stage and the fraction of it: `fetch`, `translate`, `generate`, `compile`, `import`, `cook`, `stage`, `package`, `zip`, `upload`.
- An editor project builds as it is: `ferret.json` names the code entry (`entry.code`), the start level (`entry.level`, whose map becomes the game's start map) and the spawn the player starts at (`entry.spawn`); every file outside `src/` that the game can load is imported or packaged by its original path.
- `ue_status` shows the stage while it runs; when it is done it gives `gameZip` and `projectZip`, each with `catspace` (the path in Cat Space), `bytes` and `link`, the address in Cat Space (it is read with the account's sign-in), and `exe`, the game's program name (`zip` and `link` are the game zip, as before). It gives the same answer again until `ue_cancel` clears the finished build; the zips stay in Cat Space. `ue_cancel` on a running build stops it at whatever stage it is in, ends its processes (the translator, UBT, the editor, UAT and their children), and removes its folder and the uploaded project.
- A build waits in the same queue as import and export (`minFree`, `priority`); several builds run side by side, each with its own folder and its own UE project. The disk is checked before the start and during the job: it stops with "the disk is full" when less than `minDiskGb` (default 30 GB) or less than `minFree` percent (a number, or `{disk}`) of the drive is free, or when a write fails with no space.
- It fails like the other jobs: a stage with no progress and no log output for 30 minutes, or longer than 6 hours, is killed (`FERRET_BRIDGE_STALL_MS`, `FERRET_BRIDGE_STEP_TIMEOUT_MS`); a process that survives its kill 20 s is given up; the result of a finished build that cannot be sent waits in the app's outbox and is sent again every round, also after a restart. The requester gets `{ok: false, error, log}` with the end of the log.
- The build step is one interface, `runBuildStep({projectDir, workDir, config, engine, name, stamp, onProgress, signal, spawned, log}) -> {zipPath, projectZip, exe, report}` in `tools/ue-bridge/agent/build-step.mjs`; it runs `buildProject` of `tools/ue-cpp/project/package.mjs`, and every process the build starts is handed to the job (`spawned`) so the job ends it with itself. `createFakeBuildStep` there makes the same stages and small zips without Unreal for the job's tests.
- The toolkit the app carries holds the build toolchain laid out as in the repository (`tools/ue-bridge/kit-ue-cpp.mjs`): `tools/ue-cpp` (translator, runtime, facade, generator, import and packaging scripts, the runtime plugin), the Ferret packages' sources `packages/*/src`, `tsconfig.json`, TypeScript and the type declarations of `babylonjs` and `node`. The translator runs as a process of its own with the toolkit as its root.

If no app is online, the person installs it from https://ferret.partners.solutions/ferret/download/ue-bridge/ferret-ue-bridge-latest.exe (the editor's menu **UE > Download the UE importer/exporter**), runs it and signs in with their SW account in its panel. It is Windows only and needs Unreal Engine installed there; there is no cloud variant.

## UE concepts and their Ferret equivalents

| UE5 | Ferret |
|---|---|
| Level, World Partition | a level ([Levels and packages](levels.md)), level travel |
| Actor, Actor Component, Blueprint class | entity, component, behaviour registered by name |
| Subsystems, GameMode / GameState | services; a game's rules as behaviours and services |
| Character + Character Movement Component | `character`, with the same parameters |
| Camera, Spring Arm | the camera rig of `character`, with spring-arm collision |
| Animation Blueprint, Blend Space, Montage, Notify | `animation` graph, blend spaces, montages, notifies |
| Behavior Tree, AI Controller, NavMesh | `ai` |
| Collision channels and presets, overlaps | `physics` layers and masks, triggers |
| Enhanced Input | `input` actions and bindings |
| UMG | `ui`, rewritten per widget |
| Physics Constraint, fracture | `constraints` and `fractured` in a level's `physics` block |
| Save Game | `platform` state |

What has no equivalent (Lumen, Nanite, Chaos destruction, MetaHuman rigs) is replaced per game: baked lighting, LODs, prebaked destruction, simplified rigs. The porting report lists each case.

## The pipeline

1. **Geometry and materials.** `ue/gltf.py`, run inside the Unreal editor, exports the level with UE's glTF exporter, materials baked to PBR. It needs rendering on (`-AllowCommandletRendering`) and compiles the level's materials first, otherwise the baked textures come out empty.
2. **Environment, collision, NavMesh settings, landscape, physics.** The other scripts in `tools/ue-export/ue/` write the level's sun and fog, its simple collision shapes, NavMesh build settings, landscape layers and physical materials next to the glTF.
3. **Blueprints.** `blueprint_text.py` and `blueprints.py` dump each mapped Blueprint class as a spec, stored next to the Ferret behaviour that replaces it. A mapping from the game (`Blueprint class → behaviour + params`) turns the level's actors into manifest entities. Unknown classes are listed in a report, not dropped.
4. **Post-processing.** `postprocess.mjs` (glTF-Transform) prunes, instances repeated meshes (`EXT_mesh_gpu_instancing`), simplifies foliage, scales and re-encodes textures per tier, and optionally compresses to KTX2 and meshopt.
5. **Characters and mobs.** `inventory_characters.py` lists every skeletal mesh, skeleton, animation Blueprint, sequence, blend space and montage of a project (size, bones, morphs, which skeleton each clip is on). `characters.py` exports named skeletal meshes and animation sequences to GLB with the same glTF settings (`FERRET_ASSETS`, `FERRET_OUT`, `FERRET_ANIMS=1` to carry a skeleton's clips with the mesh), one file per asset and an `export.json` report. What is game-specific stays in the game: which meshes make a character, preset and customizer data, material values.

### Landscapes (layers, far backdrop, virtual texture)

A level may have several Landscape actors (a near ground and a far mountain plane). `ue/landscape.py` writes `landscape-<label>.json` for each (heights from the collision, `LAND_STEP_<label>` per-landscape step). `ue/landscape_layers.py` writes the paint layers, one greyscale weightmap per layer over the landscape's bounds, the landscape material's texture parameters and the RuntimeVirtualTextureVolume settings; `landscape-weights.mjs` packs four layers per RGBA image. `postprocess.mjs --landscape spec.json` with `{ "landscapes": [{ "file": "landscape-Landscape3.json", "name": "Landscape3", "stride": 8, "far": true }, { "file": ..., "name": "Landscape", "layers": [...], "weights": [...], "virtualTexture": { "meshes": "^SM_rock" } }] }` builds one mesh per landscape (the far one decimated, kept out of the collision) and writes them into `environment.landscapes`, which `@ferret/levels` applies at load (`landscape.ts`): a material plugin blends the layers per pixel by the weightmaps (optionally height-blended), the far plane is marked never walked or picked, and the blended colour is baked once into a texture the named props sample near their feet (the runtime virtual texture's job in Unreal). Props of the new method (library materials, `docs/concepts/asset-library.md`) read the same baked ground through `groundBlend`, found by landscape name (`groundSourceOf`). The layer textures, their tiling and the virtual-texture meshes come from the landscape material's graph (`materials.py` writes it as text), so the spec is written by reading that graph.

```bash
node tools/ue-export/postprocess.mjs level.glb out/ --tier all --ktx2 --meshopt \
  --environment environment.json --collision collision_shapes.json --nav nav.json
```

The output is a level folder (and per tier) that the [level service](levels.md) loads, or a `.ferret` package.

## Porting a game

1. **Inventory**: list the levels, classes, plugins and UE features in use; the porting report says what maps as is, what needs a rewrite and what has no equivalent.
2. **Mapping** of each UE class to a behaviour.
3. **Rewrite** of behaviours, services and screens from the specs, one class at a time, with tests in the fixed-step loop.
4. **Export** of each level; the model viewer and agents check the result.
5. **Parity check**: the ported level is compared with the original from the same cameras, along the same walked paths, with the same triggers firing.
6. **Re-export** when the original changes; the game's edits on top of the export stay, because the manifest refers to nodes by name.

## Lessons from real ports

Most of the time in a port goes to things that run without an error and come out wrong. Three pages collect what was found:

- [Running the Unreal side of a port](ue-export-runs.md): run settings that give complete output, and the traps of the export runs.
- Making a ported level look right (skill `ferret-unreal-level-look`): black or dark surfaces, smeared materials, wrong colours, flat light, with causes and fixes; find the cause before changing anything.
- Parity with the original (skill `ferret-unreal-level-look`): the same views shot in Unreal and in Ferret, the axis mapping, and reading the differences.
- Porting characters and animation (skill `ferret-unreal-characters`): clips, parts, hair, morph targets, size, Animation Blueprints.

## The model viewer

`pnpm viewer` starts `tools/viewer`: a page that shows a level or an asset, plus an HTTP server that relays commands (skill `ferret`) from clients (agents, tests, an MCP server) to the open page and back, serves screenshots of the active camera, and saves edited manifests. `pnpm viewer:headless` opens the page without a screen, so a CI run or an agent needs none. The viewer grows into the level editor.

**Characters.** `?characters=<index.json>` (or `--characters` on the viewer server) turns the page into a character viewer for what a game exported: a list of the characters, the clips of the clips file (or the ones in each character's own file) as buttons, "play all", and a report on each character: a file that does not load, a mesh without a skin or a material, a clip with no track on this body or that moves nothing; clip bones the body does not have are noted, not counted as problems. The index is a small JSON the game's build writes next to the files, urls relative to it: `{ "clips": "cc_clips.glb", "characters": [{ "name": "CC_Female_Preset_01", "url": "low/CC_Female_Preset_01.glb" }] }`. A character made of modular parts lists their files in `"parts"` (one file per part, one skeleton): the viewer shows them together and gives each the clips; `url` is not loaded then. A character may carry its own `"clips"` file, which wins over the index's (families with a skeleton of their own: NPCs, animals). Commands for agents: `characters.list`, `characters.show {name}` (answers with the report), `characters.play {clip}`, `characters.playall {seconds?}`, `characters.stop`, `characters.check {name?}` (every character when no name is given: `{ reports, problems }`).
