File: overlay_common.py

package info (click to toggle)
blender 5.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 329,128 kB
  • sloc: cpp: 2,489,823; python: 349,859; ansic: 261,364; xml: 2,103; sh: 999; javascript: 317; makefile: 193
file content (246 lines) | stat: -rw-r--r-- 9,576 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# SPDX-FileCopyrightText: 2025 Blender Authors
#
# SPDX-License-Identifier: Apache-2.0

import bpy
import os
from pathlib import Path
import argparse

"""Common functionality for Overlay render tests.

The intended usage is setting up a Permutations instance containing all the variants/permutations for a given blend file,
then passing it to the `run_test(permutations)` function.

The `run_test` function also checks for a `--test` argument in `sys.argv`.
When set, it will reproduce the state of a given test number.
So:
`blender "(...)/tests/files/overlay/<test>.blend" -P "(...)/tests/python/overlay/<test>.py" -- --test <test-number>`
will open the blend file and set its state to the <test-number> permutation, instead of running the tests.

Common permutations can also be declared inside this file.
See `ob_modes_permutations` for an example on how to setup permutations.
"""


class Permutations:
    """Container class for the permutations of a given test file.

    Can be combined with other Permutations.
    """

    def __init__(self, reset_key=None, variants_dict={}):
        """Setup the initial set of permutations.

        :reset_key: str - The variants_dict key that resets the test to its default state.
        :variants_dict: {str: lambda}
            - Where the key is the name of the state variation (for example "xray-on" or "xray-off")
              and the lambda sets the blend to that state.
        """
        reset = []
        if reset_key:
            reset = [variants_dict[reset_key]]
        self.dict = {k: Permutation([v], reset) for k, v in variants_dict.items()}

    def add(self, key_filter_cb=None, permutations_array=[]):
        """Combine two sets of permutations.

        Replaces the current permutations with every possible combination of current and incoming permutations,
        unless key_filter_cb is set and returns False for a given key, in which case they're kept as-is.

        :key_filter_cb: lambda (key: str): :bool:
            - A callback for deciding which keys the permutations should be applied to.
        :permutations: [Permutations]
            - An array of Permutations instances. Each Permutations instance is applied sequentially,
              so a.add(None, [b]); a.add(None, [c]) is equivalent to a.add(None, [b, c])
        """
        for permutations in permutations_array:
            dict_copy = self.dict.copy()
            self.dict = {}
            for key, permutation in dict_copy.items():
                if key_filter_cb and not key_filter_cb(key):
                    self.dict[key] = permutation
                    continue
                for key_in, permutation_in in permutations.dict.items():
                    self.dict[f"{key}_{key_in}"] = permutation.combine(permutation_in)

    def loop(self):
        """Loop through each permutation, applying its state before yielding its key."""
        for key, permutation in self.dict.items():
            permutation.apply()
            yield key
            permutation.reset()


class Permutation:
    """A single test permutation.

    A permutation containing all the callbacks needed for setting up its state (_apply),
    and for resetting the test back to its default state (_reset).

    This class is meant to be used internally by the Permutations class.
    """

    def __init__(self, apply, reset):
        self._apply = apply
        self._reset = reset

    def combine(self, other):
        return Permutation(self._apply + other._apply, self._reset + other._reset)

    def apply(self):
        for cb in self._apply:
            cb()

    def reset(self):
        for cb in self._reset:
            cb()


def set_permutation_from_args(permutations):
    """If the command line requested a specific permutation, set the blend state to it."""
    import sys
    if "--" not in sys.argv:
        return False
    parser = argparse.ArgumentParser()
    parser.add_argument("--test", type=int, default=0, required=True)
    args = parser.parse_args(sys.argv[sys.argv.index("--") + 1:])
    if args.test == 0:
        return False
    else:
        key = list(permutations.dict.keys())[args.test]
        print(f"Set test permutation {args.test}: {key}")
        permutations.dict[key].apply()
        return True


def render_permutations(permutations):
    """Render each permutation and generate a list, following the conventions expected by render_report.py."""
    base_output_path = bpy.context.scene.render.filepath
    base_testname = Path(bpy.data.filepath).stem
    output_paths = []
    permutation_index = 0
    for key in permutations.loop():
        permutation_index += 1
        testname = f"{permutation_index:04d}_{key}"
        filepath = f"{base_output_path}_{testname}0001.png"
        testpath = f"{base_testname}_{testname}"
        output_paths.append(testpath)
        bpy.context.scene.render.filepath = filepath
        bpy.ops.render.opengl(write_still=True, view_context=True)

    output_list_txt = bpy.data.filepath.replace(".blend", "_permutations.txt")
    with open(output_list_txt, 'w') as file:
        file.write("\n".join(output_paths))


