mishig HF Staff commited on
Commit
503f29a
·
verified ·
1 Parent(s): 403d84a

Sync from huggingface/lerobot-dataset-visualizer via hub-sync

Browse files
Files changed (1) hide show
  1. src/components/urdf-viewer.tsx +38 -10
src/components/urdf-viewer.tsx CHANGED
@@ -68,6 +68,28 @@ const URDF_BASE_URL =
68
 
69
  const prefetchedUrdfs = new Set<string>();
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  /**
72
  * Starts downloading a robot's visual STL meshes into the geometry cache. The viewer only mounts once
73
  * the episode data has loaded, and the meshes (15.7 MB for SO-101) used to start downloading only
@@ -78,8 +100,7 @@ export function prefetchRobotModel(robotType: string | null) {
78
  if (prefetchedUrdfs.has(urdfUrl)) return;
79
  prefetchedUrdfs.add(urdfUrl);
80
  const base = THREE.LoaderUtils.extractUrlBase(urdfUrl);
81
- fetch(urdfUrl)
82
- .then((res) => res.text())
83
  .then((text) => {
84
  const doc = new DOMParser().parseFromString(text, "application/xml");
85
  for (const mesh of doc.querySelectorAll("visual mesh")) {
@@ -547,9 +568,13 @@ function RobotScene({
547
  }, 0);
548
  };
549
 
550
- loader.load(
551
- urdfUrl,
552
- (robot) => {
 
 
 
 
553
  robotRef.current = robot;
554
  robot.rotateOnAxis(new THREE.Vector3(1, 0, 0), -Math.PI / 2);
555
  robot.scale.set(scale, scale, scale);
@@ -592,14 +617,17 @@ function RobotScene({
592
  )
593
  .map((j) => j.name);
594
  onJointsLoaded(movable);
595
- },
596
- undefined,
597
- (err) => {
598
  console.error("Error loading URDF:", err);
599
  setError(String(err));
600
- },
601
- );
 
 
602
  return () => {
 
603
  if (robotRef.current) {
604
  scene.remove(robotRef.current);
605
  robotRef.current = null;
 
68
 
69
  const prefetchedUrdfs = new Set<string>();
70
 
71
+ // The bucket resolve is a 302 with no Cache-Control, to a URL re-signed every second, so the
72
+ // browser downloads the URDF again on every mount unless the text is kept here.
73
+ const urdfTextCache = new Map<string, Promise<string>>();
74
+
75
+ function loadUrdfText(url: string): Promise<string> {
76
+ let loading = urdfTextCache.get(url);
77
+ if (!loading) {
78
+ loading = fetch(url)
79
+ .then((res) => {
80
+ if (!res.ok)
81
+ throw new Error(`Failed to load URDF ${url} (HTTP ${res.status})`);
82
+ return res.text();
83
+ })
84
+ .catch((err) => {
85
+ urdfTextCache.delete(url);
86
+ throw err;
87
+ });
88
+ urdfTextCache.set(url, loading);
89
+ }
90
+ return loading;
91
+ }
92
+
93
  /**
94
  * Starts downloading a robot's visual STL meshes into the geometry cache. The viewer only mounts once
95
  * the episode data has loaded, and the meshes (15.7 MB for SO-101) used to start downloading only
 
100
  if (prefetchedUrdfs.has(urdfUrl)) return;
101
  prefetchedUrdfs.add(urdfUrl);
102
  const base = THREE.LoaderUtils.extractUrlBase(urdfUrl);
103
+ loadUrdfText(urdfUrl)
 
104
  .then((text) => {
105
  const doc = new DOMParser().parseFromString(text, "application/xml");
106
  for (const mesh of doc.querySelectorAll("visual mesh")) {
 
568
  }, 0);
569
  };
570
 
571
+ loader.workingPath = THREE.LoaderUtils.extractUrlBase(urdfUrl);
572
+ let cancelled = false;
573
+ manager.itemStart(urdfUrl);
574
+ loadUrdfText(urdfUrl)
575
+ .then((text) => {
576
+ if (cancelled) return;
577
+ const robot = loader.parse(text);
578
  robotRef.current = robot;
579
  robot.rotateOnAxis(new THREE.Vector3(1, 0, 0), -Math.PI / 2);
580
  robot.scale.set(scale, scale, scale);
 
617
  )
618
  .map((j) => j.name);
619
  onJointsLoaded(movable);
620
+ })
621
+ .catch((err) => {
622
+ if (cancelled) return;
623
  console.error("Error loading URDF:", err);
624
  setError(String(err));
625
+ })
626
+ .finally(() => {
627
+ if (!cancelled) manager.itemEnd(urdfUrl);
628
+ });
629
  return () => {
630
+ cancelled = true;
631
  if (robotRef.current) {
632
  scene.remove(robotRef.current);
633
  robotRef.current = null;