Commit Graph

54 Commits

Author SHA1 Message Date
Sam Haves 4cfebcc32b Add mj_containsFileVFS and mj_containsBufferVFS functions.
PiperOrigin-RevId: 903785790
Change-Id: I013b37a177284f8440179c4ae5c6221e0f572b49
2026-04-22 05:27:15 -07:00
Sam Haves f5d3ce3451 Introduce mjpEncoder plugin architecture
Add a new mjpEncoder plugin type mirroring the existing mjpDecoder pattern.
Encoders serialize an mjSpec + mjModel to an mjResource for a given format.

New API functions:
- mjp_registerEncoder: globally register an encoder
- mjp_defaultEncoder: zero-initialize an encoder struct
- mjp_findEncoder: look up an encoder by filename extension or content type

The mjfEncode callback takes (mjSpec*, mjModel*, mjVFS*, mjResource*) and
returns 0 on success. Writing to mjResource keeps symmetry with the decoder
reading from mjResource and leaves the door open for writable resource providers.

PiperOrigin-RevId: 889187898
Change-Id: I180771b2255b91dea188ac5e2cdc3a8f0fb85364
2026-03-25 05:35:39 -07:00
Google DeepMind b294d15558 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
2026-03-12 07:35:47 -07:00
Google DeepMind df0bc58578 Add aliases for MjModel loading functions and expose string constants as properties.
Introduces `from_xml_path` and `from_binary_path` as more idiomatic aliases for `mj_loadXML` and `mj_loadModel` respectively. Refactors the binding of `mj*STRING` constants. Instead of exposing `get_mj*STRING` functions, these constants are now available as properties on the `Module` object. Runtime getters are added via EM_ASM to lazily call the underlying C++ functions.

PiperOrigin-RevId: 882566011
Change-Id: Id3bd5c23704fd122af30a50aed089bd3143fbc96
2026-03-12 06:58:50 -07:00
Google DeepMind a83fa7299d Add support for loading XML models with VFS in WASM bindings.
PiperOrigin-RevId: 874756898
Change-Id: If2c30aef4c690cf0852587261ca32a3b91448621
2026-02-24 13:09:20 -08:00
Sam Haves 6715a0ceaa Expose mju resource functions to public header.
Many of these functions are useful when writing a custom decoder.

PiperOrigin-RevId: 868321921
Change-Id: I7e0dd285e0559b6f486acf8cbf4376ff8d91943d
2026-02-10 14:31:41 -08:00
Yuval Tassa 6419534bad Add actuator and sensor delays. Fixes #1004
PiperOrigin-RevId: 866478839
Change-Id: Id21a6da0f98454c8fa39ea5af8a5e213d6eae497
2026-02-06 08:47:36 -08:00
Google DeepMind d2efc0e154 Add object-based accessors for mjData in WASM bindings.
PiperOrigin-RevId: 862736608
Change-Id: I0618e9d2af37e377bad1aead1b3fd1a12334df35
2026-01-29 07:55:56 -08:00
Google DeepMind 59c5017073 Improve error messages for named/indexed lookups in WASM bindings.
PiperOrigin-RevId: 862682001
Change-Id: I6381af64b724de0ab5aee43cdc89ce463b590cf1
2026-01-29 04:53:54 -08:00
Yuval Tassa 30b903b6c0 Migrate mjModel size fields from int to mjtSize.
This change updates all size-related members within the `mjModel` struct from `int` to `mjtSize`. This allows MuJoCo to handle models with a larger number of elements. Corresponding changes were made to macros, function signatures, and I/O routines to accommodate the new `mjtSize` type.

PiperOrigin-RevId: 860144595
Change-Id: I701c6d607715d240766b6210a9773cd9e4258c59
2026-01-23 09:41:49 -08:00
Google DeepMind e1992e62d7 Add mj_loadBinary binding to WASM.
This change adds an Emscripten binding for loading binary MuJoCo models (`.mjb`) from the virtual filesystem. The new binding, `mj_loadBinary`, takes a filename and an `MjVFS` object, enabling the loading of models that reference assets within the VFS.

PiperOrigin-RevId: 859740455
Change-Id: I9f9d8bbba0b57422e2356867e4282a1b04566475
2026-01-22 13:08:00 -08:00
Google DeepMind 61f13cd8f1 Add custom Emscripten binding for mj_saveModel.
Also adds support to unpack nullable string args