def run_test(permutations):
    """Check if the command line requested a specific permutation, otherwise run all tests and quit Blender."""
    if set_permutation_from_args(permutations):
        return

    def run():
        render_permutations(permutations)
        bpy.ops.wm.quit_blender()

    bpy.app.timers.register(run, first_interval=1)


def ob_modes_permutations(ob, space):
    """Returns permutations for every possible object mode and overlay settings."""
    shading = space.shading
    overlay = space.overlay

    ob_modes = Permutations(None, {
        "object": lambda: bpy.ops.object.mode_set(mode='OBJECT'),
        "edit": lambda: bpy.ops.object.mode_set(mode='EDIT'),
        "sculpt": lambda: bpy.ops.object.mode_set(mode='SCULPT'),
        "vertex-paint": lambda: bpy.ops.object.mode_set(mode='VERTEX_PAINT'),
        "weight-paint": lambda: bpy.ops.object.mode_set(mode='WEIGHT_PAINT'),
        "texture-paint": lambda: bpy.ops.object.mode_set(mode='TEXTURE_PAINT'),
    })

    ob_modes.add(lambda key: "sculpt" in key, [
        Permutations("mask-off", {
            "mask-off": lambda: setattr(overlay, "show_sculpt_mask", False),
            "mask-on": lambda: setattr(overlay, "show_sculpt_mask", True),
        }),
        Permutations("sets-off", {
            "sets-off": lambda: setattr(overlay, "show_sculpt_face_sets", False),
            "sets-on": lambda: setattr(overlay, "show_sculpt_face_sets", True),
        }),
    ])

    ob_modes.add(lambda key: "paint" in key, [
        Permutations("mask-off", {
            "mask-off": lambda: (
                setattr(ob.data, "use_paint_mask", False), setattr(ob.data, "use_paint_mask_vertex", False)),
            "mask-face": lambda: (
                setattr(ob.data, "use_paint_mask", True), setattr(ob.data, "use_paint_mask_vertex", False)),
            "mask-vert": lambda: (
                setattr(ob.data, "use_paint_mask", False), setattr(ob.data, "use_paint_mask_vertex", True)),
        }),
        Permutations("paint-wire-off", {
            "paint-wire-off": lambda: setattr(overlay, "show_paint_wire", False),
            "paint-wire-on": lambda: setattr(overlay, "show_paint_wire", True),
        }),
    ])

    ob_modes.add(lambda key: "weight-paint" in key, [
        Permutations("w-contours-off", {
            "w-contours-off": lambda: setattr(overlay, "show_wpaint_contours", False),
            "w-contours-on": lambda: setattr(overlay, "show_wpaint_contours", True),
        })
    ])

    # TODO: Edit mode variants.

    shading_modes = Permutations("solid", {
        "solid": lambda: setattr(shading, "type", 'SOLID'),
        "wireframe": lambda: setattr(shading, "type", 'WIREFRAME'),
    })

    shading_modes.add(lambda key: key == "solid", [
        Permutations("xray-off", {
            "xray-off": lambda: (
                setattr(shading, "show_xray", False)),
            "xray-on": lambda: (
                setattr(shading, "show_xray", True), setattr(shading, "xray_alpha", 0.5)),
            "xray-on-alpha-1": lambda: (
                setattr(shading, "show_xray", True), setattr(shading, "xray_alpha", 1.0)),
        })
    ])

    shading_modes.add(lambda key: key == "wireframe", [
        Permutations("xray-off", {
            "xray-off": lambda: (
                setattr(shading, "show_xray_wireframe", False)),
            "xray-on": lambda: (
                setattr(shading, "show_xray_wireframe", True), setattr(shading, "xray_alpha_wireframe", 0.5)),
            "xray-on-alpha-1": lambda: (
                setattr(shading, "show_xray_wireframe", True), setattr(shading, "xray_alpha_wireframe", 1.0)),
        })
    ])

    shading_modes.add(lambda key: "solid" in key, [
        Permutations("ob-solid", {
            "ob-solid": lambda: setattr(ob, "display_type", 'SOLID'),
            "ob-wire": lambda: setattr(ob, "display_type", 'WIRE'),
        })
    ])

    shading_modes.add(lambda key: "ob-solid" in key, [
        Permutations("ob-wire-off", {
            "ob-wire-off": lambda: setattr(ob, "show_wire", False),
            "ob-wire-on": lambda: setattr(ob, "show_wire", True),
        })
    ])

    in_front_modes = Permutations("in-front-off", {
        "in-front-off": lambda: setattr(ob, "show_in_front", False),
        "in-front-on": lambda: setattr(ob, "show_in_front", True),
    })

    ob_modes.add(None, [shading_modes, in_front_modes])

    return ob_modes