Add MjSpec.encode method.

PiperOrigin-RevId: 907177387
Change-Id: I65800298798037c4a0c0cd79ec86706be67db51e
This commit is contained in:
Sam Haves
2026-04-28 14:22:11 -07:00
committed by Copybara-Service
parent ace90c00b9
commit d92fe0810c
5 changed files with 119 additions and 0 deletions
+13
View File
@@ -20,6 +20,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <functional>
#include <iterator>
#include <map>
@@ -143,6 +144,18 @@ mjSpec* mj_parse(const char* filename, const char* content_type,
int mj_encode(const mjSpec* s, const mjModel* m, const char* filename,
const char* content_type, const mjVFS* vfs, char* error,
int error_sz) {
// TODO(shaves) Move MJCF and URDF to encoders/decoders.
auto filepath = mujoco::user::FilePath(filename);
if (filepath.Ext() == ".xml" ||
(content_type && std::strcmp(content_type, "text/xml") == 0)) {
int result = mj_saveXML(s, filename, error, error_sz);
if (result < 0) {
return -1;
}
return std::filesystem::file_size(filename);
}
const mjpEncoder* encoder = mjp_findEncoder(filename, content_type);
if (!encoder) {
if (error) {