File: bl_node_group_compat.py

package info (click to toggle)
blender 4.3.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 309,564 kB
  • sloc: cpp: 2,385,210; python: 330,236; ansic: 280,972; xml: 2,446; sh: 972; javascript: 317; makefile: 170
file content (371 lines) | stat: -rw-r--r-- 18,605 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# SPDX-FileCopyrightText: 2021-2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later

import pathlib
import sys
import unittest
import tempfile
import math
from dataclasses import dataclass

import bpy

args = None


base_idname = {
    "VALUE": "NodeSocketFloat",
    "INT": "NodeSocketInt",
    "BOOLEAN": "NodeSocketBool",
    "ROTATION": "NodeSocketRotation",
    "VECTOR": "NodeSocketVector",
    "RGBA": "NodeSocketColor",
    "STRING": "NodeSocketString",
    "SHADER": "NodeSocketShader",
    "OBJECT": "NodeSocketObject",
    "IMAGE": "NodeSocketImage",
    "GEOMETRY": "NodeSocketGeometry",
    "COLLECTION": "NodeSocketCollection",
    "TEXTURE": "NodeSocketTexture",
    "MATERIAL": "NodeSocketMaterial",
}


subtype_idname = {
    ("VALUE", "NONE"): "NodeSocketFloat",
    ("VALUE", "UNSIGNED"): "NodeSocketFloatUnsigned",
    ("VALUE", "PERCENTAGE"): "NodeSocketFloatPercentage",
    ("VALUE", "FACTOR"): "NodeSocketFloatFactor",
    ("VALUE", "ANGLE"): "NodeSocketFloatAngle",
    ("VALUE", "TIME"): "NodeSocketFloatTime",
    ("VALUE", "TIME_ABSOLUTE"): "NodeSocketFloatTimeAbsolute",
    ("VALUE", "DISTANCE"): "NodeSocketFloatDistance",
    ("INT", "NONE"): "NodeSocketInt",
    ("INT", "UNSIGNED"): "NodeSocketIntUnsigned",
    ("INT", "PERCENTAGE"): "NodeSocketIntPercentage",
    ("INT", "FACTOR"): "NodeSocketIntFactor",
    ("BOOLEAN", "NONE"): "NodeSocketBool",
    ("ROTATION", "NONE"): "NodeSocketRotation",
    ("VECTOR", "NONE"): "NodeSocketVector",
    ("VECTOR", "TRANSLATION"): "NodeSocketVectorTranslation",
    ("VECTOR", "DIRECTION"): "NodeSocketVectorDirection",
    ("VECTOR", "VELOCITY"): "NodeSocketVectorVelocity",
    ("VECTOR", "ACCELERATION"): "NodeSocketVectorAcceleration",
    ("VECTOR", "EULER"): "NodeSocketVectorEuler",
    ("VECTOR", "XYZ"): "NodeSocketVectorXYZ",
    ("RGBA", "NONE"): "NodeSocketColor",
    ("STRING", "NONE"): "NodeSocketString",
    ("STRING", "FILEPATH"): "NodeSocketStringFilePath",
    ("SHADER", "NONE"): "NodeSocketShader",
    ("OBJECT", "NONE"): "NodeSocketObject",
    ("IMAGE", "NONE"): "NodeSocketImage",
    ("GEOMETRY", "NONE"): "NodeSocketGeometry",
    ("COLLECTION", "NONE"): "NodeSocketCollection",
    ("TEXTURE", "NONE"): "NodeSocketTexture",
    ("MATERIAL", "NONE"): "NodeSocketMaterial",
}


@dataclass
class SocketSpec():
    name: str
    identifier: str
    type: str
    subtype: str = 'NONE'
    hide_value: bool = False
    hide_in_modifier: bool = False
    default_value: object = None
    min_value: object = None
    max_value: object = None
    internal_links: int = 1
    external_links: int = 1

    @property
    def base_idname(self):
        return base_idname[self.type]

    @property
    def subtype_idname(self):
        return subtype_idname[(self.type, self.subtype)]


class AbstractNodeGroupInterfaceTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.testdir = args.testdir
        cls._tempdir = tempfile.TemporaryDirectory()
        cls.tempdir = pathlib.Path(cls._tempdir.name)

    def setUp(self):
        self.assertTrue(self.testdir.exists(),
                        'Test dir {0} should exist'.format(self.testdir))

    def tearDown(self):
        self._tempdir.cleanup()

    def subtype_compare(self, value, expected, subtype):
        # Rounding errors introduced at various levels, only check for roughly expected values.
        if subtype in {'ANGLE', 'EULER'}:
            # Angle values are shown in degrees in the UI, but stored as radians.
            self.assertAlmostEqual(value, math.radians(expected))
        else:
            self.assertAlmostEqual(value, expected)

    # Test properties of a node group item and associated node socket with spec data.
    def compare_group_socket_to_spec(self, item, node, spec: SocketSpec, test_links=True):
        group = item.id_data

        # Examine the interface item.
        self.assertEqual(item.name, spec.name)
        self.assertEqual(item.bl_socket_idname, spec.base_idname)
        self.assertEqual(item.identifier, spec.identifier)

        # Types that have subtypes.
        if spec.type in {'VALUE', 'INT', 'VECTOR'}:
            self.assertEqual(item.subtype, spec.subtype)

        self.assertEqual(item.hide_value, spec.hide_value)
        self.assertEqual(item.hide_in_modifier, spec.hide_in_modifier)

        if spec.type in {'VALUE', 'INT'}:
            self.subtype_compare(item.default_value, spec.default_value, spec.subtype)
            self.assertEqual(item.min_value, spec.min_value)
            self.assertEqual(item.max_value, spec.max_value)
        elif spec.type == 'VECTOR':
            self.subtype_compare(item.default_value[0], spec.default_value[0], spec.subtype)
            self.subtype_compare(item.default_value[1], spec.default_value[1], spec.subtype)
            self.subtype_compare(item.default_value[2], spec.default_value[2], spec.subtype)
            self.assertEqual(item.min_value, spec.min_value)
            self.assertEqual(item.max_value, spec.max_value)
        elif spec.type == 'RGBA':
            # Colors stored as int8 internally, enough rounding error to require fuzzy test.
            self.assertAlmostEqual(item.default_value[0], spec.default_value[0])
            self.assertAlmostEqual(item.default_value[1], spec.default_value[1])
            self.assertAlmostEqual(item.default_value[2], spec.default_value[2])
            self.assertAlmostEqual(item.default_value[3], spec.default_value[3])
        elif spec.type in {'STRING', 'BOOLEAN', 'MATERIAL', 'TEXTURE', 'OBJECT', 'COLLECTION', 'IMAGE'}:
            self.assertEqual(item.default_value, spec.default_value)
        elif spec.type in {'SHADER', 'GEOMETRY'}:
            pass
        else:
            # Add socket type testing above if this happens.
            self.fail("Socket type not supported by test")

        # Examine the node socket.
        if 'INPUT' in item.in_out:
            socket = next(s for s in node.inputs if s.identifier == spec.identifier)
            self.assertIsNotNone(socket, f"Could not find socket for group input identifier {spec.identifier}")
            self.assertEqual(socket.name, spec.name)
            self.assertEqual(socket.bl_idname, spec.subtype_idname)
            self.assertEqual(socket.type, spec.type)
            self.assertEqual(socket.hide_value, spec.hide_value)
            if test_links:
                self.assertEqual(len(socket.links), spec.external_links,
                                 f"Socket should have exactly {spec.external_links} external connections")

            input_node = next(n for n in group.nodes if n.bl_idname == 'NodeGroupInput')
            self.assertIsNotNone(input_node, "Could not find an input node in the group")
            socket = next(s for s in input_node.outputs if s.identifier == spec.identifier)
            self.assertIsNotNone(
                socket, f"Could not find group input socket for group input identifier {spec.identifier}")
            self.assertEqual(socket.name, spec.name)
            self.assertEqual(socket.bl_idname, spec.subtype_idname)
            self.assertEqual(socket.type, spec.type)
            self.assertEqual(socket.hide_value, spec.hide_value)
            if test_links:
                self.assertEqual(len(socket.links), spec.internal_links,
                                 f"Socket should have exactly {spec.internal_links} internal connections")

        if 'OUTPUT' in item.in_out:
            socket = next(s for s in node.outputs if s.identifier == spec.identifier)
            self.assertIsNotNone(socket, f"Could not find socket for group output identifier {spec.identifier}")
            self.assertEqual(socket.name, spec.name)
            self.assertEqual(socket.bl_idname, spec.subtype_idname)
            self.assertEqual(socket.type, spec.type)
            self.assertEqual(socket.hide_value, spec.hide_value)
            if test_links:
                self.assertEqual(len(socket.links), spec.external_links,
                                 f"Socket should have exactly {spec.external_links} external connections")

            output_node = next(n for n in group.nodes if n.bl_idname == 'NodeGroupOutput')
            self.assertIsNotNone(output_node, "Could not find an output node in the group")
            socket = next(s for s in output_node.inputs if s.identifier == spec.identifier)
            self.assertIsNotNone(
                socket, f"Could not find group output socket for group output identifier {spec.identifier}")
            self.assertEqual(socket.name, spec.name)
            self.assertEqual(socket.bl_idname, spec.subtype_idname)
            self.assertEqual(socket.type, spec.type)
            self.assertEqual(socket.hide_value, spec.hide_value)
            if test_links:
                self.assertEqual(len(socket.links), spec.internal_links,
                                 f"Socket should have exactly {spec.internal_links} internal connections")

    # Test node group items and associated node sockets with spec data.
    def compare_group_to_specs(self, group, node, specs, test_links=True):
        for index, spec in enumerate(specs):
            self.compare_group_socket_to_spec(group.interface.items_tree[index], node, spec, test_links=test_links)


