Fix vfs load for mesh gmeshs type

This commit is contained in:
Mark Yang
2026-03-07 21:45:12 -05:00
parent 6bc6c11c49
commit 95724d1ec2
4 changed files with 56 additions and 11 deletions
+43
View File
@@ -15,6 +15,7 @@
// Tests for user/user_model.cc.
#include <array>
#include <cstdio>
#include <string>
#include <gmock/gmock.h>
@@ -876,5 +877,47 @@ TEST_F(UserFlexTest, MeshNodePinning) {
mj_deleteModel(m);
}
TEST_F(UserFlexTest, FlexcompMeshLoadsFromVFS) {
// read cube.stl from testdata into a buffer
const std::string stl_path =
GetTestDataFilePath("user/testdata/cube.stl");
FILE* f = fopen(stl_path.c_str(), "rb");
ASSERT_THAT(f, NotNull()) << "Could not open " << stl_path;
fseek(f, 0, SEEK_END);
long stl_size = ftell(f);
fseek(f, 0, SEEK_SET);
std::string stl_data(stl_size, '\0');
fread(stl_data.data(), 1, stl_size, f);
fclose(f);
// add the STL data to a VFS
mjVFS vfs;
mj_defaultVFS(&vfs);
mj_addBufferVFS(&vfs, "cube.stl", stl_data.data(), stl_size);
// XML that uses flexcomp type="mesh" referencing the VFS file
static constexpr char xml[] = R"(
<mujoco>
<worldbody>
<body name="flex_body" pos="0 0 1">
<flexcomp name="flex_object" type="mesh" file="cube.stl" rigid="true">
<contact contype="1" conaffinity="1"/>
</flexcomp>
</body>
</worldbody>
</mujoco>
)";
// cleanup
std::array<char, 1024> error;
mjModel* m = LoadModelFromString(xml, error.data(), error.size(), &vfs);
ASSERT_THAT(m, NotNull()) << error.data();
mjData* d = mj_makeData(m);
mj_step(m, d);
mj_deleteData(d);
mj_deleteModel(m);
mj_deleteVFS(&vfs);
}
} // namespace
} // namespace mujoco