test_scene_write_read.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_scene_write_read(self, filepath_layers, filepath_layers_json, data_callbacks, do_read):
  13. """
  14. See if write/read is working for scene collections and layers
  15. """
  16. import bpy
  17. import os
  18. import tempfile
  19. import filecmp
  20. with tempfile.TemporaryDirectory() as dirpath:
  21. (self.path_exists(f) for f in (filepath_layers, filepath_layers_json))
  22. filepath_doversion = os.path.join(dirpath, 'doversion.blend')
  23. filepath_saved = os.path.join(dirpath, 'doversion_saved.blend')
  24. filepath_read_json = os.path.join(dirpath, "read.json")
  25. # doversion + write test
  26. bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_layers)
  27. self.rename_collections()
  28. bpy.ops.wm.save_mainfile('EXEC_DEFAULT', filepath=filepath_doversion)
  29. datas = query_scene(filepath_doversion, 'Main', data_callbacks)
  30. self.assertTrue(datas, "Data is not valid")
  31. filepath_doversion_json = os.path.join(dirpath, "doversion.json")
  32. with open(filepath_doversion_json, "w") as f:
  33. for data in datas:
  34. f.write(dump(data))
  35. self.assertTrue(compare_files(
  36. filepath_doversion_json,
  37. filepath_layers_json,
  38. ),
  39. "Run: test_scene_write_layers")
  40. if do_read:
  41. # read test, simply open and save the file
  42. bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_doversion)
  43. self.rename_collections()
  44. bpy.ops.wm.save_mainfile('EXEC_DEFAULT', filepath=filepath_saved)
  45. datas = query_scene(filepath_saved, 'Main', data_callbacks)
  46. self.assertTrue(datas, "Data is not valid")
  47. with open(filepath_read_json, "w") as f:
  48. for data in datas:
  49. f.write(dump(data))
  50. self.assertTrue(compare_files(
  51. filepath_read_json,
  52. filepath_layers_json,
  53. ),
  54. "Scene dump files differ")
  55. def test_scene_write_collections(self):
  56. """
  57. See if the doversion and writing are working for scene collections
  58. """
  59. import os
  60. ROOT = self.get_root()
  61. filepath_layers = os.path.join(ROOT, 'layers.blend')
  62. filepath_layers_json = os.path.join(ROOT, 'layers_simple.json')
  63. self.do_scene_write_read(
  64. filepath_layers,
  65. filepath_layers_json,
  66. (get_scene_collections,),
  67. False)
  68. def test_scene_write_layers(self):
  69. """
  70. See if the doversion and writing are working for collections and layers
  71. """
  72. import os
  73. ROOT = self.get_root()
  74. filepath_layers = os.path.join(ROOT, 'layers.blend')
  75. filepath_layers_json = os.path.join(ROOT, 'layers.json')
  76. self.do_scene_write_read(
  77. filepath_layers,
  78. filepath_layers_json,
  79. (get_scene_collections, get_layers),
  80. False)
  81. def test_scene_read_collections(self):
  82. """
  83. See if read is working for scene collections
  84. (run `test_scene_write_colections` first)
  85. """
  86. import os
  87. ROOT = self.get_root()
  88. filepath_layers = os.path.join(ROOT, 'layers.blend')
  89. filepath_layers_json = os.path.join(ROOT, 'layers_simple.json')
  90. self.do_scene_write_read(
  91. filepath_layers,
  92. filepath_layers_json,
  93. (get_scene_collections,),
  94. True)
  95. def test_scene_read_layers(self):
  96. """
  97. See if read is working for scene layers
  98. (run `test_scene_write_layers` first)
  99. """
  100. import os
  101. ROOT = self.get_root()
  102. filepath_layers = os.path.join(ROOT, 'layers.blend')
  103. filepath_layers_json = os.path.join(ROOT, 'layers.json')
  104. self.do_scene_write_read(
  105. filepath_layers,
  106. filepath_layers_json,
  107. (get_scene_collections, get_layers),
  108. True)
  109. # ############################################################
  110. # Main - Same For All Render Layer Tests
  111. # ############################################################
  112. if __name__ == '__main__':
  113. UnitTesting._extra_arguments = setup_extra_arguments(__file__)
  114. unittest.main()