From caec236b2f05567b7803d4674edae2ded749749b Mon Sep 17 00:00:00 2001 From: Haroon Qureshi Date: Thu, 11 Dec 2025 01:23:03 -0800 Subject: [PATCH] Make mj_loadModelBuffer a public API. PiperOrigin-RevId: 843105390 Change-Id: I4058471b4cf64995dd5ab43e9c8c24e322004b62 --- doc/APIreference/functions.rst | 9 +++++++++ doc/includes/references.h | 1 + include/mujoco/mujoco.h | 3 +++ python/mujoco/introspect/functions.py | 20 ++++++++++++++++++++ src/engine/engine_io.h | 4 ++-- unity/Runtime/Bindings/MjBindings.cs | 3 +++ wasm/codegen/generators/constants.py | 1 + 7 files changed, 39 insertions(+), 2 deletions(-) diff --git a/doc/APIreference/functions.rst b/doc/APIreference/functions.rst index 1095d16c..7b26f506 100644 --- a/doc/APIreference/functions.rst +++ b/doc/APIreference/functions.rst @@ -1475,6 +1475,15 @@ If vfs is not NULL, look up file in vfs before reading from disk. *Nullable:* ``vfs`` +.. _mj_loadModelBuffer: + +`mj_loadModelBuffer <#mj_loadModelBuffer>`__ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. mujoco-include:: mj_loadModelBuffer + +Load model from memory buffer. + .. _mj_deleteModel: `mj_deleteModel <#mj_deleteModel>`__ diff --git a/doc/includes/references.h b/doc/includes/references.h index 3193e820..cb9013d1 100644 --- a/doc/includes/references.h +++ b/doc/includes/references.h @@ -3096,6 +3096,7 @@ void mj_defaultVisual(mjVisual* vis); mjModel* mj_copyModel(mjModel* dest, const mjModel* src); void mj_saveModel(const mjModel* m, const char* filename, void* buffer, int buffer_sz); mjModel* mj_loadModel(const char* filename, const mjVFS* vfs); +mjModel* mj_loadModelBuffer(const void* buffer, int buffer_sz); void mj_deleteModel(mjModel* m); mjtSize mj_sizeModel(const mjModel* m); mjData* mj_makeData(const mjModel* m); diff --git a/include/mujoco/mujoco.h b/include/mujoco/mujoco.h index da43efc5..1d517431 100644 --- a/include/mujoco/mujoco.h +++ b/include/mujoco/mujoco.h @@ -212,6 +212,9 @@ MJAPI void mj_saveModel(const mjModel* m, const char* filename, void* buffer, in // Nullable: vfs MJAPI mjModel* mj_loadModel(const char* filename, const mjVFS* vfs); +// Load model from memory buffer. +MJAPI mjModel* mj_loadModelBuffer(const void* buffer, int buffer_sz); + // Free memory allocation in model. MJAPI void mj_deleteModel(mjModel* m); diff --git a/python/mujoco/introspect/functions.py b/python/mujoco/introspect/functions.py index b10979f4..4110a182 100644 --- a/python/mujoco/introspect/functions.py +++ b/python/mujoco/introspect/functions.py @@ -839,6 +839,26 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ ), doc='Load model from binary MJB file. If vfs is not NULL, look up file in vfs before reading from disk.', # pylint: disable=line-too-long )), + ('mj_loadModelBuffer', + FunctionDecl( + name='mj_loadModelBuffer', + return_type=PointerType( + inner_type=ValueType(name='mjModel'), + ), + parameters=( + FunctionParameterDecl( + name='buffer', + type=PointerType( + inner_type=ValueType(name='void', is_const=True), + ), + ), + FunctionParameterDecl( + name='buffer_sz', + type=ValueType(name='int'), + ), + ), + doc='Load model from memory buffer.', + )), ('mj_deleteModel', FunctionDecl( name='mj_deleteModel', diff --git a/src/engine/engine_io.h b/src/engine/engine_io.h index a2d7bc34..a5410f86 100644 --- a/src/engine/engine_io.h +++ b/src/engine/engine_io.h @@ -69,8 +69,8 @@ MJAPI void mjv_copyModel(mjModel* dest, const mjModel* src); // save model to binary file MJAPI void mj_saveModel(const mjModel* m, const char* filename, void* buffer, int buffer_sz); -// load binary MJB -mjModel* mj_loadModelBuffer(const void* buffer, int buffer_sz); +// load model from binary buffer +MJAPI mjModel* mj_loadModelBuffer(const void* buffer, int buffer_sz); // deallocate model MJAPI void mj_deleteModel(mjModel* m); diff --git a/unity/Runtime/Bindings/MjBindings.cs b/unity/Runtime/Bindings/MjBindings.cs index 09123d88..0553fd8a 100644 --- a/unity/Runtime/Bindings/MjBindings.cs +++ b/unity/Runtime/Bindings/MjBindings.cs @@ -6444,6 +6444,9 @@ public static unsafe extern void mj_saveModel(mjModel_* m, [MarshalAs(UnmanagedT [DllImport("mujoco", CallingConvention = CallingConvention.Cdecl)] public static unsafe extern mjModel_* mj_loadModel([MarshalAs(UnmanagedType.LPStr)]string filename, void* vfs); +[DllImport("mujoco", CallingConvention = CallingConvention.Cdecl)] +public static unsafe extern mjModel_* mj_loadModelBuffer(void* buffer, int buffer_sz); + [DllImport("mujoco", CallingConvention = CallingConvention.Cdecl)] public static unsafe extern void mj_deleteModel(mjModel_* m); diff --git a/wasm/codegen/generators/constants.py b/wasm/codegen/generators/constants.py index f4a55f5e..1cb0ef23 100644 --- a/wasm/codegen/generators/constants.py +++ b/wasm/codegen/generators/constants.py @@ -139,6 +139,7 @@ _SKIPPED_MEMORY_FUNCTIONS: tuple[str, ...] = ( "mj_freeLastXML", "mj_freeStack", "mj_loadModel", + "mj_loadModelBuffer", "mj_markStack", "mj_saveModel", "mj_stackAllocByte",