PiperOrigin-RevId: 859702186
Change-Id: Ia9e10f447ecf17b1dd74dd64d988790107a438a6
2026-01-22 11:36:13 -08:00
Google DeepMind c996f288e1 Add code generation for mjModel named accessors.
Most of it is done leveraging existing X Macros (indexers.h, indexer_xmacro.h) used for python bindings already.

PiperOrigin-RevId: 856205245
Change-Id: Ic72443999065040af3042bbf3f9f68c5b70d309d
2026-01-14 07:31:29 -08:00
Google DeepMind 37762e3f70 Add support for vfs assets in mj_compile function to MuJoCo WASM.
Regarding mj_compile VFS argument:
I was encountering a memory error when using `std::optional<MjVFS>` with `mj_compile` (because vfs should be an optional argument of the function). This is likely because `MjVFS` doesn't have copy/move constructors, which are required for types used with `std::optional`, causing issues with memory management when std::optional copies the MjVFS object.
To solve this and to make vfs an optional argument to `mj_compile`, I replaced `mj_compile_wrapper` with two overloaded functions. This avoids using `std::optional<MjVFS>` and resolves the memory issue, while still allowing mj_compile to be called with or without the vfs argument from JavaScript.

But what if we instead add those copy/move constructors so it works with `std::optional`?:
While making `MjVFS` copyable or movable would theoretically allow it to work with std::optional, it's not a feasible approach in this case.
The MjVFS struct manages file data in memory, allocating buffers when files are added and freeing them when `mj_deleteVFS` is called in its destructor. A copy would require a deep copy of the entire virtual filesystem, but MuJoCo does not provide a public API function (like a  "mj_copyVFS") to do this. Implementing a deep copy manually would be complex and fragile. A shallow copy would result in multiple MjVFS objects pointing to the same memory, leading to double-free errors when their destructors run.
Given these constraints, MjVFS cannot safely be made copyable. The approach of providing two overloaded C++ functions is better.

PiperOrigin-RevId: 855740541
Change-Id: Ia08b6db8868ad245f07145db3fb0322cb9f737cb
2026-01-13 08:36:48 -08:00
Google DeepMind 477a14dcb2 Add WASM binding for mj_compile.
It does not support model assets management via VFS yet.

PiperOrigin-RevId: 844864882
Change-Id: I422d7896ed024fc6f5c3fa923a58cdc9059f3bee
2025-12-15 11:58:26 -08:00
Haroon Qureshi caec236b2f Make mj_loadModelBuffer a public API.
PiperOrigin-RevId: 843105390
Change-Id: I4058471b4cf64995dd5ab43e9c8c24e322004b62
2025-12-11 01:24:17 -08:00
Google DeepMind 4c40d5938e Add type hints to tuple constants in codegen/generators/constants.py.
`tuple` is better than `list`  for constants if we don't expect they get modified by other parts of the code

PiperOrigin-RevId: 838748519
Change-Id: Ic950a78be7ad7518361c046d7e4542607070d3a2
2025-12-01 06:37:03 -08:00
Google DeepMind 8a5c52d395 Use a dedicated function for wrapper struct names
This change introduces a `wrapper_struct_name` function in `structs.py` to consistently generate the name of the C++ wrapper struct from a C struct name. All call sites in `functions.py` and `structs.py` that previously used `common.capitalize` for this purpose now call `wrapper_struct_name`.

PiperOrigin-RevId: 837544242
Change-Id: Ibe5fea8294283dae63bb8d064fd8beb8cfd3f2fe
2025-11-27 08:42:33 -08:00
Matias Manevi 9ca1598b23 Refactor MuJoCo WASM struct codegen.
PiperOrigin-RevId: 837530063
Change-Id: I57518b2fbb536f356a5e15839d8e17f455bccde9
2025-11-27 07:42:41 -08:00
Matija Kecman 05f194c412 Reorganization of the WASM bindings.cc template
* Move all bindings into a single EMSCRIPTEN_BINDINGS block
* Move all auto generated bindings line to the start of the block
* Alphabetically sort the constants binding lines

