File size: 3,353 Bytes
5154c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from __future__ import annotations

import os
import subprocess
import sys
from pathlib import Path


BLEND_PATH = "/home/user/Desktop/output/repaired.blend"
BLENDER_PATH = os.environ.get("BLENDER_PATH", "blender")


def _inspect(blend_path, obj_name):
    import bmesh
    import bpy
    bpy.ops.wm.open_mainfile(filepath=blend_path)
    mesh_objs = [o for o in bpy.data.objects if o.type == "MESH"]
    if obj_name not in bpy.data.objects:
        return {"object_exists": False, "mesh_count": len(mesh_objs)}
    obj = bpy.data.objects[obj_name]
    if obj.type != "MESH":
        return {"object_exists": False, "mesh_count": len(mesh_objs)}
    me = obj.data
    bm = bmesh.new()
    bm.from_mesh(me)
    bm.edges.ensure_lookup_table()
    bm.verts.ensure_lookup_table()
    non_man = sum(1 for e in bm.edges if not e.is_manifold)
    loose_verts = sum(1 for v in bm.verts if not v.link_edges)
    loose_edges = sum(1 for e in bm.edges if not e.link_faces)
    xs = [v.co.x for v in bm.verts]
    ys = [v.co.y for v in bm.verts]
    zs = [v.co.z for v in bm.verts]
    bbox_dims = (max(xs) - min(xs), max(ys) - min(ys), max(zs) - min(zs))
    signed_vol = bm.calc_volume(signed=True)
    unsigned_vol = bm.calc_volume(signed=False)
    bm.free()
    return {
        "object_exists": True,
        "mesh_count": len(mesh_objs),
        "location": tuple(obj.location),
        "verts": len(me.vertices),
        "edges": len(me.edges),
        "faces": len(me.polygons),
        "non_man": non_man,
        "loose_verts": loose_verts,
        "loose_edges": loose_edges,
        "bbox_dims": bbox_dims,
        "signed_vol": signed_vol,
        "unsigned_vol": unsigned_vol,
    }


def eval_outputs(blend_path, expected=None, postconfig=None):
    import bpy
    init_path = os.path.join(os.path.dirname(__file__), "init_file", "scene.blend")
    if not os.path.isfile(blend_path):
        return False
    init = _inspect(init_path, "Part")
    sub = _inspect(blend_path, "Part")
    if not sub.get("object_exists"):
        return False
    if sub["mesh_count"] != 1:
        return False
    if max(abs(a - b) for a, b in zip(sub["location"], init["location"])) >= 1e-5:
        return False
    if sub["non_man"] != 0 or sub["loose_verts"] != 0 or sub["loose_edges"] != 0:
        return False
    if sub["signed_vol"] <= 0:
        return False
    for i, s in zip(init["bbox_dims"], sub["bbox_dims"]):
        if abs(s - i) / max(abs(i), 1e-9) >= 0.01:
            return False
    init_aabb_vol = init["bbox_dims"][0] * init["bbox_dims"][1] * init["bbox_dims"][2]
    if abs(sub["unsigned_vol"] - init_aabb_vol) / max(init_aabb_vol, 1e-9) >= 0.02:
        return False
    return True


def _have_bpy() -> bool:
    try:
        import bpy  # noqa: F401
        return True
    except Exception:
        return False


def _run_via_blender() -> bool:
    proc = subprocess.run(
        [BLENDER_PATH, "--background", "--factory-startup", "--python", __file__],
        capture_output=True,
        text=True,
        encoding="utf-8",
        errors="replace",
        timeout=600,
    )
    return proc.returncode == 0


def main():
    passed = eval_outputs(BLEND_PATH) if _have_bpy() else _run_via_blender()
    print("True" if passed else "False")
    return 0 if passed else 1


if __name__ == "__main__":
    raise SystemExit(main())