test_layer_linking.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 do_layer_linking(self, filepath_json, link_mode):
  13. import bpy
  14. import os
  15. import tempfile
  16. import filecmp
  17. ROOT = self.get_root()
  18. with tempfile.TemporaryDirectory() as dirpath:
  19. filepath_layers = os.path.join(ROOT, 'layers.blend')
  20. # open file
  21. bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_layers)
  22. self.rename_collections()
  23. # create sub-collections
  24. three_b = bpy.data.objects.get('T.3b')
  25. three_c = bpy.data.objects.get('T.3c')
  26. scene = bpy.context.scene
  27. subzero = scene.master_collection.collections['1'].collections.new('sub-zero')
  28. scorpion = subzero.collections.new('scorpion')
  29. # test linking sync
  30. subzero.objects.link(three_b)
  31. scorpion.objects.link(three_c)
  32. # test unlinking sync
  33. layer = scene.view_layers.new('Fresh new Layer')
  34. if link_mode in {'COLLECTION_LINK', 'COLLECTION_UNLINK'}:
  35. layer.collections.link(subzero)
  36. if link_mode == 'COLLECTION_UNLINK':
  37. initial_collection = layer.collections['Master Collection']
  38. layer.collections.unlink(initial_collection)
  39. # save file
  40. filepath_nested = os.path.join(dirpath, 'nested.blend')
  41. bpy.ops.wm.save_mainfile('EXEC_DEFAULT', filepath=filepath_nested)
  42. # get the generated json
  43. datas = query_scene(filepath_nested, 'Main', (get_scene_collections, get_layers))
  44. self.assertTrue(datas, "Data is not valid")
  45. filepath_nested_json = os.path.join(dirpath, "nested.json")
  46. with open(filepath_nested_json, "w") as f:
  47. for data in datas:
  48. f.write(dump(data))
  49. self.assertTrue(compare_files(
  50. filepath_nested_json,
  51. filepath_json,
  52. ),
  53. "Scene dump files differ")
  54. def test_syncing_layer_new(self):
  55. """
  56. See if the creation of new layers is going well
  57. """
  58. import os
  59. ROOT = self.get_root()
  60. filepath_json = os.path.join(ROOT, 'layers_new_layer.json')
  61. self.do_layer_linking(filepath_json, 'LAYER_NEW')
  62. def test_syncing_layer_collection_link(self):
  63. """
  64. See if the creation of new layers is going well
  65. And linking a new scene collection in the layer works
  66. """
  67. import os
  68. ROOT = self.get_root()
  69. filepath_json = os.path.join(ROOT, 'layers_layer_collection_link.json')
  70. self.do_layer_linking(filepath_json, 'COLLECTION_LINK')
  71. def test_syncing_layer_collection_unlink(self):
  72. """
  73. See if the creation of new layers is going well
  74. And unlinking the origin scene collection works
  75. """
  76. import os
  77. ROOT = self.get_root()
  78. filepath_json = os.path.join(ROOT, 'layers_layer_collection_unlink.json')
  79. self.do_layer_linking(filepath_json, 'COLLECTION_UNLINK')
  80. # ############################################################
  81. # Main - Same For All Render Layer Tests
  82. # ############################################################
  83. if __name__ == '__main__':
  84. UnitTesting._extra_arguments = setup_extra_arguments(__file__)
  85. unittest.main()