PiperOrigin-RevId: 837142009
Change-Id: I218a37dbc0ca6c6f16a455718889c08fe1b41d56
2025-11-26 08:43:51 -08:00
Matija Kecman 181dcab6be Combine markers and content and reorganize parts of the WASM template file
PiperOrigin-RevId: 836729951
Change-Id: Ib3bb25a23325cfbd84fd0439243db37ca8236bf3
2025-11-25 10:40:44 -08:00
Google DeepMind 6e59f61c70 Make WASM codegen output to be alphabetically ordered.
PiperOrigin-RevId: 836680575
Change-Id: I7ddb7267c0db4911b2e84528f948dee6fccdf81d
2025-11-25 08:29:30 -08:00
Google DeepMind 711a388b3d Sort and generate optional struct bindings in WASM.
PiperOrigin-RevId: 836273537
Change-Id: I2af2401969a4780f593df86dc9336adcc285fd1b
2025-11-24 10:18:09 -08:00
Google DeepMind 217317ccba Change WASM generators from classes to functions.
PiperOrigin-RevId: 836257289
Change-Id: I7a3c1d7bb545353a3d278330582e33c0551792cb
2025-11-24 09:36:36 -08:00
Matija Kecman ee8dbd4347 Simplify enum WASM bindings generator
PiperOrigin-RevId: 834725472
Change-Id: I278447145cd8dcdd145f0d50bc3bae649ea2b440
2025-11-20 05:52:02 -08:00
Matija Kecman a0d49ade1b More simplification to functions.py in WASM bindings
PiperOrigin-RevId: 834643175
Change-Id: Iff873f6f0916e0039ff6a44468afd9b4d8f98f37
2025-11-20 01:06:29 -08:00
Matija Kecman 102f382c9d Rename uppercase_first_letter to capitalize
Also rename `lowercase_first_letter` to `decapitalize`

PiperOrigin-RevId: 834622292
Change-Id: I1cfbda1be782ff40dd3fc0726601cc6b1f427345
2025-11-19 23:54:02 -08:00
Matija Kecman 48ecddbe3b Simplify functions.py in WASM bindings generation
PiperOrigin-RevId: 834470193
Change-Id: I3c26d65ea8812fd6a4c78264fbce8353bac6fb2f
2025-11-19 15:35:45 -08:00
Google DeepMind 49394d57d9 Rename WASM binding wrapper functions and reorder generated code.
PiperOrigin-RevId: 834265965
Change-Id: I58e7d572dcae4f1ca262ae5d876fcd8353af183c
2025-11-19 06:26:03 -08:00
Google DeepMind 10fe5ff07f Generate MjVisual, MjModel, MjData and MjSpec structs declaration
PiperOrigin-RevId: 834264723
Change-Id: Id33d7568a1f9f0cfb1271cf9dd2d8713c00eee34
2025-11-19 06:22:28 -08:00
Matias Manevi 53e254b715 Generate using declarations for anonymous structs in WASM bindings.
PiperOrigin-RevId: 832359379
Change-Id: Ie5913c584c5605f512dbe423524a829043179b6f
2025-11-14 10:03:12 -08:00
Matias Manevi a36f452250 Refactor MuJoCo WASM bindings functions code generation and auto-generate functions requiring argument boundchecks.
PiperOrigin-RevId: 832333773
Change-Id: Id2e1a59e8d366baa2ee044d693f76aadcd1dfd32
2025-11-14 08:51:18 -08:00
Matias Manevi fa79276290 Remove destructors from Mjs wrapper structs.
PiperOrigin-RevId: 831001833
Change-Id: I54550737a4c1cb504bf62b9bfed12d7a70434233
2025-11-11 11:31:21 -08:00
Google DeepMind 6d261528e0 Enhance error reporting in WASM codegen generators.
In `structs.py`, a `RuntimeError` is now raised when an anonymous struct is encountered but is not present in `STRUCTS_TO_BIND`.

PiperOrigin-RevId: 830978376
Change-Id: Id64720a12c3cce313bd513a40a74ddf57718a7d1
2025-11-11 10:38:39 -08:00
Google DeepMind 8e4a5901c3 Rename internal lists of skipped functions in constants.py.
PiperOrigin-RevId: 830976939
Change-Id: I833ef4c98107e19822f5e96c7541d294e129ac6c
2025-11-11 10:35:22 -08:00
Matias Manevi 54661ab140 Fully generate Emscripten bindings for MuJoCo functions.
PiperOrigin-RevId: 830975595
Change-Id: Ie4d0d16c80f490abaa5ae8218628ac1b3d32c0ef
2025-11-11 10:32:04 -08:00
Matias Manevi 3e167e0f0b Correctly format constructor initializer lists in struct wrappers.
PiperOrigin-RevId: 830887996
Change-Id: Ibe663fcc17571ad1fd62d70249687c917653618e
2025-11-11 06:38:19 -08:00
Matias Manevi 75774b2a35 Simplify struct header generation by merging the internal _build_struct_header_internal function into build_struct_header
PiperOrigin-RevId: 830878041
Change-Id: I6867984ac6ec091daaf2172fbde0e6ac6f38d1f3
2025-11-11 06:03:13 -08:00
Matija Kecman 4e46db8903 Refactor WASM binding directory structure
* All tests moved into wasm/codegen/tests folder
* Merged wasm/codegen/helpers/ into wasm/codegen/generators/

