mujoco 3.2.3 → 3.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/README.md +0 -208
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mujoco",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.5",
|
|
4
4
|
"description": "MuJoCo WASM bindings",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,8 +15,9 @@
|
|
|
15
15
|
"mujoco",
|
|
16
16
|
"physics",
|
|
17
17
|
"wasm",
|
|
18
|
+
"google",
|
|
19
|
+
"deepmind",
|
|
18
20
|
"webassembly",
|
|
19
|
-
"bindings",
|
|
20
21
|
"simulation",
|
|
21
22
|
"robotics",
|
|
22
23
|
"emscripten",
|
package/README.md
DELETED
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
# MuJoCo WASM bindings
|
|
2
|
-
|
|
3
|
-
Official WebAssembly (WASM) bindings for the MuJoCo physics engine, compiled from the MuJoCo C/C++ sources into WASM with JS glue (Emscripten + Embind). The package ships prebuilt ESM-ready JS/WASM artifacts and TypeScript types for immediate use.
|
|
4
|
-
|
|
5
|
-
**Important**:_These bindings are still a WIP_.
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
```sh
|
|
9
|
-
npm install mujoco
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
## Quick start (ESM / TypeScript)
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
import loadMujoco from 'mujoco';
|
|
16
|
-
|
|
17
|
-
const mujoco = await loadMujoco();
|
|
18
|
-
|
|
19
|
-
const model = mujoco.MjModel.fromXMLString(`
|
|
20
|
-
<mujoco>
|
|
21
|
-
<worldbody>
|
|
22
|
-
<geom type="sphere" size="0.1"/>
|
|
23
|
-
</worldbody>
|
|
24
|
-
</mujoco>
|
|
25
|
-
`);
|
|
26
|
-
|
|
27
|
-
const data = new mujoco.MjData(model);
|
|
28
|
-
mujoco.mj_step(model, data);
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Usage Guide
|
|
32
|
-
When interacting with MuJoCo objects through the WASM bindings, it's important to understand how data is accessed. Properties on objects like `MjModel` and `MjData` can expose data in two ways: by copy or by reference.
|
|
33
|
-
|
|
34
|
-
### Copy vs. Reference
|
|
35
|
-
|
|
36
|
-
#### 1. By Copy (Value-based access)
|
|
37
|
-
|
|
38
|
-
Some properties return a copy of the data at the time of access. This is common for complex data structures that need to be marshalled from C++ to JavaScript.
|
|
39
|
-
|
|
40
|
-
A key example is `MjData.contact`. When you access `data.contact`, you get a new array containing the contacts at that specific moment in the simulation. If you step the simulation forward, this array will not be updated. You must access `data.contact` again to get the new contact information.
|
|
41
|
-
|
|
42
|
-
Example:
|
|
43
|
-
```typescript
|
|
44
|
-
// Gets contacts at the current time.
|
|
45
|
-
const contacts = data.contact;
|
|
46
|
-
|
|
47
|
-
// Step the simulation
|
|
48
|
-
mujoco.mj_step(model, data);
|
|
49
|
-
|
|
50
|
-
// `contacts` is now stale. To get the new contacts, you must access the property again:
|
|
51
|
-
const newContacts = data.contact;
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
#### 2. By Reference (View-based access)
|
|
55
|
-
Many properties, especially large numerical arrays, return a live view directly into the WebAssembly memory. This is highly efficient as it avoids copying large amounts of data.
|
|
56
|
-
|
|
57
|
-
A key example is `MjData.qpos` (joint positions). When you get a reference to this array, it points directly to the simulation's state data. Any changes in the simulation (e.g., after a call to `mj_step`) will be immediately reflected in this array.
|
|
58
|
-
|
|
59
|
-
```typescript
|
|
60
|
-
// `qpos` is a live view into the simulation state.
|
|
61
|
-
const qpos = data.qpos;
|
|
62
|
-
|
|
63
|
-
console.log(qpos[0]); // Print initial position
|
|
64
|
-
|
|
65
|
-
// Step the simulation
|
|
66
|
-
mujoco.mj_step(model, data);
|
|
67
|
-
|
|
68
|
-
// `qpos` is automatically updated.
|
|
69
|
-
console.log(qpos[0]); // Print new position
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Data Layout: Row-Major Matrices
|
|
73
|
-
When a function from the MuJoCo C API returns a matrix (or needs a matrix as input), these are represented in the JavaScript bindings as flat, one-dimensional `TypedArray`'s. The elements are stored in row-major order.
|
|
74
|
-
|
|
75
|
-
For example, a 3x10 matrix will be returned as a flat array with 30 elements. The first 10 elements represent the first row, the next 10 represent the second row, and so on.
|
|
76
|
-
|
|
77
|
-
Example: Accessing an element at `(row, col)`
|
|
78
|
-
```typescript
|
|
79
|
-
// A 3x10 matrix stored as a flat array.
|
|
80
|
-
const matrix: Float64Array = ...;
|
|
81
|
-
const nRows = 3;
|
|
82
|
-
const nCols = 10;
|
|
83
|
-
|
|
84
|
-
// To access the element at row `i` and column `j`:
|
|
85
|
-
const element = matrix[i * nCols + j];
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### Working with Out Parameters
|
|
89
|
-
Many functions in the MuJoCo C API use "out parameters" to return data. This means instead of returning a value, they write the result into one of the arguments passed to them by reference (using pointers). In our JavaScript bindings, you'll need to handle these cases specifically.
|
|
90
|
-
|
|
91
|
-
There are two main scenarios you'll encounter:
|
|
92
|
-
|
|
93
|
-
#### 1. Array-like Out Parameters
|
|
94
|
-
When a function expects a pointer to a primitive type (like `mjtNum*` or `int*`) to write an array of values, you need to pre-allocate memory for the result on the JavaScript side. We provide helper classes for this: `mujoco.Uint8Buffer`, `mujoco.DoubleBuffer`, `mujoco.FloatBuffer`, and `mujoco.IntBuffer`.
|
|
95
|
-
|
|
96
|
-
Here's how to use them:
|
|
97
|
-
|
|
98
|
-
1. Create a buffer: Instantiate the appropriate buffer class with an initial array of the correct size (e.g., an array of zeros).
|
|
99
|
-
2. Call the function: Pass the buffer instance to the function as the out parameter.
|
|
100
|
-
3. Access the result: Use the `.getView()` method on the buffer to get a `TypedArray` view of the data written by the C++ function.
|
|
101
|
-
4. Free the memory: When you are done with the buffer, you must call the `.delete()` method to free the underlying memory and prevent memory leaks.
|
|
102
|
-
|
|
103
|
-
Example: Rotating a vector
|
|
104
|
-
|
|
105
|
-
The function `mju_rotVecQuat` rotates a vector `vec` by a quaternion `quat` and stores the result in the `res` out parameter.
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
// Create a buffer to hold the 3D vector result.
|
|
109
|
-
const res = new mujoco.DoubleBuffer([0, 0, 0]);
|
|
110
|
-
|
|
111
|
-
const vec = [1, 0, 0];
|
|
112
|
-
const quat = [0.707, 0, 0, 0.707]; // 90-degree rotation around z-axis
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
// Call the function with the buffer as the out parameter.
|
|
116
|
-
mujoco.mju_rotVecQuat(res, vec, quat);
|
|
117
|
-
|
|
118
|
-
// Get the result as a Float64Array.
|
|
119
|
-
const resultView = res.getView();
|
|
120
|
-
console.log(resultView); // Expected: approximately [0, 1, 0]
|
|
121
|
-
|
|
122
|
-
} finally {
|
|
123
|
-
// IMPORTANT: Free the memory allocated for the buffer.
|
|
124
|
-
res.delete();
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
#### 2. Struct Out Parameters (e.g., mjvCamera*, mjvScene*)
|
|
129
|
-
When a function modifies a struct passed by pointer, you should pass an instance of the corresponding JavaScript wrapper class. The underlying C++ struct will be modified in place.
|
|
130
|
-
|
|
131
|
-
Example: Updating a scene
|
|
132
|
-
|
|
133
|
-
The function `mjv_updateScene` populates an `mjvScene` object with information from `mjModel` and `mjData`.
|
|
134
|
-
```typescript
|
|
135
|
-
// Create instances of the necessary structs.
|
|
136
|
-
const model = mujoco.MjModel.loadFromXML(xmlContent);
|
|
137
|
-
const data = new mujoco.MjData(model);
|
|
138
|
-
const scene = new mujoco.MjvScene(model, 1000);
|
|
139
|
-
const option = new mujoco.MjvOption();
|
|
140
|
-
const perturb = new mujoco.MjvPerturb();
|
|
141
|
-
const camera = new mujoco.MjvCamera();
|
|
142
|
-
|
|
143
|
-
// ... (step simulation, etc.)
|
|
144
|
-
|
|
145
|
-
// Update the scene. The 'scene' object is modified by the function.
|
|
146
|
-
mujoco.mjv_updateScene(
|
|
147
|
-
model,
|
|
148
|
-
data,
|
|
149
|
-
option,
|
|
150
|
-
perturb,
|
|
151
|
-
camera,
|
|
152
|
-
mujoco.mjtCatBit.mjCAT_ALL.value,
|
|
153
|
-
scene
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
console.log('Number of geoms in scene:', scene.ngeom);
|
|
157
|
-
|
|
158
|
-
// Remember to delete all created objects when they are no longer needed.
|
|
159
|
-
scene.delete();
|
|
160
|
-
camera.delete();
|
|
161
|
-
perturb.delete();
|
|
162
|
-
option.delete();
|
|
163
|
-
data.delete();
|
|
164
|
-
model.delete();
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
As with buffers, you are responsible for managing the memory of these struct instances and must call `.delete()` on them when you are finished.
|
|
168
|
-
|
|
169
|
-
### Enums
|
|
170
|
-
Access via `.value`:
|
|
171
|
-
```javascript
|
|
172
|
-
mujoco.mjtDisableBit.mjDSBL_CLAMPCTRL.value
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
### Constants
|
|
176
|
-
Scalar constants will be accessed the same way they are on python, simply:
|
|
177
|
-
|
|
178
|
-
```javascript
|
|
179
|
-
mujoco.mjNEQDATA
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
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:
|
|
183
|
-
|
|
184
|
-
```javascript
|
|
185
|
-
mujoco.get_mjFRAMESTRING()
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
This will return a javascript array representation of the values in MuJoCo `mjFRAMESTRING`.
|
|
189
|
-
|
|
190
|
-
## Notes
|
|
191
|
-
The package is ESM (`type: module`) and ships TypeScript types.
|
|
192
|
-
|
|
193
|
-
Ensure your bundler or dev server serves the `.wasm` asset at runtime.
|
|
194
|
-
|
|
195
|
-
### Development
|
|
196
|
-
For detailed build instructions see the repository [README](https://github.com/google-deepmind/mujoco/tree/main/wasm#mujoco-javascript-bindings) (Emscripten toolchain, Embind bindings, producing the .wasm artifact, and targets for Node and browser).
|
|
197
|
-
|
|
198
|
-
## Versioning
|
|
199
|
-
Package versions follow the official MuJoCo release versions.
|
|
200
|
-
For example:
|
|
201
|
-
|
|
202
|
-
| npm version | MuJoCo version |
|
|
203
|
-
|-------------|----------------|
|
|
204
|
-
| 3.5.0 | 3.5.0 |
|
|
205
|
-
|
|
206
|
-
---
|
|
207
|
-
|
|
208
|
-
For full engine documentation, see: https://mujoco.readthedocs.io
|