Add from_xml_string to MuJoCo WASM bindings.
This change introduces new functions to load an `mjModel` from an XML string. Two overloads are provided: one that creates a temporary VFS and one that uses a MjVFS arg. Also updated other places where `from_xml_string` should be used: demos, readme, etc. PiperOrigin-RevId: 882581762 Change-Id: I8428196f8f56ae9630fcd4f0bb6763cdc1c43da0
This commit is contained in:
committed by
Copybara-Service
parent
df0bc58578
commit
b294d15558
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/bind.h>
|
||||
#include <emscripten/em_asm.h>
|
||||
#include <emscripten/val.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -8361,6 +8362,39 @@ std::unique_ptr<MjModel> mj_loadModel_wrapper(std::string filename, const MjVFS&
|
||||
return std::unique_ptr<MjModel>(new MjModel(model));
|
||||
}
|
||||
|
||||
std::unique_ptr<MjModel> from_xml_string_wrapper_1(const std::string& xml) {
|
||||
mjVFS vfs;
|
||||
mj_defaultVFS(&vfs);
|
||||
const char* filename = "model.xml";
|
||||
int add_result = mj_addBufferVFS(&vfs, filename, xml.c_str(), xml.length());
|
||||
if (add_result != 0) {
|
||||
mj_deleteVFS(&vfs);
|
||||
mju_error("Could not add XML string to VFS: %d", add_result);
|
||||
}
|
||||
char error[1000];
|
||||
mjModel* model = mj_loadXML(filename, &vfs, error, sizeof(error));
|
||||
mj_deleteVFS(&vfs);
|
||||
if (!model) {
|
||||
mju_error("Loading error: %s\n", error);
|
||||
}
|
||||
return std::unique_ptr<MjModel>(new MjModel(model));
|
||||
}
|
||||
|
||||
std::unique_ptr<MjModel> from_xml_string_wrapper_2(const std::string& xml, const MjVFS& vfs) {
|
||||
std::string filename = "model.xml";
|
||||
int add_result = mj_addBufferVFS(vfs.get(), filename.c_str(), xml.c_str(), xml.length());
|
||||
if (add_result != 0) {
|
||||
mju_error("Could not add XML string to VFS: %d", add_result);
|
||||
}
|
||||
char error[1000];
|
||||
mjModel* model = mj_loadXML(filename.c_str(), vfs.get(), error, sizeof(error));
|
||||
mj_deleteFileVFS(vfs.get(), filename.c_str());
|
||||
if (!model) {
|
||||
mju_error("Loading error: %s\n", error);
|
||||
}
|
||||
return std::unique_ptr<MjModel>(new MjModel(model));
|
||||
}
|
||||
|
||||
std::unique_ptr<MjSpec> parseXMLString_wrapper(const std::string &xml) {
|
||||
char error[1000];
|
||||
mjSpec *ptr = mj_parseXMLString(xml.c_str(), nullptr, error, sizeof(error));
|
||||
@@ -11596,12 +11630,16 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
|
||||
.property("useexisting", &MjLROpt::useexisting, &MjLROpt::set_useexisting, reference())
|
||||
.property("uselimit", &MjLROpt::uselimit, &MjLROpt::set_uselimit, reference());
|
||||
emscripten::class_<MjModel>("MjModel")
|
||||
// mj_loadXML is deprecated and will be removed in a future release
|
||||
.class_function("mj_loadXML", emscripten::select_overload<std::unique_ptr<MjModel>(std::string)>(&mj_loadXML_wrapper_1))
|
||||
.class_function("from_xml_path", emscripten::select_overload<std::unique_ptr<MjModel>(std::string)>(&mj_loadXML_wrapper_1))
|
||||
.class_function("mj_loadXML", emscripten::select_overload<std::unique_ptr<MjModel>(std::string, const MjVFS&)>(&mj_loadXML_wrapper_2))
|
||||
.class_function("from_xml_path", emscripten::select_overload<std::unique_ptr<MjModel>(std::string, const MjVFS&)>(&mj_loadXML_wrapper_2))
|
||||
// mj_loadModel is deprecated and will be removed in a future release
|
||||
.class_function("mj_loadModel", &mj_loadModel_wrapper)
|
||||
.class_function("from_binary_path", &mj_loadModel_wrapper)
|
||||
.class_function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&)>(&from_xml_string_wrapper_1))
|
||||
.class_function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&, const MjVFS&)>(&from_xml_string_wrapper_2))
|
||||
.class_function("from_xml_path", emscripten::select_overload<std::unique_ptr<MjModel>(std::string)>(&mj_loadXML_wrapper_1))
|
||||
.class_function("from_xml_path", emscripten::select_overload<std::unique_ptr<MjModel>(std::string, const MjVFS&)>(&mj_loadXML_wrapper_2))
|
||||
.constructor<const MjModel &>()
|
||||
// Binds the functions on MjModel that return accessors.
|
||||
#define X_ACCESSOR(NAME, Name, OBJTYPE, field_name, nfield) \
|
||||
@@ -13382,6 +13420,8 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
|
||||
// as using std::optional<MjVFS> caused memory errors due to missing copy/move constructors.
|
||||
function("mj_compile", emscripten::select_overload<std::unique_ptr<MjModel>(const MjSpec&)>(&mj_compile_wrapper_1));
|
||||
function("mj_compile", emscripten::select_overload<std::unique_ptr<MjModel>(const MjSpec&, const MjVFS&)>(&mj_compile_wrapper_2));
|
||||
function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&)>(&from_xml_string_wrapper_1));
|
||||
function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&, const MjVFS&)>(&from_xml_string_wrapper_2));
|
||||
|
||||
emscripten::class_<WasmBuffer<float>>("FloatBuffer")
|
||||
.constructor<int>()
|
||||
|
||||
Reference in New Issue
Block a user