Spaces:
Running
Running
Download tools/render_thumbs.py from projectsim/simready-library: direct link, hf CLI and curl.
- Browser
- Download file 2.45 kB
-
https://huggingface.co/spaces/projectsim/simready-library/resolve/main/tools/render_thumbs.py
- Command line
-
hf download hf://spaces/projectsim/simready-library/tools/render_thumbs.py
-
curl -L -o render_thumbs.py https://huggingface.co/spaces/projectsim/simready-library/resolve/main/tools/render_thumbs.py
2.45 kB
| """Render previews/thumbs/<asset>.webp (640 px studio thumbnails) from previews/<asset>.glb. | |
| Only renders thumbnails that are missing or older than their GLB, in a few parallel | |
| Blender batches (tools/blender_thumb.py). Run after tools/make_previews.py: | |
| python3 tools/render_thumbs.py [--blender PATH] [--jobs 3] [asset ...] | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import subprocess | |
| import sys | |
| from concurrent.futures import ThreadPoolExecutor | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("assets", nargs="*") | |
| ap.add_argument("--previews", default="previews") | |
| ap.add_argument("--size", type=int, default=640) | |
| ap.add_argument("--blender", default="/Applications/Blender.app/Contents/MacOS/Blender") | |
| ap.add_argument("--jobs", type=int, default=3) | |
| args = ap.parse_args() | |
| out = os.path.join(args.previews, "thumbs") | |
| os.makedirs(out, exist_ok=True) | |
| hidden_path = os.path.join(HERE, "hidden.json") | |
| hidden = set(json.load(open(hidden_path))["assets"]) if os.path.exists(hidden_path) else set() | |
| names = args.assets or sorted(f[:-4] for f in os.listdir(args.previews) if f.endswith(".glb") and f[:-4] not in hidden) | |
| def stale(n): | |
| thumb, glb = os.path.join(out, f"{n}.webp"), os.path.join(args.previews, f"{n}.glb") | |
| return args.assets or not os.path.exists(thumb) or os.path.getmtime(thumb) < os.path.getmtime(glb) | |
| todo = [n for n in names if stale(n)] | |
| print(f"{len(todo)} thumbnails to render", file=sys.stderr) | |
| if not todo: | |
| return | |
| batches = [todo[i::args.jobs] for i in range(args.jobs)] | |
| def run(batch): | |
| if not batch: | |
| return 0, [] | |
| cmd = [args.blender, "-b", "--factory-startup", "--python", os.path.join(HERE, "blender_thumb.py"), | |
| "--", out, str(args.size)] + [os.path.join(args.previews, f"{n}.glb") for n in batch] | |
| res = subprocess.run(cmd, capture_output=True, text=True) | |
| done = [l.split()[1] for l in res.stdout.splitlines() if l.startswith("THUMB_OK")] | |
| return len(done), [n for n in batch if n not in done] | |
| with ThreadPoolExecutor(args.jobs) as pool: | |
| results = list(pool.map(run, batches)) | |
| made = sum(r[0] for r in results) | |
| failed = [n for r in results for n in r[1]] | |
| print(f"rendered {made}, failed {len(failed)}: {failed[:20]}", file=sys.stderr) | |
| if __name__ == "__main__": | |
| main() | |