test_operator_context.py 4.8 KB

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