Swapping Gear on the Fly:
Picking a Model From the Inspector
Up until now, adding a MeshComponent to an entity in RoboEngine was a bit of a tease. You could click Add Component → Mesh, stare at a blank slot, and…
…that was it.
To actually put a 3D model on screen, you had to save your scene, crack open the .scene.json file in a text editor, type the path by hand, and reload the whole world.
I finally fixed that workflow by adding a “Choose Model…” button right inside the Inspector. Clicking it launches our modal file picker filtered to .obj files, loads the model (or reuses an existing GPU copy already in the scene), and logs the entire swap as a single undoable step.
The button itself is tiny, the real treasure here is the architectural pattern behind it: teaching decoupled component UIs how to ask the editor for favors they can’t execute on their own.
Before:
Look, Don’t Touch
Previously, the MeshComponent Inspector panel was strictly read-only. It printed the current file path and a polite warning if nothing was loaded:
bool drawMesh(entt::registry ®istry, Entity entity)
{
const auto &mesh = registry.get<MeshComponent>(entity);
ImGui::Text("File: %s", mesh.path.empty() ? "(none)" : mesh.path.c_str());
if (!mesh.model)
{
ImGui::TextDisabled("No model loaded; nothing is drawn.");
}
return false;
}
Making that path editable required more than slapping a button into drawMesh. Every component’s Inspector UI is a standalone free function given only the entt::registry and an Entity handle. Swapping a 3D model, however, demands three heavy hitters that belong exclusively to ReEditor: a modal file picker, the Vulkan device to upload vertex/index buffers, and the undo history stack.
The Upgrade:
UIs That Ask, an Editor That Acts
Rather than passing a massive ReEditor* pointer down into every component drawer, we introduced a lightweight request channel.
1. The Requests Struct
Component UI functions gained a third parameter: a Requests struct where any panel can raise a flag asking the editor to perform privileged work once the Inspector finishes drawing:
// Things a component UI can't do itself because the editor owns them (dialogs, GPU
// resources, undo). The UI sets a flag; the editor handles it after the Inspector is drawn.
struct Requests
{
bool chooseModel = false; // Open the model picker for this entity's Mesh
};
using DrawFn = bool (*)(entt::registry ®istry, Entity entity, Requests &requests);
Now drawMesh stays completely decoupled, it just draws a full-width button and flips the switch:
// The editor opens the picker, loads the model, and records the change for undo
if (ImGui::Button("Choose Model...", ImVec2(-FLT_MIN, 0.0f)))
{
requests.chooseModel = true;
}
At the top of the Inspector loop, the editor creates a fresh Requests struct, passes it through every active component UI, and dispatches whatever flags were raised afterward:
if (requests.chooseModel)
{
requestChooseModel(scene, selected);
}
Meanwhile, Transform, Point Light, and Orbit simply accept the parameter and ignore it.
2. Reusing ReFilePicker
Our custom ImGui file browser gained a new PickerPurpose::Model mode alongside a modelTarget variable remembering which entity requested the swap. When triggered, the picker opens directly inside the current model’s folder with its filename pre-filled (or defaults to content/models for an empty mesh):
std::string startDir = MODELS_DIR; // "content/models"
std::string name;
if (const auto *mesh = scene.registry().try_get<MeshComponent>(entity); mesh && !mesh->path.empty())
{
startDir = std::filesystem::path(mesh->path).parent_path().string();
name = std::filesystem::path(mesh->path).filename().string();
}
pickerPurpose = PickerPurpose::Model;
modelTarget = entity;
filePicker.open(ReFilePicker::Mode::Open, "Choose Model", startDir, MODEL_EXTENSION, name);
The moment you confirm a file, drawFileDialogs() routes the chosen path straight to assignModel().
3. Safe, Instant Model Assignment
assignModel() executes in a strict, defensive sequence:
- Verify the target still exists.
- The entity might have been deleted while the dialog was open.
- Check for an existing GPU copy.
- Scan the live scene for any
MeshComponentalready using the same path and share itsshared_ptr<ReModel>so duplicate meshes only live in VRAM once.
- Scan the live scene for any
- Load from disk if needed.
- If loading throws an exception, report the error in the status bar and bail out before touching the scene or undo stack.
- Snapshot, wait, swap, record.
- Capture the entity’s before-state, wait for the GPU to finish in-flight frames, swap in the new model and path, and push a single undo step labeled “Set model of …”:
std::shared_ptr<ReModel> model;
for (auto [other, mesh] : registry.view<const MeshComponent>().each())
{
if (mesh.model && mesh.path == path) { model = mesh.model; break; }
}
if (!model)
{
try { model = ReModel::createModelFromFile(reDevice, path); }
catch (const std::exception &e)
{
statusMessage = "Couldn't load " + path;
return;
}
}
EntitySnapshot before = captureEntity(scene, entity);
vkDeviceWaitIdle(reDevice.device()); // Previous model may still be in flight on the GPU
auto &mesh = registry.get_or_emplace<MeshComponent>(entity);
mesh.model = std::move(model);
mesh.path = path;
recordChange({"Set model of " + entityName(scene, entity), {std::move(before)}, {captureEntity(scene, entity)}, entity});
Because our undo snapshots clone the entire MeshComponent, including its shared_ptr<ReModel>, hitting Ctrl+Z restores the previous 3D mesh instantaneously with zero disk I/O.
Traps & Mobs Along the Way
The world can shift while a modal is open
- Even though
ReFilePickeris modal, its result arrives several frames after the button click. Checkingscene.isValid(entity)first leverages EnTT’s versioned IDs to guarantee we never write to a deleted entity (even if its index slot was recycled). Usingget_or_emplace<MeshComponent>also protects us if the component itself was somehow stripped in the meantime.
Do the risky work before committing anything.
- Disk I/O and mesh parsing can fail. Performing
createModelFromFilebefore taking snapshots, callingvkDeviceWaitIdle, or mutating the component guarantees that a bad.objfile leaves your scene, dirty flag, and undo history completely untouched.
Model reuse scans the live scene, not a global cache.
- Our deduplication loop checks
MeshComponents currently active in the registry. If a model is only being kept alive inside the undo stack from a deleted entity, picking that file again will load a fresh copy. Similarly, paths compare as raw strings (content/models/cube.objvs./content/models/cube.obj), thoughReFilePicker’s relative-path normalization keeps them consistent in normal use.
Keep component UIs dumb and isolated.
- Keeping
drawMeshas a pure free function with zero knowledge ofReEditormakes every component drawer easy to test and reason about. Adding future editor actions (like a texture picker or material browser), is now as simple as adding one boolean toRequestsand oneifcheck in the Inspector.
Next Up on the Quest
Hold onto your helmets, we’ll cover the biggest leap in RoboEngine’s journey yet! In the next few DevBlogs, we’re splitting the codebase into a clean Engine runtime library and Editor application, while unleashing PBR materials, Image-Based Lighting (IBL), shadows, skeletal meshes, glTF loading, and a deferred rendering pipeline all in one massive upgrade!
