Spaces:
Running on Zero
Running on Zero
update
Browse files
app.py
CHANGED
|
@@ -599,19 +599,29 @@ def _find_urdf_stem(asset_dir: str) -> str | None:
|
|
| 599 |
return None
|
| 600 |
|
| 601 |
|
| 602 |
-
def _zip_items(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 603 |
"""Write files/dirs into a zip.
|
| 604 |
|
| 605 |
Args:
|
| 606 |
zip_path: Output archive path.
|
| 607 |
items: ``(src_abspath, arcname)`` pairs. Directories are walked and
|
| 608 |
stored under ``arcname``.
|
|
|
|
|
|
|
| 609 |
"""
|
| 610 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 611 |
for src, arcname in items:
|
| 612 |
if os.path.isdir(src):
|
| 613 |
for root_d, _, files in os.walk(src):
|
| 614 |
for fn in files:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
fp = os.path.join(root_d, fn)
|
| 616 |
rel = os.path.relpath(fp, src)
|
| 617 |
zf.write(fp, os.path.join(arcname, rel))
|
|
@@ -622,28 +632,44 @@ def _zip_items(zip_path: str, items: list[tuple[str, str]]) -> str:
|
|
| 622 |
|
| 623 |
def _fmt_zip_path(
|
| 624 |
asset_dir: str, fmt: str, req: gr.Request
|
| 625 |
-
) -> tuple[str, str]:
|
| 626 |
-
"""Build the per-format output zip path named ``<uid>.zip``.
|
| 627 |
|
| 628 |
-
Returns ``(zip_path, uid)``.
|
| 629 |
-
downloaded filename stays ``<uid>.zip`` without colliding across formats.
|
| 630 |
"""
|
| 631 |
uid = os.path.basename(os.path.normpath(asset_dir))
|
| 632 |
-
|
| 633 |
-
os.makedirs(
|
| 634 |
-
|
|
|
|
| 635 |
|
| 636 |
|
| 637 |
def download_urdf(asset_dir: str, req: gr.Request) -> str | None:
|
| 638 |
-
"""Package the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
if not asset_dir or not os.path.isdir(asset_dir):
|
| 640 |
gr.Warning("Please select an asset first.")
|
| 641 |
return None
|
| 642 |
gr.Info("β³ Preparing URDF asset for download, please wait...")
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 647 |
return zip_path
|
| 648 |
|
| 649 |
|
|
@@ -673,9 +699,9 @@ def download_usd(asset_dir: str, req: gr.Request) -> str | None:
|
|
| 673 |
gr.Warning("No USD file available for this asset.")
|
| 674 |
return None
|
| 675 |
|
| 676 |
-
zip_path, uid = _fmt_zip_path(asset_dir, "usd", req)
|
| 677 |
_zip_items(zip_path, items)
|
| 678 |
-
gr.Info(f"β
{
|
| 679 |
return zip_path
|
| 680 |
|
| 681 |
|
|
@@ -692,9 +718,9 @@ def download_mjcf(asset_dir: str, req: gr.Request) -> str | None:
|
|
| 692 |
gr.Warning("No MJCF folder available for this asset.")
|
| 693 |
return None
|
| 694 |
|
| 695 |
-
zip_path, uid = _fmt_zip_path(asset_dir, "mjcf", req)
|
| 696 |
_zip_items(zip_path, [(mjcf_dir, "mjcf")])
|
| 697 |
-
gr.Info(f"β
{
|
| 698 |
return zip_path
|
| 699 |
|
| 700 |
|
|
|
|
| 599 |
return None
|
| 600 |
|
| 601 |
|
| 602 |
+
def _zip_items(
|
| 603 |
+
zip_path: str,
|
| 604 |
+
items: list[tuple[str, str]],
|
| 605 |
+
exclude_suffixes: tuple[str, ...] = (),
|
| 606 |
+
) -> str:
|
| 607 |
"""Write files/dirs into a zip.
|
| 608 |
|
| 609 |
Args:
|
| 610 |
zip_path: Output archive path.
|
| 611 |
items: ``(src_abspath, arcname)`` pairs. Directories are walked and
|
| 612 |
stored under ``arcname``.
|
| 613 |
+
exclude_suffixes: Lowercase filename suffixes to skip when walking
|
| 614 |
+
directories, e.g. ``(".glb",)``.
|
| 615 |
"""
|
| 616 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 617 |
for src, arcname in items:
|
| 618 |
if os.path.isdir(src):
|
| 619 |
for root_d, _, files in os.walk(src):
|
| 620 |
for fn in files:
|
| 621 |
+
if exclude_suffixes and fn.lower().endswith(
|
| 622 |
+
exclude_suffixes
|
| 623 |
+
):
|
| 624 |
+
continue
|
| 625 |
fp = os.path.join(root_d, fn)
|
| 626 |
rel = os.path.relpath(fp, src)
|
| 627 |
zf.write(fp, os.path.join(arcname, rel))
|
|
|
|
| 632 |
|
| 633 |
def _fmt_zip_path(
|
| 634 |
asset_dir: str, fmt: str, req: gr.Request
|
| 635 |
+
) -> tuple[str, str, str]:
|
| 636 |
+
"""Build the per-format output zip path named ``<uid>_<fmt>.zip``.
|
| 637 |
|
| 638 |
+
Returns ``(zip_path, uid, zip_name)``.
|
|
|
|
| 639 |
"""
|
| 640 |
uid = os.path.basename(os.path.normpath(asset_dir))
|
| 641 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 642 |
+
os.makedirs(user_dir, exist_ok=True)
|
| 643 |
+
zip_name = f"{uid}_{fmt}.zip"
|
| 644 |
+
return os.path.join(user_dir, zip_name), uid, zip_name
|
| 645 |
|
| 646 |
|
| 647 |
def download_urdf(asset_dir: str, req: gr.Request) -> str | None:
|
| 648 |
+
"""Package the ``.urdf`` file(s) and the ``mesh/`` folder (minus ``.glb``).
|
| 649 |
+
|
| 650 |
+
The visual/collision meshes referenced by the URDF live in ``mesh/`` as
|
| 651 |
+
``.obj`` + material files; the ``.glb`` (a separate viewer asset) and all
|
| 652 |
+
other formats (USD/MJCF/video/...) are intentionally excluded.
|
| 653 |
+
"""
|
| 654 |
if not asset_dir or not os.path.isdir(asset_dir):
|
| 655 |
gr.Warning("Please select an asset first.")
|
| 656 |
return None
|
| 657 |
gr.Info("β³ Preparing URDF asset for download, please wait...")
|
| 658 |
+
rel = _rel_dir(asset_dir)
|
| 659 |
+
ensure_hf_files([f"{rel}/*.urdf", f"{rel}/mesh/**"])
|
| 660 |
+
|
| 661 |
+
items: list[tuple[str, str]] = [
|
| 662 |
+
(os.path.join(asset_dir, f), f)
|
| 663 |
+
for f in os.listdir(asset_dir)
|
| 664 |
+
if f.lower().endswith(".urdf")
|
| 665 |
+
]
|
| 666 |
+
mesh_dir = os.path.join(asset_dir, "mesh")
|
| 667 |
+
if os.path.isdir(mesh_dir):
|
| 668 |
+
items.append((mesh_dir, "mesh"))
|
| 669 |
+
|
| 670 |
+
zip_path, uid, zip_name = _fmt_zip_path(asset_dir, "urdf", req)
|
| 671 |
+
_zip_items(zip_path, items, exclude_suffixes=(".glb",))
|
| 672 |
+
gr.Info(f"β
{zip_name} is ready.")
|
| 673 |
return zip_path
|
| 674 |
|
| 675 |
|
|
|
|
| 699 |
gr.Warning("No USD file available for this asset.")
|
| 700 |
return None
|
| 701 |
|
| 702 |
+
zip_path, uid, zip_name = _fmt_zip_path(asset_dir, "usd", req)
|
| 703 |
_zip_items(zip_path, items)
|
| 704 |
+
gr.Info(f"β
{zip_name} is ready.")
|
| 705 |
return zip_path
|
| 706 |
|
| 707 |
|
|
|
|
| 718 |
gr.Warning("No MJCF folder available for this asset.")
|
| 719 |
return None
|
| 720 |
|
| 721 |
+
zip_path, uid, zip_name = _fmt_zip_path(asset_dir, "mjcf", req)
|
| 722 |
_zip_items(zip_path, [(mjcf_dir, "mjcf")])
|
| 723 |
+
gr.Info(f"β
{zip_name} is ready.")
|
| 724 |
return zip_path
|
| 725 |
|
| 726 |
|