class NodeGroupVersioning36Test(AbstractNodeGroupInterfaceTest):
    def open_file(self):
        bpy.ops.wm.open_mainfile(filepath=str(self.testdir / "nodegroup36.blend"))
        self.assertEqual(bpy.data.version, (3, 6, 11))

    def test_load_compositor_nodes(self):
        self.open_file()

        tree = bpy.data.scenes['Scene'].node_tree
        group = bpy.data.node_groups.get('NodeGroup')
        self.assertIsNotNone(group, "Compositor node group not found")
        node = tree.nodes['Group']
        self.assertEqual(node.node_tree, group, "Node group must use compositor node tree")

        # autopep8: off
        self.compare_group_to_specs(group, node, [
            SocketSpec("Output Float", "Output_9", "VALUE", hide_value=True, default_value=3.0, min_value=1.0, max_value=1.0),
            SocketSpec("Output Vector", "Output_10", "VECTOR", subtype="EULER", default_value=( 10, 20, 30), min_value=-10.0, max_value=10.0),
            SocketSpec("Output Color", "Output_11", "RGBA", default_value=(0, 1, 1, 1)),

            SocketSpec("Input Float", "Input_6", "VALUE", subtype="ANGLE", default_value=-20.0, min_value=5.0, max_value=6.0),
            SocketSpec("Input Vector", "Input_7", "VECTOR", hide_value=True, default_value=( 2, 4, 6), min_value=-4.0, max_value=100.0),
            SocketSpec("Input Color", "Input_8", "RGBA", default_value=(0.5, 0.4, 0.3, 0.2)),
        ])
        # autopep8: on

    def test_load_shader_nodes(self):
        self.open_file()

        tree = bpy.data.materials['Material'].node_tree
        group = bpy.data.node_groups.get('NodeGroup.003')
        self.assertIsNotNone(group, "Shader node group not found")
        node = tree.nodes['Group']
        self.assertEqual(node.node_tree, group, "Node group must use shader node tree")

        # autopep8: off
        self.compare_group_to_specs(group, node, [
            SocketSpec("Output Float", "Output_30", "VALUE", hide_value=True, default_value=3.0, min_value=1.0, max_value=1.0),
            SocketSpec("Output Vector", "Output_31", "VECTOR", subtype="EULER", default_value=( 10, 20, 30), min_value=-10.0, max_value=10.0),
            SocketSpec("Output Color", "Output_32", "RGBA", default_value=(0, 1, 1, 1)),
            SocketSpec("Output Shader", "Output_33", "SHADER"),

            SocketSpec("Input Float", "Input_26", "VALUE", subtype="ANGLE", default_value=-20.0, min_value=5.0, max_value=6.0),
            SocketSpec("Input Vector", "Input_27", "VECTOR", hide_value=True, default_value=( 2, 4, 6), min_value=-4.0, max_value=100.0),
            SocketSpec("Input Color", "Input_28", "RGBA", default_value=(0.5, 0.4, 0.3, 0.2)),
            SocketSpec("Input Shader", "Input_29", "SHADER"),
        ])
        # autopep8: on

    def test_load_geometry_nodes(self):
        self.open_file()

        tree = bpy.data.node_groups['Geometry Nodes']
        group = bpy.data.node_groups.get('NodeGroup.002')
        self.assertIsNotNone(group, "Geometry node group not found")
        node = tree.nodes['Group']
        self.assertEqual(node.node_tree, group, "Node group must use geometry node tree")

        # autopep8: off
        self.compare_group_to_specs(group, node, [
            SocketSpec("Output Float", "Output_7", "VALUE", hide_value=True, default_value=3.0, min_value=1.0, max_value=1.0),
            SocketSpec("Output Vector", "Output_8", "VECTOR", subtype="EULER", default_value=( 10, 20, 30), min_value=-10.0, max_value=10.0),
            SocketSpec("Output Color", "Output_9", "RGBA", default_value=(0, 1, 1, 1)),
            SocketSpec("Output String", "Output_19", "STRING", default_value=""),
            SocketSpec("Output Bool", "Output_20", "BOOLEAN", default_value=False),
            SocketSpec("Output Material", "Output_21", "MATERIAL", default_value=bpy.data.materials['TestMaterial']),
            SocketSpec("Output Int", "Output_22", "INT", default_value=0, min_value=-2147483648, max_value=2147483647),
            SocketSpec("Output Geometry", "Output_23", "GEOMETRY"),
            SocketSpec("Output Collection", "Output_24", "COLLECTION", default_value=bpy.data.collections['TestCollection']),
            SocketSpec("Output Texture", "Output_25", "TEXTURE", default_value=bpy.data.textures['TestTexture']),
            SocketSpec("Output Object", "Output_26", "OBJECT", default_value=bpy.data.objects['TestObject']),
            SocketSpec("Output Image", "Output_27", "IMAGE", default_value=bpy.data.images['TestImage']),

            SocketSpec("Input Float", "Input_4", "VALUE", subtype="ANGLE", default_value=-20.0, min_value=5.0, max_value=6.0),
            SocketSpec("Input Vector", "Input_5", "VECTOR", hide_value=True, default_value=( 2, 4, 6), min_value=-4.0, max_value=100.0),
            SocketSpec("Input Color", "Input_6", "RGBA", default_value=(0.5, 0.4, 0.3, 0.2)),
            SocketSpec("Input String", "Input_10", "STRING", default_value="hello world!"),
            SocketSpec("Input Bool", "Input_11", "BOOLEAN", default_value=True, hide_in_modifier=True),
            SocketSpec("Input Material", "Input_12", "MATERIAL", default_value=bpy.data.materials['TestMaterial']),
            SocketSpec("Input Int", "Input_13", "INT", default_value=500, min_value=200, max_value=1000),
            SocketSpec("Input Geometry", "Input_14", "GEOMETRY"),
            SocketSpec("Input Collection", "Input_15", "COLLECTION", default_value=bpy.data.collections['TestCollection']),
            SocketSpec("Input Texture", "Input_16", "TEXTURE", default_value=bpy.data.textures['TestTexture']),
            SocketSpec("Input Object", "Input_17", "OBJECT", default_value=bpy.data.objects['TestObject']),
            SocketSpec("Input Image", "Input_18", "IMAGE", default_value=bpy.data.images['TestImage']),
        ])
        # autopep8: on


