Merge branch 'master' into blender2.8
[blender.git] / tests / python / render_layer / test_operator_context.py
1 # ############################################################
2 # Importing - Same For All Render Layer Tests
3 # ############################################################
4
5 import unittest
6 import os
7 import sys
8
9 from render_layer_common import *
10
11
12 # ############################################################
13 # Testing
14 # ############################################################
15
16 class UnitTesting(RenderLayerTesting):
17     def test_operator_context(self):
18         """
19         See if render layer context is properly set/get with operators overrides
20         when we set render_layer in context, the collection should change as well
21         """
22         import bpy
23         import os
24
25         class SampleOperator(bpy.types.Operator):
26             bl_idname = "testing.sample"
27             bl_label = "Sample Operator"
28
29             render_layer = bpy.props.StringProperty(
30                     default="Not Set",
31                     options={'SKIP_SAVE'},
32                     )
33
34             scene_collection = bpy.props.StringProperty(
35                     default="",
36                     options={'SKIP_SAVE'},
37                     )
38
39             use_verbose = bpy.props.BoolProperty(
40                     default=False,
41                     options={'SKIP_SAVE'},
42                     )
43
44             def execute(self, context):
45                 render_layer = context.render_layer
46                 ret = {'FINISHED'}
47
48                 # this is simply playing safe
49                 if render_layer.name != self.render_layer:
50                     if self.use_verbose:
51                         print('ERROR: Render Layer mismatch: "{0}" != "{1}"'.format(
52                             render_layer.name, self.render_layer))
53                     ret = {'CANCELLED'}
54
55                 scene_collection_name = None
56                 if self.scene_collection:
57                     scene_collection_name = self.scene_collection
58                 else:
59                     scene_collection_name = render_layer.collections.active.name
60
61                 # while this is the real test
62                 if context.scene_collection.name != scene_collection_name:
63                     if self.use_verbose:
64                         print('ERROR: Scene Collection mismatch: "{0}" != "{1}"'.format(
65                             context.scene_collection.name, scene_collection_name))
66                     ret = {'CANCELLED'}
67                 return ret
68
69         bpy.utils.register_class(SampleOperator)
70
71         # open sample file
72         ROOT = self.get_root()
73         filepath_layers = os.path.join(ROOT, 'layers.blend')
74         bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_layers)
75         self.rename_collections()
76
77         # change the file
78         three_b = bpy.data.objects.get('T.3b')
79         three_c = bpy.data.objects.get('T.3c')
80         scene = bpy.context.scene
81         subzero = scene.master_collection.collections['1'].collections.new('sub-zero')
82         scorpion = subzero.collections.new('scorpion')
83         subzero.objects.link(three_b)
84         scorpion.objects.link(three_c)
85         layer = scene.render_layers.new('Fresh new Layer')
86         layer.collections.unlink(layer.collections.active)
87         layer.collections.link(subzero)
88         layer.collections.active_index = 3
89         self.assertEqual(layer.collections.active.name, 'scorpion')
90
91         # Change active scene layer (do it for workspace too just to don't get mangled in workspace bugs)
92         scene = bpy.context.scene
93         scene.render_layers.active_index = len(scene.render_layers) - 2
94         self.assertEqual(scene.render_layers.active.name, "Viewport")
95         bpy.context.workspace.render_layer = bpy.context.scene.render_layers['Viewport']
96
97         # old layer
98         self.assertEqual(bpy.ops.testing.sample(render_layer='Viewport', use_verbose=True), {'FINISHED'})
99
100         # expected to fail
101         self.assertTrue(bpy.ops.testing.sample(render_layer=layer.name), {'CANCELLED'})
102
103         # set render layer and scene collection
104         override = bpy.context.copy()
105         override["render_layer"] = layer
106         override["scene_collection"] = subzero
107         self.assertEqual(bpy.ops.testing.sample(
108             override,
109             render_layer=layer.name,
110             scene_collection=subzero.name,  # 'sub-zero'
111             use_verbose=True), {'FINISHED'})
112
113         # set only render layer
114         override = bpy.context.copy()
115         override["render_layer"] = layer
116
117         self.assertEqual(bpy.ops.testing.sample(
118             override,
119             render_layer=layer.name,
120             scene_collection=layer.collections.active.name,  # 'scorpion'
121             use_verbose=True), {'FINISHED'})
122
123
124 # ############################################################
125 # Main - Same For All Render Layer Tests
126 # ############################################################
127
128 if __name__ == '__main__':
129     UnitTesting._extra_arguments = setup_extra_arguments(__file__)
130     unittest.main()