PiperOrigin-RevId: 829471173
Change-Id: I2dbc5d9351771817ec260c7ddf87c31e66a9eabe
2025-11-07 09:37:26 -08:00
Matija Kecman 44220fcc51 Refactor WASM bindings in preparation for codegen directory structure update
PiperOrigin-RevId: 829459535
Change-Id: I4045e163a6e7fcee9d9585131d1e5e30f0b50b89
2025-11-07 09:02:43 -08:00
Matias Manevi 06ecdd6ba1 Refactor: Generate all struct bindings within the codegen.
PiperOrigin-RevId: 828973537
Change-Id: I3f371d2f88e1cd9b1bb5c0a9c6a5ace7a21673fb
2025-11-06 08:49:48 -08:00
Google DeepMind 3577c98f82 Refactor Emscripten bindings generation for MuJoCo structs.
PiperOrigin-RevId: 828550215
Change-Id: Iab0bf3a9fbaca4d7774f89a4d8977e762aa57629
2025-11-05 11:14:29 -08:00
Google DeepMind 8104235f8e Generate wrappers for anonymous structs within mjVisual.
PiperOrigin-RevId: 828548681
Change-Id: I07f8b407157b91ac24b38604b20826541e20e23e
2025-11-05 11:10:52 -08:00
Google DeepMind fe1b6b7c80 Refactor: Group autogenerated structs source in bindings.cc
This change modifies the code generation for structs in `bindings.cc`. Instead of placing each struct's constructor and member functions under individual markers, all autogenerated struct source code is now grouped together under a single `// {{ AUTOGENNED_STRUCTS_SOURCE }}` marker. This improves the organization of the generated file and simplifies the template.

Future work would move the rest of the structs into this pattern

PiperOrigin-RevId: 828390272
Change-Id: Iee9d0e8766db86b2c1d4296a37ac7a8e6e05428b
2025-11-05 03:26:09 -08:00
Google DeepMind 5b00b033af Refactor struct dependency sorting to use wrapped field data.
The `sort_structs_by_dependency` function now takes a dictionary of `WrappedStructData` and uses the pre-computed `typename` within `WrappedFieldData` for dependency analysis, instead of re-introspecting the AST. This change also includes populating the `typename` in various `WrappedFieldData` generation methods.

PiperOrigin-RevId: 828389481
Change-Id: Ie97c1a66e5e69bd148a928fda26afdaa09f56687
2025-11-05 03:23:35 -08:00
Matija Kecman 29c1a3a0a5 Cleanup WASM bindings generators so they all have a similar interface
PiperOrigin-RevId: 827965451
Change-Id: I657fc4d3a480fbc62124c84fe9ddc0900a91183f
2025-11-04 07:32:32 -08:00
Matija Kecman f808632cf8 Rename WASM bindings function helper file
PiperOrigin-RevId: 827897836
Change-Id: Ifb13ebaf54dacabc2a3ed8a8ea4244ecea2a8f41
2025-11-04 03:57:03 -08:00
Matija Kecman 12a31674a3 Consolidate WASM bindings struct generator helper files into helpers/structs.py
PiperOrigin-RevId: 827889180
Change-Id: I5f8bf0e25d1af7b21b554672bd438bbcd04e62be
2025-11-04 03:23:24 -08:00
Matija Kecman 9b71fc728d Consolidate WASM bindings test files
PiperOrigin-RevId: 827888382
Change-Id: I4ded54cd49182de0b4243770e2507038ad6fac7a
2025-11-04 03:19:35 -08:00
Matija Kecman 3d59b20b65 Simplified struct code generation for WASM bindings
PiperOrigin-RevId: 827878780
Change-Id: I97ab732eb11c63a6aaf7fa95add0b51bc0a56525
2025-11-04 02:51:11 -08:00