Download OVOW/benchmark/scripts/extract_blend_meshes.py from AIGeeksGroup/PhysicalWM: direct link, hf CLI and curl.
- Browser
- Download file 4.53 kB
-
https://huggingface.co/datasets/AIGeeksGroup/PhysicalWM/resolve/main/OVOW/benchmark/scripts/extract_blend_meshes.py
- Command line
-
hf download hf://datasets/AIGeeksGroup/PhysicalWM/OVOW/benchmark/scripts/extract_blend_meshes.py
-
curl -L -o extract_blend_meshes.py https://huggingface.co/datasets/AIGeeksGroup/PhysicalWM/resolve/main/OVOW/benchmark/scripts/extract_blend_meshes.py
4.53 kB
| from __future__ import annotations | |
| import argparse | |
| import pickle | |
| import sys | |
| from pathlib import Path | |
| import bpy | |
| import numpy as np | |
| def parse_args() -> argparse.Namespace: | |
| argv_source = getattr(bpy.app, "argv", None) | |
| if argv_source is None: | |
| argv_source = sys.argv | |
| argv: list[str] = [] | |
| if "--" in argv_source: | |
| argv = argv_source[argv_source.index("--") + 1 :] | |
| parser = argparse.ArgumentParser(description="Extract triangle meshes from a .blend/.glb/.gltf scene") | |
| parser.add_argument( | |
| "--input", | |
| default=None, | |
| help="Input scene path. If omitted, uses the file already opened by Blender.", | |
| ) | |
| parser.add_argument("--output", required=True, help="Output pickle path") | |
| parser.add_argument( | |
| "--frame_mode", | |
| default="first_mid_last", | |
| help="Frame sampling mode: first_only, first_mid_last, last_only, or exact:<frame>", | |
| ) | |
| return parser.parse_args(argv) | |
| def load_input_scene(input_path: str | None) -> None: | |
| if not input_path: | |
| return | |
| path = Path(input_path).resolve() | |
| if not path.exists(): | |
| raise FileNotFoundError(f"Input scene does not exist: {path}") | |
| ext = path.suffix.lower() | |
| if ext == ".blend": | |
| bpy.ops.wm.open_mainfile(filepath=str(path)) | |
| return | |
| if ext in {".glb", ".gltf"}: | |
| bpy.ops.object.select_all(action="SELECT") | |
| bpy.ops.object.delete() | |
| bpy.ops.import_scene.gltf(filepath=str(path)) | |
| return | |
| raise ValueError(f"Unsupported input scene extension: {path.suffix}") | |
| def _parse_exact_frame(mode: str) -> int | None: | |
| if not mode.startswith("exact:"): | |
| return None | |
| raw = mode.split(":", 1)[1].strip() | |
| if not raw: | |
| raise ValueError("exact frame mode requires a frame number, e.g. exact:55") | |
| return int(raw) | |
| def frame_indices(scene: bpy.types.Scene, mode: str) -> list[int]: | |
| start = int(scene.frame_start) | |
| end = int(scene.frame_end) | |
| if end < start: | |
| end = start | |
| exact = _parse_exact_frame(mode) | |
| if exact is not None: | |
| return [exact] | |
| if mode == "last_only": | |
| return [end] | |
| if mode == "first_only": | |
| return [start] | |
| if mode != "first_mid_last": | |
| raise ValueError(f"Unsupported frame_mode: {mode}") | |
| if start == end: | |
| return [start] | |
| mid = (start + end) // 2 | |
| out: list[int] = [] | |
| for f in (start, mid, end): | |
| if f not in out: | |
| out.append(f) | |
| return out | |
| def world_vertices(obj_eval: bpy.types.Object, mesh: bpy.types.Mesh) -> np.ndarray: | |
| local = np.empty(len(mesh.vertices) * 3, dtype=np.float32) | |
| mesh.vertices.foreach_get("co", local) | |
| local = local.reshape(-1, 3) | |
| mat = np.array(obj_eval.matrix_world, dtype=np.float32) | |
| rot = mat[:3, :3] | |
| trans = mat[:3, 3] | |
| return (local @ rot.T + trans).astype(np.float32, copy=False) | |
| def extract_frame(scene: bpy.types.Scene, frame: int) -> dict[str, dict[str, np.ndarray]]: | |
| scene.frame_set(frame) | |
| depsgraph = bpy.context.evaluated_depsgraph_get() | |
| out: dict[str, dict[str, np.ndarray]] = {} | |
| for obj in scene.objects: | |
| if obj.type != "MESH" or obj.hide_render: | |
| continue | |
| obj_eval = obj.evaluated_get(depsgraph) | |
| mesh = obj_eval.to_mesh(preserve_all_data_layers=False, depsgraph=depsgraph) | |
| if mesh is None: | |
| continue | |
| try: | |
| mesh.calc_loop_triangles() | |
| if len(mesh.vertices) == 0 or len(mesh.loop_triangles) == 0: | |
| continue | |
| verts = world_vertices(obj_eval, mesh) | |
| faces = np.array([tri.vertices for tri in mesh.loop_triangles], dtype=np.int32) | |
| out[obj.name] = { | |
| "vertices": verts, | |
| "faces": faces, | |
| } | |
| finally: | |
| obj_eval.to_mesh_clear() | |
| return out | |
| def main() -> None: | |
| args = parse_args() | |
| load_input_scene(args.input) | |
| output_path = Path(args.output) | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| scene = bpy.context.scene | |
| frames = frame_indices(scene, args.frame_mode) | |
| objects_per_frame: list[dict[str, dict[str, np.ndarray]]] = [] | |
| for frame in frames: | |
| objects_per_frame.append(extract_frame(scene, frame)) | |
| payload = { | |
| "frame_indices": frames, | |
| "objects_per_frame": objects_per_frame, | |
| } | |
| with output_path.open("wb") as f: | |
| pickle.dump(payload, f, protocol=pickle.HIGHEST_PROTOCOL) | |
| if __name__ == "__main__": | |
| main() | |