qx13's picture
Upload folder using huggingface_hub (part 5)
4c20479 verified
|
Raw History Blame Contribute Delete
4.98 kB
# USD models cleaner
Finds the USD models under `assets/usd_models` that no scene actually uses, and
deletes them. Pure standard library β€” no Isaac Sim, no `pxr`, no pip installs.
Two scripts, run in order:
| File | What it does |
| --- | --- |
| `usd_models_cleaner.py` | Seeds from `assets/scenes/*.json`, follows references *inside* the USD files transitively, writes the list to JSON. Read-only. |
| `usd_models_delete.py` | Takes that JSON and deletes the listed files, then removes folders left without any USD. Destructive. |
Both resolve paths against the repo root, so they work from any directory.
## 1. Build the list
```bash
python3 scripts/usd_cleaner/usd_models_cleaner.py --unused
```
Writes **`unused_usds.json`** next to the script β€” every USD file under
`assets/usd_models` that no scene references. That is the delete list.
Without `--unused` it writes **`used_usds.json`** instead: the models that *are*
referenced. Useful as a sanity check.
| Flag | Effect |
| --- | --- |
| `--unused` | Invert: list what is NOT reachable (the delete list) |
| `--aggressive` | Drop the sibling-directory guard (riskier, deletes more) |
| `--no_follow` | Scene JSONs only β€” **unsafe**, reproduces the old broken behaviour |
| `--out PATH` | Write somewhere else (a bare name lands in this folder) |
| `--no_out` | Print only, write no file |
| `--verbose` | Annotate each path with the scenes referencing it |
| `--scenes_dir` / `--models_dir` | Point at different directories |
## 2. Preview
```bash
python3 scripts/usd_cleaner/usd_models_delete.py unused_usds.json --dry-run
```
Prints every file and folder that would go, plus the total freed. Touches nothing.
**Always run this first.**
## 3. Delete
```bash
python3 scripts/usd_cleaner/usd_models_delete.py unused_usds.json
```
Prints a summary and asks `[y/N]` before removing anything.
| Flag | Effect |
| --- | --- |
| `--dry-run` | Preview only |
| `--yes` | Skip the confirmation prompt |
| `--keep_empty_dirs` | Delete the files but leave USD-less folders in place |
| `--models_dir` | Confine deletion to a different directory |
## Rules it follows
- **Folder removal.** A folder is removed only if it *held* a USD file and now
holds none. A folder that never held one β€” `textures/`, `material/` β€” belongs
to its parent model and is left alone, so surviving models keep their assets.
- **Protected folders.** `.git`, `.svn`, `.hg`, `__pycache__` and any dotted
folder are never swept. Without this the "no USD inside" rule would delete
`assets/usd_models/.git` wholesale.
- **Containment.** Deletion cannot leave `--models_dir`. A `../` in the list
aborts the run instead of escaping.
- **Only `.usd`, `.usda`, `.usdc`, `.usdz`** count as models. A `.png` or a
backup file whose *name* contains `.usd` is not a model and is not counted.
## How references are followed
A scene names a wrapper; the geometry and joints usually live elsewhere. The
cleaner therefore starts at the scene paths and walks outward:
1. **`.usda` text** β€” parsed for `@asset/path@` (and the `@@@path@@@` form),
resolved relative to the file holding the reference. Reliable.
2. **`.usdc` binary** β€” the token table is compressed, so printable strings are
mined and kept only when they resolve to a file that exists. Best-effort.
3. **Companion folder** β€” `X.usd` beside a folder `X/` pulls in that whole
folder. This is the convention these assets use and it is what catches what
the binary scan misses.
4. **Sibling directories** β€” a reachable *binary* USD also protects the
directories next to it, since its real references are unreadable.
`kitchen_background.usd` keeps `localized/` this way. `--aggressive` turns
this off.
Every step is repeated transitively until nothing new is reached.
## Why this matters
An earlier version scanned scene JSONs only. It deleted
`model_dishwasher_v2/resource/v_0/link_default_0.usda` β€” never named by any
scene, but the file holding the dishwasher's articulation. Isaac Sim then failed
with:
RuntimeError: Failed to find an articulation when resolving
'/World/envs/env_0/model_dishwasher'
`--no_follow` reproduces that behaviour for comparison. Do not delete with it.
## Before you delete
The binary side is a heuristic, not a parser. Only `pxr` (via
`/isaac-sim/python.sh`) can resolve `.usdc` references with certainty. Treat the
list as a strong suggestion, not proof.
Scene JSONs may also not be the only referrer β€” task configs or code can name a
model directly.
`assets/usd_models` is a git repo, which is the real safety net: after deleting,
`git status` shows every removal and `git reset --hard HEAD` restores them.
Load a scene before committing the deletions.
## Current numbers
158 paths declared by scenes expand to **264 reachable USD files**; 652 are
unreachable. Scene-JSON-only scanning would have kept just 158 and deleted 729 β€”
the 106-file difference is what broke the dishwasher.