File size: 3,682 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from __future__ import annotations

import argparse
import os
import subprocess
import sys
import tempfile


BLEND_PATH = "/home/user/Desktop/answer.blend"
BLENDER_PATH = os.environ.get("BLENDER_PATH", "blender")
TOL = 1e-3


def _full_update():
    import bpy
    scn = bpy.context.scene
    bpy.context.view_layer.update()
    scn.frame_set(scn.frame_current)


def _find_y_driver(obj):
    ad = obj.animation_data
    if ad is None:
        return None
    for fc in ad.drivers:
        if fc.data_path == "location" and fc.array_index == 1:
            return fc
    return None


def _check_driver_paths(fc):
    invalid_paths = []
    try:
        for v in fc.driver.variables:
            for t in v.targets:
                dp = getattr(t, "data_path", "")
                if dp and t.id is not None:
                    try:
                        t.id.path_resolve(dp)
                    except Exception as ex:
                        invalid_paths.append(f"{v.name}:{dp} ({ex})")
    except Exception as ex:
        invalid_paths.append(f"(traversal error: {ex})")
    return invalid_paths


def _run_eval(blend_path: str) -> bool:
    import bpy

    if not os.path.isfile(blend_path):
        return False

    try:
        bpy.ops.wm.open_mainfile(filepath=blend_path)
    except Exception:
        return False

    source = bpy.data.objects.get("Source")
    anchor = bpy.data.objects.get("Anchor")
    driven = bpy.data.objects.get("Driven")
    if any(obj is None or obj.type != "MESH" for obj in (source, anchor, driven)):
        return False

    fc = _find_y_driver(driven)
    if fc is None:
        return False

    if _check_driver_paths(fc):
        return False

    if not bool(getattr(fc.driver, "is_valid", True)):
        return False

    scn = bpy.context.scene

    scn.frame_set(1)
    _full_update()
    if abs(driven.location.y - (source.location.x - anchor.location.z)) >= TOL:
        return False

    source.location.x = 5.0
    _full_update()
    if abs(driven.location.y - 4.0) >= TOL:
        return False

    anchor.location.z = 2.0
    _full_update()
    if abs(driven.location.y - 3.0) >= TOL:
        return False

    return True


def _write_result(result_file: str | None, passed: bool):
    print("True" if passed else "False")


def _run_in_blender(blend_path: str, result_file: str | None) -> bool:
    passed = _run_eval(blend_path)
    _write_result(result_file, passed)
    return passed


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


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


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() -> int:
    blend_path = _parse_blend_path()
    passed = _run_in_blender(blend_path, None) if _have_bpy() else _run_via_blender(blend_path)
    print("True" if passed else "False")
    return 0 if passed else 1


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