Rename any instance of scene layer or render layer in code with view layer
[blender.git] / tests / python / view_layer / test_object_copy.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_object_copy(self, 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             filepath_json = os.path.join(ROOT, 'layers_object_copy_duplicate.json')
27
28             # open file
29             bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_layers)
30             self.rename_collections()
31
32             # create sub-collections
33             three_b = bpy.data.objects.get('T.3b')
34             three_c = bpy.data.objects.get('T.3c')
35
36             scene = bpy.context.scene
37             subzero = scene.master_collection.collections['1'].collections.new('sub-zero')
38             scorpion = subzero.collections.new('scorpion')
39             subzero.objects.link(three_b)
40             scorpion.objects.link(three_c)
41             layer = scene.view_layers.new('Fresh new Layer')
42             layer.collections.link(subzero)
43
44             scene.view_layers.active_index = len(scene.view_layers) - 1
45             bpy.context.workspace.view_layer = bpy.context.scene.view_layers['Fresh new Layer']
46
47             if mode == 'DUPLICATE':
48                 # assuming the latest layer is the active layer
49                 bpy.ops.object.select_all(action='DESELECT')
50                 three_c.select_set(action='SELECT')
51                 bpy.ops.object.duplicate()
52
53             elif mode == 'NAMED':
54                 bpy.ops.object.add_named(name=three_c.name)
55
56             # save file
57             filepath_objects = os.path.join(dirpath, 'objects.blend')
58             bpy.ops.wm.save_mainfile('EXEC_DEFAULT', filepath=filepath_objects)
59
60             # get the generated json
61             datas = query_scene(filepath_objects, 'Main', (get_scene_collections, get_layers))
62             self.assertTrue(datas, "Data is not valid")
63
64             filepath_objects_json = os.path.join(dirpath, "objects.json")
65             with open(filepath_objects_json, "w") as f:
66                 for data in datas:
67                     f.write(dump(data))
68
69             self.assertTrue(compare_files(
70                 filepath_objects_json,
71                 filepath_json,
72                 ),
73                 "Scene dump files differ")
74
75     def test_copy_object(self):
76         """
77         OBJECT_OT_duplicate
78         """
79         self.do_object_copy('DUPLICATE')
80
81     def test_copy_object_named(self):
82         """
83         OBJECT_OT_add_named
84         """
85         self.do_object_copy('NAMED')
86
87
88 # ############################################################
89 # Main - Same For All Render Layer Tests
90 # ############################################################
91
92 if __name__ == '__main__':
93     UnitTesting._extra_arguments = setup_extra_arguments(__file__)
94     unittest.main()