Add pluggable resource writing to MuJoCo

Extend mjpResourceProvider with an optional write callback (write)
so that mj_encode, mj_saveXML, and mj_saveModel can write to any
registered provider.

PiperOrigin-RevId: 945202741
Change-Id: I37903425260932e555f4a8c2392c4ff8c2e6cc06
This commit is contained in:
Sam Haves
2026-07-09 10:47:01 -07:00
committed by Copybara-Service
parent bdeb7e7c4d
commit dc7581acfa
18 changed files with 324 additions and 83 deletions
+5
View File
@@ -58,6 +58,10 @@ typedef int (*mjfUnmountResource)(mjResource* resource);
// returns < 0 if the resource is older than the given timestamp
typedef int (*mjfResourceModified)(const mjResource* resource, const char* timestamp);
// callback for writing bytes to a resource
// return number of bytes written, return -1 if error
typedef mjtSize (*mjfWriteResource)(mjResource* resource, const void* buffer, mjtSize nbytes);
// struct describing a single resource provider
typedef struct mjpResourceProvider {
const char* prefix; // prefix for match against a resource name
@@ -67,6 +71,7 @@ typedef struct mjpResourceProvider {
mjfMountResource mount; // mounting callback (optional)
mjfUnmountResource unmount; // unmounting callback (optional)
mjfResourceModified modified; // resource modified callback (optional)
mjfWriteResource write; // writing callback (optional)
void* data; // opaque data pointer (resource invariant)
} mjpResourceProvider;
+5
View File
@@ -1588,6 +1588,11 @@ MJAPI void mju_closeResource(mjResource* resource);
// return negative value if error.
MJAPI int mju_readResource(mjResource* resource, const void** buffer);
// Write resource data via its resource provider, return bytes written or -1 on error.
// Nullable: vfs, error
MJAPI mjtSize mju_writeResource(const char* name, const void* buffer, mjtSize nbytes,
const mjVFS* vfs, char* error, size_t nerror);
// For a resource with a name partitioned as {dir}{filename}, get the dir and ndir pointers.
MJAPI void mju_getResourceDir(mjResource* resource, const char** dir, int* ndir);