test_object_copy.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_object_copy(self, 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. filepath_json = os.path.join(ROOT, 'layers_object_copy_duplicate.json')
  21. # open file
  22. bpy.ops.wm.open_mainfile('EXEC_DEFAULT', filepath=filepath_layers)
  23. self.rename_collections()
  24. # create sub-collections
  25. three_b = bpy.data.objects.get('T.3b')
  26. three_c = bpy.data.objects.get('T.3c')
  27. scene = bpy.context.scene
  28. subzero = scene.master_collection.collections['1'].collections.new('sub-zero')
  29. scorpion = subzero.collections.new('scorpion')
  30. subzero.objects.link(three_b)
  31. scorpion.objects.link(three_c)
  32. layer = scene.view_layers.new('Fresh new Layer')
  33. layer.collections.link(subzero)
  34. bpy.context.window.view_layer = bpy.context.scene.view_layers['Fresh new Layer']
  35. if mode == 'DUPLICATE':
  36. # assuming the latest layer is the active layer
  37. bpy.ops.object.select_all(action='DESELECT')
  38. three_c.select_set(True)
  39. bpy.ops.object.duplicate()
  40. elif mode == 'NAMED':
  41. bpy.ops.object.add_named(name=three_c.name)
  42. # save file
  43. filepath_objects = os.path.join(dirpath, 'objects.blend')
  44. bpy.ops.wm.save_mainfile('EXEC_DEFAULT', filepath=filepath_objects)
  45. # get the generated json
  46. datas = query_scene(filepath_objects, 'Main', (get_scene_collections, get_layers))
  47. self.assertTrue(datas, "Data is not valid")
  48. filepath_objects_json = os.path.join(dirpath, "objects.json")
  49. with open(filepath_objects_json, "w") as f:
  50. for data in datas:
  51. f.write(dump(data))
  52. self.assertTrue(compare_files(
  53. filepath_objects_json,
  54. filepath_json,
  55. ),
  56. "Scene dump files differ")
  57. def test_copy_object(self):
  58. """
  59. OBJECT_OT_duplicate
  60. """
  61. self.do_object_copy('DUPLICATE')
  62. def test_copy_object_named(self):
  63. """
  64. OBJECT_OT_add_named
  65. """
  66. self.do_object_copy('NAMED')
  67. # ############################################################
  68. # Main - Same For All Render Layer Tests
  69. # ############################################################
  70. if __name__ == '__main__':
  71. UnitTesting._extra_arguments = setup_extra_arguments(__file__)
  72. unittest.main()