mujoco-react 8.1.0 → 8.2.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/README.md CHANGED
@@ -8,7 +8,9 @@
8
8
 
9
9
  Composable [React Three Fiber](https://docs.pmnd.rs/react-three-fiber) wrapper around [mujoco-js](https://www.npmjs.com/package/mujoco-js). Load any MuJoCo model, step physics, render bodies, and write controllers as React components.
10
10
 
11
- **[Demo](https://mujoco-react-example.pages.dev)** | **[Docs](https://dadd.mintlify.app)** | **[Example Source](https://github.com/noah-wardlow/mujoco-react-example)** | **[llms.txt](https://dadd.mintlify.app/llms.txt)**
11
+ [![npm](https://img.shields.io/npm/v/mujoco-react)](https://www.npmjs.com/package/mujoco-react)
12
+
13
+ **[Demo](https://mujoco-react-example.pages.dev)** | **[Docs](https://dadd.mintlify.app)** | **[npm](https://www.npmjs.com/package/mujoco-react)** | **[Example Source](https://github.com/noah-wardlow/mujoco-react-example)** | **[llms.txt](https://dadd.mintlify.app/llms.txt)**
12
14
 
13
15
  ## Install
14
16
 
@@ -111,22 +113,45 @@ Drop it into the tree:
111
113
  </MujocoCanvas>
112
114
  ```
113
115
 
114
- The `createController<TConfig>()` factory adds typed config and default merging for reusable plugins:
116
+ The `createControllerHook` factory produces a typed hook with config stabilization and default merging. Pass `null` to disable:
117
+
118
+ ```tsx
119
+ import { createControllerHook, useBeforePhysicsStep } from "mujoco-react";
120
+
121
+ export const useMyController = createControllerHook<{ gain: number }, { value: number }>(
122
+ { name: "useMyController", defaultConfig: { gain: 1.0 } },
123
+ (config) => {
124
+ useBeforePhysicsStep((_model, data) => {
125
+ if (!config) return;
126
+ data.ctrl[0] = config.gain * Math.sin(data.time);
127
+ });
128
+ if (!config) return null;
129
+ return { value: config.gain };
130
+ },
131
+ );
132
+
133
+ // const result = useMyController({ gain: 2.0 });
134
+ // const disabled = useMyController(null); // returns null
135
+ ```
136
+
137
+ The `createController` factory is the component equivalent — same config stabilization, but returns a component that can render children:
115
138
 
116
139
  ```tsx
117
- import { createController, useBeforePhysicsStep } from "mujoco-react";
140
+ import { createController, useBeforePhysicsStep, Debug } from "mujoco-react";
118
141
 
119
142
  export const MyController = createController<{ gain: number }>(
120
143
  { name: "MyController", defaultConfig: { gain: 1.0 } },
121
- ({ config }) => {
144
+ ({ config, children }) => {
122
145
  useBeforePhysicsStep((_model, data) => {
123
146
  data.ctrl[0] = config.gain * Math.sin(data.time);
124
147
  });
125
- return null;
148
+ return <>{children}</>;
126
149
  },
127
150
  );
128
151
 
129
- // <MyController config={{ gain: 2.0 }} />
152
+ // <MyController config={{ gain: 2.0 }}>
153
+ // <Debug showJoints />
154
+ // </MyController>
130
155
  ```
131
156
 
132
157
  ## Architecture
@@ -727,7 +752,7 @@ The full API object available via `ref` or `useMujoco()` (when `isReady`):
727
752
 
728
753
  ### Building Controllers
729
754
 
730
- See [Building Controllers](https://dadd.mintlify.app/guides/building-controllers) for full patterns including config-driven controllers, IK gizmo coexistence, multi-arm support, and the `createController` factory.
755
+ See [Building Controllers](https://dadd.mintlify.app/guides/building-controllers) for full patterns including config-driven controllers, IK gizmo coexistence, multi-arm support, and the `createControllerHook`/`createController` factories.
731
756
 
732
757
  ### Contact Parameters
733
758
 
package/dist/index.d.ts CHANGED
@@ -790,6 +790,33 @@ declare function createController<TConfig>(options: ControllerOptions<TConfig>,
790
790
  config: TConfig;
791
791
  children?: React.ReactNode;
792
792
  }>): ControllerComponent<TConfig>;
793
+ /**
794
+ * Factory that produces a typed controller hook.
795
+ *
796
+ * Same config stabilization and default merging as `createController`,
797
+ * but returns a hook instead of a component. Pass `null` to disable.
798
+ *
799
+ * @example
800
+ * ```tsx
801
+ * const useMyController = createControllerHook<MyConfig, MyValue>(
802
+ * { name: 'useMyController', defaultConfig: { gain: 1.0 } },
803
+ * function useMyControllerImpl(config) {
804
+ * // config is MyConfig | null — hooks must be called unconditionally
805
+ * useBeforePhysicsStep((_model, data) => {
806
+ * if (!config) return;
807
+ * data.ctrl[0] = config.gain * Math.sin(data.time);
808
+ * });
809
+ * if (!config) return null;
810
+ * return { /* value *\/ };
811
+ * },
812
+ * );
813
+ *
814
+ * // Usage:
815
+ * const value = useMyController({ gain: 2.0 });
816
+ * const disabled = useMyController(null); // returns null
817
+ * ```
818
+ */
819
+ declare function createControllerHook<TConfig, TValue>(options: ControllerOptions<TConfig>, useImpl: (config: TConfig | null) => TValue | null): (config: TConfig | null) => TValue | null;
793
820
 
794
821
  /**
795
822
  * @license
@@ -1274,4 +1301,4 @@ interface CameraAnimationAPI {
1274
1301
  */
1275
1302
  declare function useCameraAnimation(): CameraAnimationAPI;
1276
1303
 
1277
- export { type ActuatorInfo, type Actuators, type Bodies, Body, type BodyInfo, type BodyProps, type BodyStateResult, type CameraAnimationAPI, type ContactInfo, ContactListener, type ContactListenerProps, ContactMarkers, type ControllerComponent, type ControllerOptions, type CtrlHandle, Debug, type DebugProps, DragInteraction, type DragInteractionProps, FlexRenderer, type GeomInfo, type Geoms, type IKSolveFn, type IkConfig, type IkContextValue, IkGizmo, type IkGizmoProps, type JointInfo, type JointStateResult, type Joints, type KeyBinding, type KeyboardTeleopConfig, type Keyframes, type ModelOptions, MujocoCanvas, type MujocoCanvasProps, type MujocoContact, type MujocoContactArray, type MujocoContextValue, type MujocoData, type MujocoModel, type MujocoModule, MujocoPhysics, type MujocoPhysicsProps, MujocoProvider, type MujocoSimAPI, MujocoSimProvider, type PhysicsConfig, type PhysicsStepCallback, type PlaybackState, type PolicyConfig, type RayHit, type Register, type SceneConfig, SceneLights, type SceneLightsProps, type SceneMarker, type SceneObject, type SensorHandle, type SensorInfo, type SensorResult, type Sensors, type SiteInfo, type SitePositionResult, type Sites, type StateSnapshot, TendonRenderer, type TrajectoryData, type TrajectoryFrame, type TrajectoryInput, TrajectoryPlayer, type TrajectoryPlayerProps, type XmlPatch, createController, findActuatorByName, findBodyByName, findGeomByName, findJointByName, findKeyframeByName, findSensorByName, findSiteByName, findTendonByName, getContact, getName, loadScene, useActuators, useAfterPhysicsStep, useBeforePhysicsStep, useBodyMeshes, useBodyState, useCameraAnimation, useContactEvents, useContacts, useCtrl, useCtrlNoise, useGamepad, useGravityCompensation, useIkController, useJointState, useKeyboardTeleop, useMujoco, useMujocoWasm, usePolicy, useSceneLights, useSelectionHighlight, useSensor, useSensors, useSitePosition, useTrajectoryPlayer, useTrajectoryRecorder, useVideoRecorder };
1304
+ export { type ActuatorInfo, type Actuators, type Bodies, Body, type BodyInfo, type BodyProps, type BodyStateResult, type CameraAnimationAPI, type ContactInfo, ContactListener, type ContactListenerProps, ContactMarkers, type ControllerComponent, type ControllerOptions, type CtrlHandle, Debug, type DebugProps, DragInteraction, type DragInteractionProps, FlexRenderer, type GeomInfo, type Geoms, type IKSolveFn, type IkConfig, type IkContextValue, IkGizmo, type IkGizmoProps, type JointInfo, type JointStateResult, type Joints, type KeyBinding, type KeyboardTeleopConfig, type Keyframes, type ModelOptions, MujocoCanvas, type MujocoCanvasProps, type MujocoContact, type MujocoContactArray, type MujocoContextValue, type MujocoData, type MujocoModel, type MujocoModule, MujocoPhysics, type MujocoPhysicsProps, MujocoProvider, type MujocoSimAPI, MujocoSimProvider, type PhysicsConfig, type PhysicsStepCallback, type PlaybackState, type PolicyConfig, type RayHit, type Register, type SceneConfig, SceneLights, type SceneLightsProps, type SceneMarker, type SceneObject, type SensorHandle, type SensorInfo, type SensorResult, type Sensors, type SiteInfo, type SitePositionResult, type Sites, type StateSnapshot, TendonRenderer, type TrajectoryData, type TrajectoryFrame, type TrajectoryInput, TrajectoryPlayer, type TrajectoryPlayerProps, type XmlPatch, createController, createControllerHook, findActuatorByName, findBodyByName, findGeomByName, findJointByName, findKeyframeByName, findSensorByName, findSiteByName, findTendonByName, getContact, getName, loadScene, useActuators, useAfterPhysicsStep, useBeforePhysicsStep, useBodyMeshes, useBodyState, useCameraAnimation, useContactEvents, useContacts, useCtrl, useCtrlNoise, useGamepad, useGravityCompensation, useIkController, useJointState, useKeyboardTeleop, useMujoco, useMujocoWasm, usePolicy, useSceneLights, useSelectionHighlight, useSensor, useSensors, useSitePosition, useTrajectoryPlayer, useTrajectoryRecorder, useVideoRecorder };
package/dist/index.js CHANGED
@@ -391,6 +391,18 @@ function sceneObjectToXml(obj) {
391
391
  const condim = obj.condim ? ` condim="${obj.condim}"` : "";
392
392
  return `<body name="${obj.name}" pos="${pos}">${joint}<geom type="${obj.type}" size="${size}" rgba="${rgba}" contype="1" conaffinity="1"${mass}${friction}${solref}${solimp}${condim}/></body>`;
393
393
  }
394
+ function ensureDir(mujoco, fname) {
395
+ const dirParts = fname.split("/");
396
+ dirParts.pop();
397
+ let currentPath = "/working";
398
+ for (const part of dirParts) {
399
+ currentPath += "/" + part;
400
+ try {
401
+ mujoco.FS.mkdir(currentPath);
402
+ } catch {
403
+ }
404
+ }
405
+ }
394
406
  async function loadScene(mujoco, config, onProgress) {
395
407
  try {
396
408
  mujoco.FS.unmount("/working");
@@ -402,66 +414,75 @@ async function loadScene(mujoco, config, onProgress) {
402
414
  }
403
415
  const baseUrl = config.src.endsWith("/") ? config.src : config.src + "/";
404
416
  const downloaded = /* @__PURE__ */ new Set();
405
- const queue = [config.sceneFile];
417
+ const xmlQueue = [config.sceneFile];
418
+ const assetFiles = [];
406
419
  const parser = new DOMParser();
407
- while (queue.length > 0) {
408
- const fname = queue.shift();
420
+ while (xmlQueue.length > 0) {
421
+ const fname = xmlQueue.shift();
409
422
  if (downloaded.has(fname)) continue;
410
423
  downloaded.add(fname);
424
+ if (!fname.endsWith(".xml")) {
425
+ assetFiles.push(fname);
426
+ continue;
427
+ }
411
428
  onProgress?.(`Downloading ${fname}...`);
412
429
  const res = await fetch(baseUrl + fname);
413
430
  if (!res.ok) {
414
431
  console.warn(`Failed to fetch ${fname}: ${res.status} ${res.statusText}`);
415
432
  continue;
416
433
  }
417
- const dirParts = fname.split("/");
418
- dirParts.pop();
419
- let currentPath = "/working";
420
- for (const part of dirParts) {
421
- currentPath += "/" + part;
422
- try {
423
- mujoco.FS.mkdir(currentPath);
424
- } catch {
425
- }
426
- }
427
- if (fname.endsWith(".xml")) {
428
- let text = await res.text();
429
- for (const patch of config.xmlPatches ?? []) {
430
- if (fname.endsWith(patch.target) || fname === patch.target) {
431
- if (patch.replace) {
432
- const [from, to] = patch.replace;
433
- if (text.includes(from)) {
434
- text = text.replace(from, to);
435
- } else {
436
- const preview = from.length > 80 ? `${from.slice(0, 80)}...` : from;
437
- console.warn(`XML patch replace pattern not found in ${fname}: "${preview}"`);
438
- }
434
+ let text = await res.text();
435
+ for (const patch of config.xmlPatches ?? []) {
436
+ if (fname.endsWith(patch.target) || fname === patch.target) {
437
+ if (patch.replace) {
438
+ const [from, to] = patch.replace;
439
+ if (text.includes(from)) {
440
+ text = text.replace(from, to);
441
+ } else {
442
+ const preview = from.length > 80 ? `${from.slice(0, 80)}...` : from;
443
+ console.warn(`XML patch replace pattern not found in ${fname}: "${preview}"`);
439
444
  }
440
- if (patch.inject && patch.injectAfter) {
441
- const idx = text.indexOf(patch.injectAfter);
442
- if (idx !== -1) {
443
- const tagEnd = text.indexOf(">", idx + patch.injectAfter.length);
444
- if (tagEnd !== -1) {
445
- text = text.slice(0, tagEnd + 1) + patch.inject + text.slice(tagEnd + 1);
446
- } else {
447
- console.warn(`XML patch inject failed in ${fname}: could not find tag end after "${patch.injectAfter}"`);
448
- }
445
+ }
446
+ if (patch.inject && patch.injectAfter) {
447
+ const idx = text.indexOf(patch.injectAfter);
448
+ if (idx !== -1) {
449
+ const tagEnd = text.indexOf(">", idx + patch.injectAfter.length);
450
+ if (tagEnd !== -1) {
451
+ text = text.slice(0, tagEnd + 1) + patch.inject + text.slice(tagEnd + 1);
449
452
  } else {
450
- const preview = patch.injectAfter.length > 80 ? `${patch.injectAfter.slice(0, 80)}...` : patch.injectAfter;
451
- console.warn(`XML patch inject anchor not found in ${fname}: "${preview}"`);
453
+ console.warn(`XML patch inject failed in ${fname}: could not find tag end after "${patch.injectAfter}"`);
452
454
  }
455
+ } else {
456
+ const preview = patch.injectAfter.length > 80 ? `${patch.injectAfter.slice(0, 80)}...` : patch.injectAfter;
457
+ console.warn(`XML patch inject anchor not found in ${fname}: "${preview}"`);
453
458
  }
454
459
  }
455
460
  }
456
- if (fname === config.sceneFile && config.sceneObjects?.length) {
457
- const xml = config.sceneObjects.map((obj) => sceneObjectToXml(obj)).join("");
458
- text = text.replace("</worldbody>", xml + "</worldbody>");
459
- }
460
- mujoco.FS.writeFile(`/working/${fname}`, text);
461
- scanDependencies(text, fname, parser, downloaded, queue);
462
- } else {
463
- const buffer = new Uint8Array(await res.arrayBuffer());
464
- mujoco.FS.writeFile(`/working/${fname}`, buffer);
461
+ }
462
+ if (fname === config.sceneFile && config.sceneObjects?.length) {
463
+ const xml = config.sceneObjects.map((obj) => sceneObjectToXml(obj)).join("");
464
+ text = text.replace("</worldbody>", xml + "</worldbody>");
465
+ }
466
+ ensureDir(mujoco, fname);
467
+ mujoco.FS.writeFile(`/working/${fname}`, text);
468
+ scanDependencies(text, fname, parser, downloaded, xmlQueue);
469
+ }
470
+ if (assetFiles.length > 0) {
471
+ onProgress?.(`Downloading ${assetFiles.length} assets...`);
472
+ const results = await Promise.all(
473
+ assetFiles.map(async (fname) => {
474
+ const res = await fetch(baseUrl + fname);
475
+ if (!res.ok) {
476
+ console.warn(`Failed to fetch ${fname}: ${res.status} ${res.statusText}`);
477
+ return null;
478
+ }
479
+ return { fname, buffer: new Uint8Array(await res.arrayBuffer()) };
480
+ })
481
+ );
482
+ for (const result of results) {
483
+ if (!result) continue;
484
+ ensureDir(mujoco, result.fname);
485
+ mujoco.FS.writeFile(`/working/${result.fname}`, result.buffer);
465
486
  }
466
487
  }
467
488
  onProgress?.("Loading model...");
@@ -4155,6 +4176,6 @@ function useCameraAnimation() {
4155
4176
  * useCameraAnimation — composable camera animation hook.
4156
4177
  */
4157
4178
 
4158
- export { Body, ContactListener, ContactMarkers, Debug, DragInteraction, FlexRenderer, IkGizmo, MujocoCanvas, MujocoPhysics, MujocoProvider, MujocoSimProvider, SceneLights, TendonRenderer, TrajectoryPlayer, createController, findActuatorByName, findBodyByName, findGeomByName, findJointByName, findKeyframeByName, findSensorByName, findSiteByName, findTendonByName, getContact, getName, loadScene, useActuators, useAfterPhysicsStep, useBeforePhysicsStep, useBodyMeshes, useBodyState, useCameraAnimation, useContactEvents, useContacts, useCtrl, useCtrlNoise, useGamepad, useGravityCompensation, useIkController, useJointState, useKeyboardTeleop, useMujoco, useMujocoWasm, usePolicy, useSceneLights, useSelectionHighlight, useSensor, useSensors, useSitePosition, useTrajectoryPlayer, useTrajectoryRecorder, useVideoRecorder };
4179
+ export { Body, ContactListener, ContactMarkers, Debug, DragInteraction, FlexRenderer, IkGizmo, MujocoCanvas, MujocoPhysics, MujocoProvider, MujocoSimProvider, SceneLights, TendonRenderer, TrajectoryPlayer, createController, createControllerHook, findActuatorByName, findBodyByName, findGeomByName, findJointByName, findKeyframeByName, findSensorByName, findSiteByName, findTendonByName, getContact, getName, loadScene, useActuators, useAfterPhysicsStep, useBeforePhysicsStep, useBodyMeshes, useBodyState, useCameraAnimation, useContactEvents, useContacts, useCtrl, useCtrlNoise, useGamepad, useGravityCompensation, useIkController, useJointState, useKeyboardTeleop, useMujoco, useMujocoWasm, usePolicy, useSceneLights, useSelectionHighlight, useSensor, useSensors, useSitePosition, useTrajectoryPlayer, useTrajectoryRecorder, useVideoRecorder };
4159
4180
  //# sourceMappingURL=index.js.map
4160
4181
  //# sourceMappingURL=index.js.map