Rename any instance of scene layer or render layer in code with view layer
[blender.git] / tests / python / view_layer / test_layer_linking.py
1 # ############################################################
2 # Importing - Same For All Render Layer Tests
3 # ############################################################
4
5 import unittest
6 import os
7 import sys
8
9 from view_layer_common import *
10
11
12 # ############################################################
13 # Testing
14 # ############################################################
15
16 class UnitTesting(ViewLayerTesting):
17     def do_layer_linking(self, filepath_json, link_mode):
18         import bpy
19         import os
20         import tempfile
21         import filecmp
22
23         ROOT = self.get_root()
24         with tempfile.TemporaryDirectory() as dirpath:
25             filepath_layers = os.path.join(ROOT, 'layers.blend')
26
27             # open file
28             bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_layers)
29             self.rename_collections()
30
31             # create sub-collections
32             three_b = bpy.data.objects.get('T.3b')
33             three_c = bpy.data.objects.get('T.3c')
34
35             scene = bpy.context.scene
36
37             subzero = scene.master_collection.collections['1'].collections.new('sub-zero')
38             scorpion = subzero.collections.new('scorpion')
39
40             # test linking sync
41             subzero.objects.link(three_b)
42             scorpion.objects.link(three_c)
43
44             # test unlinking sync
45             layer = scene.view_layers.new('Fresh new Layer')
46
47             if link_mode in {'COLLECTION_LINK', 'COLLECTION_UNLINK'}:
48                 layer.collections.link(subzero)
49
50             if link_mode == 'COLLECTION_UNLINK':
51                 initial_collection = layer.collections['Master Collection']
52                 layer.collections.unlink(initial_collection)
53
54             # save file
55             filepath_nested = os.path.join(dirpath, 'nested.blend')
56             bpy.ops.wm.save_mainfile('EXEC_DEFAULT', filepath=filepath_nested)
57
58             # get the generated json
59             datas = query_scene(filepath_nested, 'Main', (get_scene_collections, get_layers))
60             self.assertTrue(datas, "Data is not valid")
61
62             filepath_nested_json = os.path.join(dirpath, "nested.json")
63             with open(filepath_nested_json, "w") as f:
64                 for data in datas:
65                     f.write(dump(data))
66
67             self.assertTrue(compare_files(
68                 filepath_nested_json,
69                 filepath_json,
70                 ),
71                 "Scene dump files differ")
72
73     def test_syncing_layer_new(self):
74         """
75         See if the creation of new layers is going well
76         """
77         import os
78         ROOT = self.get_root()
79         filepath_json = os.path.join(ROOT, 'layers_new_layer.json')
80         self.do_layer_linking(filepath_json, 'LAYER_NEW')
81
82     def test_syncing_layer_collection_link(self):
83         """
84         See if the creation of new layers is going well
85         And linking a new scene collection in the layer works
86         """
87         import os
88         ROOT = self.get_root()
89         filepath_json = os.path.join(ROOT, 'layers_layer_collection_link.json')
90         self.do_layer_linking(filepath_json, 'COLLECTION_LINK')
91
92     def test_syncing_layer_collection_unlink(self):
93         """
94         See if the creation of new layers is going well
95         And unlinking the origin scene collection works
96         """
97         import os
98         ROOT = self.get_root()
99         filepath_json = os.path.join(ROOT, 'layers_layer_collection_unlink.json')
100         self.do_layer_linking(filepath_json, 'COLLECTION_UNLINK')
101
102
103 # ############################################################
104 # Main - Same For All Render Layer Tests
105 # ############################################################
106
107 if __name__ == '__main__':
108     UnitTesting._extra_arguments = setup_extra_arguments(__file__)
109     unittest.main()