@hackbuild/humanoid 0.0.0-stage → 0.1.0

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/llms.md ADDED
@@ -0,0 +1,284 @@
1
+ # @hackbuild/humanoid
2
+
3
+ > Run a physics humanoid in the page: MuJoCo simulates a robot from its MJCF files, and you set its joint targets, shove it and draw it.
4
+
5
+ Generated by tools/llms.mjs from the TypeDoc JSON, the README and the examples of hackbuild-motion. Human docs: https://hackbuild.github.io/hackbuild-motion/docs/humanoid/
6
+
7
+ ## Purpose
8
+
9
+ - Loads MuJoCo, Google DeepMind's WebAssembly build (`@mujoco/mujoco`, single threaded, so the page needs no cross-origin isolation), at run time: from a factory your bundler imports, or from a URL. It is never bundled.
10
+ - Reads a robot from the URL of its MJCF or from a dropped folder, with every file the MJCF names (includes, meshes, textures), keyed the way MuJoCo's virtual file system asks for them; files that do not arrive are listed.
11
+ - Steps the robot at the model's own timestep however the page's frames fall, resets it to a keyframe, writes joint targets to its position actuators, holds its current pose, shoves it, and reads any body's world pose.
12
+ - Draws it in three.js from the `./three` subpath: one mesh per visible geom, MuJoCo's meshes shared, turned from MuJoCo's z up into y up with the robot facing +Z.
13
+ - Names the Unitree G1 (29 joints) from MuJoCo Menagerie, pinned to one commit, as a ready robot; its files load from GitHub at run time.
14
+
15
+ ## Install
16
+
17
+ ```sh
18
+ npm install @hackbuild/humanoid
19
+ ```
20
+
21
+ It has no dependencies. MuJoCo and three.js are optional peers: install `@mujoco/mujoco` to import its factory, or let `loadMujoco` load it from `https://cdn.jsdelivr.net/npm/@mujoco/mujoco@4.0.1/mujoco.js`, with `mujoco.wasm` read beside it. `onLicence` receives the line to show. The G1's files come from GitHub when you load it.
22
+
23
+ | download | size |
24
+ |---|---|
25
+ | mujoco.wasm | 11.2 MB, 2.8 MB gzip (measured) |
26
+ | the G1's meshes | 19.7 MB (vendor) |
27
+
28
+ ## Exports
29
+
30
+ ### @hackbuild/humanoid
31
+
32
+ ```ts
33
+ function fetchModel(url: string, o?: { fetch?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>; onFile?: (path: string, done: number, of: number) => void }): Promise<ModelFiles>
34
+ ```
35
+
36
+ A robot from the URL of its main MJCF; every file it names is fetched beside it.
37
+
38
+ ```ts
39
+ function filesFrom(list: Iterable<File>): Promise<ModelFiles>
40
+ ```
41
+
42
+ A robot from dropped files (a folder keeps its paths): the main MJCF is the one with a worldbody that no other file includes.
43
+
44
+ ```ts
45
+ const G1: { readonly bytes: 19666440; readonly joints: readonly ["left_hip_pitch_joint", "left_hip_roll_joint", "left_hip_yaw_joint", "left_knee_joint", "left_ankle_pitch_joint", "left_ankle_roll_joint", "right_hip_pitch_joint", "right_hip_roll_joint", "right_hip_yaw_joint", "right_knee_joint", "right_ankle_pitch_joint", "right_ankle_roll_joint", "waist_yaw_joint", "waist_roll_joint", "waist_pitch_joint", "left_shoulder_pitch_joint", "left_shoulder_roll_joint", "left_shoulder_yaw_joint", "left_elbow_joint", "left_wrist_roll_joint", "left_wrist_pitch_joint", "left_wrist_yaw_joint", "right_shoulder_pitch_joint", "right_shoulder_roll_joint", "right_shoulder_yaw_joint", "right_elbow_joint", "right_wrist_roll_joint", "right_wrist_pitch_joint", "right_wrist_yaw_joint"]; readonly keyframe: "stand"; readonly licence: "unitree g1 model and meshes from mujoco menagerie, bsd-3-clause, copyright unitree robotics. they load at run time and are not part of this library."; readonly name: "unitree g1, 29 joints"; readonly url: "https://raw.githubusercontent.com/google-deepmind/mujoco_menagerie/98a70c569c4c1b6da467a20012f766ee1f8dc521/unitree_g1/scene.xml" }
46
+ ```
47
+
48
+ The G1 from MuJoCo Menagerie, by URL, with its licence and download size.
49
+
50
+ ```ts
51
+ class Humanoid {
52
+ readonly actuated: string[];
53
+ readonly bodies: string[];
54
+ readonly data: MjData;
55
+ readonly joints: string[];
56
+ readonly keyframes: string[];
57
+ readonly missing: string[];
58
+ readonly model: MjModel;
59
+ readonly timestep: number;
60
+ readonly moving: boolean;
61
+ readonly rootBody: string | null;
62
+ readonly targets: Float64Array;
63
+ readonly time: number;
64
+ dispose(): void;
65
+ hold(): void;
66
+ joint(name: string): { dq: number; q: number } | null;
67
+ moveTo(targets: Record<string, number> | ArrayLike<number>, seconds?: number): void;
68
+ pose(body: string | number): Pose | null;
69
+ push(vx: number, vy: number, vz?: number): boolean;
70
+ reset(key?: string | number): void;
71
+ step(seconds: number, max?: number): number;
72
+ static open(mj: Mujoco, files: Pick<ModelFiles, "xml" | "assets"> & { missing?: string[] }): Humanoid;
73
+ }
74
+ ```
75
+
76
+ A robot running in MuJoCo.
77
+
78
+ ```ts
79
+ class HumanoidError extends Error {
80
+ constructor(message: string);
81
+ }
82
+ ```
83
+
84
+ A model or runtime this library cannot open, with the reason in words a page can show.
85
+
86
+ ```ts
87
+ function loadMujoco(o?: MujocoOptions): Promise<Mujoco>
88
+ ```
89
+
90
+ Loads MuJoCo once per factory or URL and resolves to the module.
91
+
92
+ ```ts
93
+ interface MjData {
94
+ ctrl: Float64Array;
95
+ geom_xmat: Float64Array;
96
+ geom_xpos: Float64Array;
97
+ qpos: Float64Array;
98
+ qvel: Float64Array;
99
+ time: number;
100
+ xpos: Float64Array;
101
+ xquat: Float64Array;
102
+ delete(): void;
103
+ }
104
+ ```
105
+
106
+ A model's state; the arrays are views on the wasm heap, live after each step.
107
+
108
+ ```ts
109
+ interface MjModel {
110
+ actuator_trnid: Int32Array;
111
+ geom_dataid: Int32Array;
112
+ geom_group: Int32Array;
113
+ geom_rgba: Float32Array;
114
+ geom_size: Float64Array;
115
+ geom_type: Int32Array;
116
+ jnt_bodyid: Int32Array;
117
+ jnt_dofadr: Int32Array;
118
+ jnt_qposadr: Int32Array;
119
+ jnt_type: Int32Array;
120
+ mesh_face: Int32Array;
121
+ mesh_faceadr: Int32Array;
122
+ mesh_facenum: Int32Array;
123
+ mesh_vert: Float32Array;
124
+ mesh_vertadr: Int32Array;
125
+ mesh_vertnum: Int32Array;
126
+ nbody: number;
127
+ ngeom: number;
128
+ njnt: number;
129
+ nkey: number;
130
+ nmesh: number;
131
+ nq: number;
132
+ nu: number;
133
+ nv: number;
134
+ opt: { timestep: number };
135
+ delete(): void;
136
+ }
137
+ ```
138
+
139
+ A compiled model: sizes, options and the arrays this library reads.
140
+
141
+ ```ts
142
+ interface MjVFS {
143
+ addBuffer(name: string, bytes: Uint8Array): void;
144
+ delete(): void;
145
+ }
146
+ ```
147
+
148
+ MuJoCo's virtual file system, for the files a model names.
149
+
150
+ ```ts
151
+ interface ModelFiles {
152
+ assets: Map<string, Uint8Array<ArrayBufferLike>>;
153
+ missing: string[];
154
+ xml: string;
155
+ }
156
+ ```
157
+
158
+ A robot's main MJCF and the files it names.
159
+
160
+ ```ts
161
+ interface Mujoco {
162
+ MjData: new (model: MjModel) => MjData;
163
+ MjModel: { from_xml_string(xml: string, vfs?: MjVFS): MjModel };
164
+ mjtObj: Record<string, { value: number }>;
165
+ MjVFS: new () => MjVFS;
166
+ mj_forward(model: MjModel, data: MjData): void;
167
+ mj_id2name(model: MjModel, type: number, id: number): string;
168
+ mj_resetData(model: MjModel, data: MjData): void;
169
+ mj_resetDataKeyframe(model: MjModel, data: MjData, key: number): void;
170
+ mj_step(model: MjModel, data: MjData): void;
171
+ }
172
+ ```
173
+
174
+ The MuJoCo module: the classes and functions this library calls.
175
+
176
+ ```ts
177
+ const MUJOCO_LICENCE: "mujoco by google deepmind, under the apache license 2.0 (https://github.com/google-deepmind/mujoco). it loads at run time and is not part of this library."
178
+ ```
179
+
180
+ The line a page shows the first time MuJoCo loads.
181
+
182
+ ```ts
183
+ const MUJOCO_URL: "https://cdn.jsdelivr.net/npm/@mujoco/mujoco@4.0.1/mujoco.js"
184
+ ```
185
+
186
+ Where MuJoCo loads from when no factory is given.
187
+
188
+ ```ts
189
+ const MUJOCO_VERSION: "4.0.1"
190
+ ```
191
+
192
+ The @mujoco/mujoco version the default URL pins.
193
+
194
+ ```ts
195
+ interface MujocoOptions {
196
+ factory?: (options?: Record<string, unknown>) => Promise<unknown>;
197
+ onLicence?: (line: string) => void;
198
+ url?: string;
199
+ wasm?: string | ArrayBuffer | Uint8Array<ArrayBufferLike>;
200
+ }
201
+ ```
202
+
203
+ Options for loadMujoco.
204
+
205
+ ```ts
206
+ interface Pose {
207
+ pos: [number, number, number];
208
+ quat: [number, number, number, number];
209
+ }
210
+ ```
211
+
212
+ A body's world pose: position x y z and quaternion w x y z, in MuJoCo's frame.
213
+
214
+ ```ts
215
+ function referencedFiles(xml: string, dirs?: { assetdir?: string; meshdir?: string; texturedir?: string }): { files: string[]; includes: string[] }
216
+ ```
217
+
218
+ The files one MJCF names, split into includes and the rest, joined onto its compiler's directories.
219
+
220
+ ### @hackbuild/humanoid/three
221
+
222
+ ```ts
223
+ class HumanoidView {
224
+ constructor(sim: Humanoid, o?: HumanoidViewOptions);
225
+ readonly root: Group<Object3DEventMap>;
226
+ readonly count: number;
227
+ dispose(): void;
228
+ update(): void;
229
+ }
230
+ ```
231
+
232
+ A three.js group that follows a Humanoid; add `root` to a scene and call `update` after each step.
233
+
234
+ ```ts
235
+ interface HumanoidViewOptions {
236
+ floor?: boolean;
237
+ forward?: "x" | "-x" | "y" | "-y";
238
+ groups?: number[];
239
+ }
240
+ ```
241
+
242
+ Options for HumanoidView.
243
+
244
+ ## Examples
245
+
246
+ ### quick start
247
+
248
+ ```ts
249
+ import * as THREE from "three";
250
+ import { G1, Humanoid, fetchModel, loadMujoco } from "@hackbuild/humanoid";
251
+ import { HumanoidView } from "@hackbuild/humanoid/three";
252
+
253
+ const renderer = new THREE.WebGLRenderer({ antialias: true });
254
+ renderer.setSize(innerWidth, innerHeight);
255
+ document.body.append(renderer.domElement);
256
+ const scene = new THREE.Scene().add(new THREE.HemisphereLight(0xffffff, 0x887766, 2));
257
+ const camera = new THREE.PerspectiveCamera(35, innerWidth / innerHeight, 0.05, 50).translateZ(2.6).translateY(0.8);
258
+
259
+ // mujoco loads from jsdelivr and the g1's files from github, at run time
260
+ const mj = await loadMujoco({ onLicence: (line) => console.info(line) });
261
+ const robot = Humanoid.open(mj, await fetchModel(G1.url));
262
+ robot.reset(G1.keyframe); // upright, arms down
263
+ const view = new HumanoidView(robot);
264
+ scene.add(view.root);
265
+ // bend the knees through the position actuators, eased over 0.8 s
266
+ robot.moveTo({ left_knee_joint: 0.6, right_knee_joint: 0.6, left_hip_pitch_joint: -0.3, right_hip_pitch_joint: -0.3 }, 0.8);
267
+ renderer.setAnimationLoop(() => (robot.step(1 / 60), view.update(), renderer.render(scene, camera)));
268
+ ```
269
+
270
+ ## Mistakes to avoid
271
+
272
+ - Do not bundle @mujoco/mujoco or copy mujoco.wasm into the package build; pass its factory from your bundler or let `loadMujoco` load it from its URL.
273
+ - Do not use the `/mt` MuJoCo build; it needs SharedArrayBuffer and cross-origin isolation. This package uses the single threaded one.
274
+ - Do not commit the G1's meshes or any robot's files to an app; load them from `G1.url` or from a drop, and show the robot's licence.
275
+ - Call `dispose()` on a Humanoid you are done with; MuJoCo's model and data live on the wasm heap and are not garbage collected.
276
+ - Read poses after `step()`; they are MuJoCo's frame (z up), and the three.js view turns them to y up for you.
277
+ - Write targets in the actuators' order (`actuated`), not the model's joint order; for the G1 they are the same 29 joints in `G1.joints`.
278
+
279
+ ## Errors this package throws
280
+
281
+ - `mujoco did not load from ...` (offline, or the URL is blocked): install `@mujoco/mujoco` and pass its factory, or serve it and pass `url`
282
+ - `mujoco could not compile the model: ...` (a named file is missing or the MJCF is invalid): keep the robot's folder structure; the message names files that did not arrive
283
+ - `the model has no keyframe ...` (a keyframe name the model lacks): use one from `keyframes`
284
+ - `no mjcf here` (the drop holds no MJCF with a worldbody): drop the robot's whole folder
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@hackbuild/humanoid",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A physics humanoid in the page: MuJoCo (Google DeepMind's WebAssembly build, loaded at run time and never bundled) runs a robot from its MJCF files, holds joint targets with its position actuators, and gives the body's pose to a three.js view. Starts with the Unitree G1 from MuJoCo Menagerie, its files loaded at run time.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "Moheeb Zara <hackbuildvideo@gmail.com>",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/hackbuild/hackbuild-motion.git",
11
+ "directory": "packages/humanoid"
12
+ },
13
+ "engines": {
14
+ "node": "^22.18.0 || >=24.11.0"
15
+ },
16
+ "sideEffects": false,
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js"
21
+ },
22
+ "./three": {
23
+ "types": "./dist/three.d.ts",
24
+ "import": "./dist/three.js"
25
+ },
26
+ "./iife": "./dist/humanoid.iife.js"
27
+ },
28
+ "unpkg": "dist/humanoid.iife.js",
29
+ "jsdelivr": "dist/humanoid.iife.js",
30
+ "files": [
31
+ "dist",
32
+ "README.md",
33
+ "llms.md",
34
+ "LICENSE",
35
+ "how.ts"
36
+ ],
37
+ "scripts": {
38
+ "build": "node ../../tools/build-package.mjs humanoid"
39
+ },
40
+ "peerDependencies": {
41
+ "@mujoco/mujoco": ">=4.0.1",
42
+ "three": ">=0.186.1"
43
+ },
44
+ "peerDependenciesMeta": {
45
+ "@mujoco/mujoco": {
46
+ "optional": true
47
+ },
48
+ "three": {
49
+ "optional": true
50
+ }
51
+ }
52
+ }