File size: 2,769 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
from __future__ import annotations

import argparse
import os
import subprocess
import sys


EXPECTED_BONE_NAMES = {"Bone.Root", "Bone.A", "Bone.B", "Bone.Tip"}
BLEND_PATH = "/home/user/Desktop/answer.blend"
BLENDER_PATH = os.environ.get("BLENDER_PATH", "blender")


def _run_eval(blend_path):
    import bpy

    if not os.path.isfile(blend_path):
        return False
    try:
        bpy.ops.wm.open_mainfile(filepath=blend_path)
    except Exception:
        return False

    rig = bpy.data.objects.get("Rig")
    if rig is None or rig.type != "ARMATURE":
        return False
    bones = list(rig.data.bones)
    if len(bones) != 4:
        return False
    bone_names = {b.name for b in bones}
    if bone_names != EXPECTED_BONE_NAMES:
        return False
    by_name = {b.name: b for b in bones}
    if not (by_name["Bone.Root"].parent is None and by_name["Bone.A"].parent and by_name["Bone.A"].parent.name == "Bone.Root" and by_name["Bone.B"].parent and by_name["Bone.B"].parent.name == "Bone.A" and by_name["Bone.Tip"].parent and by_name["Bone.Tip"].parent.name == "Bone.B"):
        return False

    skin = bpy.data.objects.get("Skin")
    if skin is None or skin.type != "MESH":
        return False
    arm_mods = [m for m in skin.modifiers if m.type == "ARMATURE"]
    if len(arm_mods) != 1 or arm_mods[0].object is None or arm_mods[0].object.name != "Rig":
        return False
    vg_names = {vg.name for vg in skin.vertex_groups}
    if vg_names != EXPECTED_BONE_NAMES:
        return False
    return all(name in bone_names for name in vg_names)


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


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


def eval_outputs(blend_path, expected=None, postconfig=None):
    return _run_eval(blend_path) if _have_bpy() else _run_via_blender(blend_path)


def _parse_blend_path() -> str:
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("--blend", default=BLEND_PATH)
    script_args = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []
    return parser.parse_known_args(script_args)[0].blend


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


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