class NodeGroupVersioning25Test(AbstractNodeGroupInterfaceTest):
    def open_file(self):
        bpy.ops.wm.open_mainfile(filepath=str(self.testdir / "nodegroup25.blend"))
        self.assertEqual(bpy.data.version, (2, 55, 0))

    def test_load_compositor_nodes(self):
        self.open_file()

        tree = bpy.data.scenes['Scene'].node_tree
        group = bpy.data.node_groups.get('NodeGroup.002')
        self.assertIsNotNone(group, "Compositor node group not found")
        node = tree.nodes['NodeGroup.002']
        self.assertEqual(node.node_tree, group, "Node group must use compositor node tree")

        # autopep8: off
        self.compare_group_to_specs(group, node, [
            SocketSpec("Image", "Image", "RGBA", default_value=(0, 0, 0, 1)),
            SocketSpec("Alpha", "Alpha", "VALUE", default_value=1.0, min_value=0.0, max_value=0.0),
            SocketSpec("Alpha", "Alpha.001", "VALUE", default_value=0.0, min_value=0.0, max_value=0.0),
            SocketSpec("Alpha", "Alpha.002", "VALUE", default_value=0.0, min_value=0.0, max_value=0.0),

            SocketSpec("Fac", "Fac", "VALUE", default_value=0.5, min_value=0.0, max_value=0.0),
            SocketSpec("ID value", "ID value", "VALUE", default_value=0.8, min_value=0.0, max_value=0.0),
            SocketSpec("ID value", "ID value.001", "VALUE", default_value=0.8, min_value=0.0, max_value=0.0),
        ], test_links=False)
        # autopep8: on

    def test_load_shader_nodes(self):
        self.open_file()

        tree = bpy.data.materials['Material'].node_tree
        group = bpy.data.node_groups.get('NodeGroup')
        self.assertIsNotNone(group, "Shader node group not found")
        node = tree.nodes['NodeGroup']
        self.assertEqual(node.node_tree, group, "Node group must use shader node tree")

        # autopep8: off
        self.compare_group_to_specs(group, node, [
            SocketSpec("Color", "Color", "RGBA", default_value=(0, 0, 0, 1)),
            SocketSpec("Color", "Color.001", "RGBA", default_value=(0, 0, 0, 1)),
            SocketSpec("Vector", "Vector", "VECTOR", default_value=(0, 0, 0), min_value=0.0, max_value=0.0),
            SocketSpec("Value", "Value", "VALUE", default_value=0.0, min_value=0.0, max_value=0.0),

            SocketSpec("Fac", "Fac", "VALUE", default_value=0.5, min_value=0.0, max_value=0.0),
            SocketSpec("Color1", "Color1", "RGBA", default_value=(0.5, 0.5, 0.5, 1)),
            SocketSpec("Color2", "Color2", "RGBA", default_value=(0.5, 0.5, 0.5, 1)),
            SocketSpec("Fac", "Fac.001", "VALUE", default_value=0.5, min_value=0.0, max_value=0.0),
            SocketSpec("Color1", "Color1.001", "RGBA", default_value=(0.5, 0.5, 0.5, 1)),
            SocketSpec("Color2", "Color2.001", "RGBA", default_value=(0.5, 0.5, 0.5, 1)),
            SocketSpec("Vector", "Vector", "VECTOR", default_value=(0.5, 0.5, 0.5), min_value=0.0, max_value=0.0),
            SocketSpec("Vector", "Vector.001", "VECTOR", default_value=(0.5, 0.5, 0.5), min_value=0.0, max_value=0.0),
        ], test_links=False)
        # autopep8: on


def main():
    global args
    import argparse

    if '--' in sys.argv:
        argv = [sys.argv[0]] + sys.argv[sys.argv.index('--') + 1:]
    else:
        argv = sys.argv

    parser = argparse.ArgumentParser()
    parser.add_argument('--testdir', required=True, type=pathlib.Path)
    args, remaining = parser.parse_known_args(argv)

    unittest.main(argv=remaining)


if __name__ == "__main__":
    main()