Rename any instance of scene layer or render layer in code with view layer
[blender.git] / tests / python / view_layer / test_scene_write_read.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_scene_write_read(self, filepath_layers, filepath_layers_json, data_callbacks, do_read):
18         """
19         See if write/read is working for scene collections and layers
20         """
21         import bpy
22         import os
23         import tempfile
24         import filecmp
25
26         with tempfile.TemporaryDirectory() as dirpath:
27             (self.path_exists(f) for f in (filepath_layers, filepath_layers_json))
28
29             filepath_doversion = os.path.join(dirpath, 'doversion.blend')
30             filepath_saved = os.path.join(dirpath, 'doversion_saved.blend')
31             filepath_read_json = os.path.join(dirpath, "read.json")
32
33             # doversion + write test
34             bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_layers)
35             self.rename_collections()
36             bpy.ops.wm.save_mainfile('EXEC_DEFAULT', filepath=filepath_doversion)
37
38             datas = query_scene(filepath_doversion, 'Main', data_callbacks)
39             self.assertTrue(datas, "Data is not valid")
40
41             filepath_doversion_json = os.path.join(dirpath, "doversion.json")
42             with open(filepath_doversion_json, "w") as f:
43                 for data in datas:
44                     f.write(dump(data))
45
46             self.assertTrue(compare_files(
47                 filepath_doversion_json,
48                 filepath_layers_json,
49                 ),
50                 "Run: test_scene_write_layers")
51
52             if do_read:
53                 # read test, simply open and save the file
54                 bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_doversion)
55                 self.rename_collections()
56                 bpy.ops.wm.save_mainfile('EXEC_DEFAULT', filepath=filepath_saved)
57
58                 datas = query_scene(filepath_saved, 'Main', data_callbacks)
59                 self.assertTrue(datas, "Data is not valid")
60
61                 with open(filepath_read_json, "w") as f:
62                     for data in datas:
63                         f.write(dump(data))
64
65                 self.assertTrue(compare_files(
66                     filepath_read_json,
67                     filepath_layers_json,
68                     ),
69                     "Scene dump files differ")
70
71     def test_scene_write_collections(self):
72         """
73         See if the doversion and writing are working for scene collections
74         """
75         import os
76
77         ROOT = self.get_root()
78         filepath_layers = os.path.join(ROOT, 'layers.blend')
79         filepath_layers_json = os.path.join(ROOT, 'layers_simple.json')
80
81         self.do_scene_write_read(
82                 filepath_layers,
83                 filepath_layers_json,
84                 (get_scene_collections,),
85                 False)
86
87     def test_scene_write_layers(self):
88         """
89         See if the doversion and writing are working for collections and layers
90         """
91         import os
92
93         ROOT = self.get_root()
94         filepath_layers = os.path.join(ROOT, 'layers.blend')
95         filepath_layers_json = os.path.join(ROOT, 'layers.json')
96
97         self.do_scene_write_read(
98                 filepath_layers,
99                 filepath_layers_json,
100                 (get_scene_collections, get_layers),
101                 False)
102
103     def test_scene_read_collections(self):
104         """
105         See if read is working for scene collections
106         (run `test_scene_write_colections` first)
107         """
108         import os
109
110         ROOT = self.get_root()
111         filepath_layers = os.path.join(ROOT, 'layers.blend')
112         filepath_layers_json = os.path.join(ROOT, 'layers_simple.json')
113
114         self.do_scene_write_read(
115                 filepath_layers,
116                 filepath_layers_json,
117                 (get_scene_collections,),
118                 True)
119
120     def test_scene_read_layers(self):
121         """
122         See if read is working for scene layers
123         (run `test_scene_write_layers` first)
124         """
125         import os
126
127         ROOT = self.get_root()
128         filepath_layers = os.path.join(ROOT, 'layers.blend')
129         filepath_layers_json = os.path.join(ROOT, 'layers.json')
130
131         self.do_scene_write_read(
132                 filepath_layers,
133                 filepath_layers_json,
134                 (get_scene_collections, get_layers),
135                 True)
136
137
138 # ############################################################
139 # Main - Same For All Render Layer Tests
140 # ############################################################
141
142 if __name__ == '__main__':
143     UnitTesting._extra_arguments = setup_extra_arguments(__file__)
144     unittest.main()