test_layer_syncing.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_syncing(self, filepath_json, unlink_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. three_d = bpy.data.objects.get('T.3d')
  27. scene = bpy.context.scene
  28. subzero = scene.master_collection.collections['1'].collections.new('sub-zero')
  29. scorpion = scene.master_collection.collections['1'].collections.new('scorpion')
  30. # test linking sync
  31. subzero.objects.link(three_b)
  32. scorpion.objects.link(three_c)
  33. # test unlinking sync
  34. if unlink_mode in {'OBJECT', 'COLLECTION'}:
  35. scorpion.objects.link(three_d)
  36. scorpion.objects.unlink(three_d)
  37. if unlink_mode == 'COLLECTION':
  38. scorpion.objects.link(three_d)
  39. scene.master_collection.collections['1'].collections.remove(subzero)
  40. scene.master_collection.collections['1'].collections.remove(scorpion)
  41. # save file
  42. filepath_nested = os.path.join(dirpath, 'nested.blend')
  43. bpy.ops.wm.save_mainfile('EXEC_DEFAULT', filepath=filepath_nested)
  44. # get the generated json
  45. datas = query_scene(filepath_nested, 'Main', (get_scene_collections, get_layers))
  46. self.assertTrue(datas, "Data is not valid")
  47. filepath_nested_json = os.path.join(dirpath, "nested.json")
  48. with open(filepath_nested_json, "w") as f:
  49. for data in datas:
  50. f.write(dump(data))
  51. self.assertTrue(compare_files(
  52. filepath_nested_json,
  53. filepath_json,
  54. ),
  55. "Scene dump files differ")
  56. def test_syncing_link(self):
  57. """
  58. See if scene collections and layer collections are in sync
  59. when we create new subcollections and link new objects
  60. """
  61. import os
  62. ROOT = self.get_root()
  63. filepath_json = os.path.join(ROOT, 'layers_nested.json')
  64. self.do_syncing(filepath_json, 'NONE')
  65. def test_syncing_unlink_object(self):
  66. """
  67. See if scene collections and layer collections are in sync
  68. when we create new subcollections, link new objects and unlink
  69. some.
  70. """
  71. import os
  72. ROOT = self.get_root()
  73. filepath_json = os.path.join(ROOT, 'layers_nested.json')
  74. self.do_syncing(filepath_json, 'OBJECT')
  75. def test_syncing_unlink_collection(self):
  76. """
  77. See if scene collections and layer collections are in sync
  78. when we create new subcollections, link new objects and unlink full collections
  79. some.
  80. """
  81. import os
  82. ROOT = self.get_root()
  83. filepath_json = os.path.join(ROOT, 'layers.json')
  84. self.do_syncing(filepath_json, 'COLLECTION')
  85. # ############################################################
  86. # Main - Same For All Render Layer Tests
  87. # ############################################################
  88. if __name__ == '__main__':
  89. UnitTesting._extra_arguments = setup_extra_arguments(__file__)
  90. unittest.main()