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
|
# ############################################################
# Importing - Same For All Render Layer Tests
# ############################################################
import unittest
import os
import sys
from render_layer_common import *
# ############################################################
# Testing
# ############################################################
class UnitTesting(RenderLayerTesting):
def test_operator_context(self):
"""
See if render layer context is properly set/get with operators overrides
when we set render_layer in context, the collection should change as well
"""
import bpy
import os
class SampleOperator(bpy.types.Operator):
bl_idname = "testing.sample"
bl_label = "Sample Operator"
render_layer = bpy.props.StringProperty(
default="Not Set",
options={'SKIP_SAVE'},
)
scene_collection = bpy.props.StringProperty(
default="",
options={'SKIP_SAVE'},
)
use_verbose = bpy.props.BoolProperty(
default=False,
options={'SKIP_SAVE'},
)
def execute(self, context):
render_layer = context.render_layer
ret = {'FINISHED'}
# this is simply playing safe
if render_layer.name != self.render_layer:
if self.use_verbose:
print('ERROR: Render Layer mismatch: "{0}" != "{1}"'.format(
render_layer.name, self.render_layer))
ret = {'CANCELLED'}
scene_collection_name = None
if self.scene_collection:
scene_collection_name = self.scene_collection
else:
scene_collection_name = render_layer.collections.active.name
# while this is the real test
if context.scene_collection.name != scene_collection_name:
if self.use_verbose:
print('ERROR: Scene Collection mismatch: "{0}" != "{1}"'.format(
context.scene_collection.name, scene_collection_name))
ret = {'CANCELLED'}
return ret
bpy.utils.register_class(SampleOperator)
# open sample file
ROOT = self.get_root()
filepath_layers = os.path.join(ROOT, 'layers.blend')
bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_layers)
self.rename_collections()
# change the file
three_b = bpy.data.objects.get('T.3b')
three_c = bpy.data.objects.get('T.3c')
scene = bpy.context.scene
subzero = scene.master_collection.collections['1'].collections.new('sub-zero')
scorpion = subzero.collections.new('scorpion')
subzero.objects.link(three_b)
scorpion.objects.link(three_c)
layer = scene.render_layers.new('Fresh new Layer')
layer.collections.unlink(layer.collections.active)
layer.collections.link(subzero)
layer.collections.active_index = 3
self.assertEqual(layer.collections.active.name, 'scorpion')
bpy.context.workspace.render_layer = bpy.context.scene.render_layers['Viewport']
# old layer
self.assertEqual(bpy.ops.testing.sample(render_layer='Viewport', use_verbose=True), {'FINISHED'})
# expected to fail
self.assertTrue(bpy.ops.testing.sample(render_layer=layer.name), {'CANCELLED'})
# set render layer and scene collection
override = bpy.context.copy()
override["render_layer"] = layer
override["scene_collection"] = subzero
self.assertEqual(bpy.ops.testing.sample(
override,
render_layer=layer.name,
scene_collection=subzero.name, # 'sub-zero'
use_verbose=True), {'FINISHED'})
# set only render layer
override = bpy.context.copy()
override["render_layer"] = layer
self.assertEqual(bpy.ops.testing.sample(
override,
render_layer=layer.name,
scene_collection=layer.collections.active.name, # 'scorpion'
use_verbose=True), {'FINISHED'})
# ############################################################
# Main - Same For All Render Layer Tests
# ############################################################
if __name__ == '__main__':
import sys
extra_arguments = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []
sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 2:] if "--" in sys.argv else [])
UnitTesting._extra_arguments = extra_arguments
unittest.main()
|