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
This commit is contained in:
Google DeepMind
2026-03-12 06:58:20 -07:00
committed by Copybara-Service
parent a62501f305
commit df0bc58578
5 changed files with 85 additions and 14 deletions
+13 -5
View File
@@ -348,23 +348,31 @@ mujoco.mjtDisableBit.mjDSBL_CLAMPCTRL.value
```
### Constants
Scalar constants will be accessed the same way they are on python, simply:
Scalar constants can be accessed as properties:
```javascript
mujoco.mjNEQDATA
```
Due to Embind limitations, more complex constants that are not scalar, but are
represented in more dimensions are exposed as functions. E.g. to use
`mujoco.mjFRAMESTRING` you will need to call a function:
Non-scalar constants like `mjFRAMESTRING` are also accessed as properties, and
return JavaScript arrays:
```javascript
mujoco.get_mjFRAMESTRING()
mujoco.mjFRAMESTRING
```
This will return a javascript array representation of the values in MuJoCo
`mjFRAMESTRING`.
> [!NOTE]
> You will notice constants like `mjFRAMESTRING` are typed as `any`. This is
> because they are bound using `emscripten::val::array()` in C++, and Embind
> maps `emscripten::val` to `any` in TypeScript definition files. While
> `EMSCRIPTEN_DECLARE_VAL_TYPE(StringArray)` could be used to define
> `StringArray` as an alias for `emscripten::val` and hint to Embind how to
> handle conversions in function signatures or when using `.as()` it does not
> change how `emscripten::constant` infers types for properties.
## Development
In order to change the bindings you will need to change the [`bindings.cc`](codegen/generated/bindings.cc)