File size: 5,542 Bytes
84669f3
 
a5c598b
84669f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5c598b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84669f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Planning tests that do not require Blender."""
from pathlib import Path
import ast
import sys
import tempfile
import unittest


ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
import process
from texture_library import choose_set, texture_sets


class ProcessorTests(unittest.TestCase):
    def setUp(self):
        self.temporary = tempfile.TemporaryDirectory()
        self.addCleanup(self.temporary.cleanup)
        self.root = Path(self.temporary.name).resolve()
        self.input = self.root / "data_scene"
        self.library = self.root / "textures"
        self.output = self.root / "processed"
        for scene in ("00100", "00101"):
            source = self.input / "batch" / scene / "flat.blend"
            source.parent.mkdir(parents=True)
            source.write_bytes(b"test source")
        self.add_set("set_01")

    def add_set(self, name):
        folder = self.library / name
        folder.mkdir(parents=True)
        for filename in ("white_wall.png", "wood_floor.jpeg"):
            (folder / filename).write_bytes(b"texture")

    def plan(self, *extra, input_path=None):
        args = process.arguments([
            "--input", str(input_path or self.input),
            "--output", str(self.output),
            "--textures", str(self.library),
            *extra,
        ])
        return process.make_plan(args)[0]

    def test_new_named_set_is_available_without_changing_code(self):
        self.add_set("my_new_palette")
        self.assertEqual(texture_sets(self.library), ("my_new_palette", "set_01"))
        selections = {
            choose_set(self.library, seed=seed, scene_key="scene")
            for seed in range(50)
        }
        self.assertEqual(selections, {"my_new_palette", "set_01"})
        self.assertTrue(all(row[2] == "my_new_palette" for row in self.plan(
            "--texture-set", "my_new_palette")))

    def test_incomplete_pair_is_an_error(self):
        (self.library / "incomplete").mkdir()
        with self.assertRaisesRegex(ValueError, "Incomplete texture set"):
            self.plan()

    def test_seed_and_relative_batch_layout(self):
        self.add_set("new_pair")
        first = self.plan("--seed", "42")
        self.assertEqual(first, self.plan("--seed", "42"))
        self.assertEqual(
            [row[1].relative_to(self.output).as_posix() for row in first],
            ["batch/00100/flat.blend", "batch/00101/flat.blend"],
        )
        self.assertEqual(len(self.plan("--limit", "1")), 1)
        self.assertFalse(self.output.exists())

    def test_single_scene_file_and_directory(self):
        scene = self.input / "batch" / "00100"
        for input_path in (scene, scene / "flat.blend"):
            with self.subTest(input_path=input_path):
                plan = self.plan(input_path=input_path)
                self.assertEqual(len(plan), 1)
                self.assertEqual(plan[0][1], self.output / "flat.blend")
        self.assertEqual(process.default_output(scene), scene.with_name("00100_processed"))
        self.assertEqual(process.default_output(scene / "flat.blend"),
                         scene.with_name("00100_processed"))

    def test_default_library_is_relative_to_script(self):
        args = process.arguments(["--input", str(self.input)])
        self.assertEqual(args.textures, ROOT / "textures")

    def test_processing_sources_use_descriptive_lighting_names(self):
        self.assertIn("configure lighting", process.__doc__)

        def strings(filename):
            tree = ast.parse((ROOT / "_blender" / filename).read_text())
            return [node.value for node in ast.walk(tree)
                    if isinstance(node, ast.Constant) and isinstance(node.value, str)]

        lighting = strings("apply_lighting.py")
        textured = strings("process_scene_textured.py")
        self.assertEqual(lighting.count("TopPreviewCamera"), 2)
        self.assertEqual(textured.count("TopPreviewCamera"), 1)
        self.assertTrue(any(value.startswith("LIGHTING_VERIFIED ") for value in lighting))
        self.assertTrue(any(value.startswith("LIGHTING_DONE ") for value in lighting))
        self.assertIn("random textures then environment and sun lighting", textured)

    def test_input_and_output_overlap_is_rejected(self):
        for output in (self.input, self.input / "output", self.root):
            with self.subTest(output=output):
                self.output = output
                with self.assertRaisesRegex(ValueError, "trees must be separate"):
                    self.plan()

    def test_complete_outputs_require_skip_or_overwrite(self):
        target = self.output / "batch" / "00100"
        target.mkdir(parents=True)
        for name in process.OUTPUT_NAMES:
            (target / name).write_bytes(b"existing output")
        with self.assertRaisesRegex(ValueError, "Output exists"):
            self.plan()
        self.assertEqual([row[3] for row in self.plan("--skip-existing")],
                         ["skip", "process"])
        self.assertTrue(all(row[3] == "process" for row in self.plan("--overwrite")))
        (target / "preview_top.png").unlink()
        with self.assertRaisesRegex(ValueError, "Incomplete existing output"):
            self.plan("--skip-existing")
        (target / "keep.txt").write_text("do not delete")
        with self.assertRaisesRegex(ValueError, "Unexpected content"):
            self.plan("--overwrite")
        self.assertTrue((target / "keep.txt").exists())


if __name__ == "__main__":
    unittest.main()