Cleanup model loading flow.

Inlines the platform::LoadMujocoModel into the App class which simplifies
the logic and allows us to correctly display loading errors.

Also updates shortcuts:

Load Model (Ctrl+L) -> Open File (Ctrl+O)
Reload Model (Ctrl+R) -> Reload Model (Ctrl+L)

PiperOrigin-RevId: 840232553
Change-Id: I957d29046ae681d2aeb69310c3a2eec64d042d49
This commit is contained in:
Haroon Qureshi
2025-12-04 07:17:45 -08:00
committed by Copybara-Service
parent 8823e9fedf
commit c5cd276e1f
4 changed files with 67 additions and 74 deletions
-32
View File
@@ -30,41 +30,9 @@
#include <mujoco/mjxmacro.h>
#include <mujoco/mujoco.h>
#include "engine/engine_vis_visualize.h"
#include "xml/xml_api.h"
namespace mujoco::platform {
mjModel* LoadMujocoModel(const std::string& model_file, const mjVFS* vfs) {
mjModel* model = nullptr;
if (model_file.empty()) {
auto spec = mj_makeSpec();
model = mj_compile(spec, 0);
mj_deleteSpec(spec);
} else if (model_file.ends_with(".mjb")) {
model = mj_loadModel(model_file.c_str(), 0);
if (!model) {
return nullptr;
}
} else if (model_file.ends_with(".xml")) {
char error[1000] = "";
model = mj_loadXML(model_file.c_str(), vfs, error, sizeof(error));
if (!model) {
return nullptr;
}
} else {
char error[1000] = "";
auto spec =
mj_parseXMLString(model_file.c_str(), nullptr, error, sizeof(error));
if (!spec) {
return nullptr;
}
model = mj_compile(spec, 0);
mj_deleteSpec(spec);
}
return model;
}
void SaveText(const std::string& contents, const std::string& filename) {
std::ofstream file(filename);
file.write(contents.data(), contents.size());
-3
View File
@@ -46,9 +46,6 @@ void SaveDepthToWebp(int width, int height, const float* data,
void SaveScreenshotToWebp(int width, int height, mjrContext* con,
const std::string& filename);
// Loads a MuJoCo model from the given file.
mjModel* LoadMujocoModel(const std::string& model_file, const mjVFS* vfs);
// Returns a pointer to the value of the given field in the given data.
// Returns nullptr if the field is not found or the index is out of bounds.
const void* GetValue(const mjModel* model, const mjData* data,