3
0

DetachPrefab_WithNestedEntities.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. def DetachPrefab_WithNestedEntities():
  7. from pathlib import Path
  8. import pyside_utils
  9. @pyside_utils.wrap_async
  10. async def run_test():
  11. import azlmbr.bus as bus
  12. import azlmbr.editor as editor
  13. import azlmbr.legacy.general as general
  14. import azlmbr.math as math
  15. from editor_python_test_tools.editor_entity_utils import EditorEntity
  16. from editor_python_test_tools.prefab_utils import Prefab
  17. from editor_python_test_tools.wait_utils import PrefabWaiter
  18. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  19. TEST_PREFAB_FILE_NAME = Path(__file__).stem + '_prefab1'
  20. NESTED_ENTITIES_NAME_PREFIX = 'Entity_'
  21. NUM_NESTED_ENTITIES_LEVELS = 10
  22. POSITION = math.Vector3(0.0, 0.0, 0.0)
  23. prefab_test_utils.open_base_tests_level()
  24. # Creates new nested entities at the root level. Asserts if creation didn't succeed
  25. nested_entities_root = prefab_test_utils.create_linear_nested_entities(NESTED_ENTITIES_NAME_PREFIX,
  26. NUM_NESTED_ENTITIES_LEVELS, POSITION)
  27. level_entity = nested_entities_root.get_parent_id()
  28. prefab_test_utils.validate_linear_nested_entities(nested_entities_root, NUM_NESTED_ENTITIES_LEVELS,
  29. POSITION)
  30. # Creates a prefab from the nested entities. Asserts if prefab creation doesn't succeed
  31. _, nested_entities_prefab_instance = Prefab.create_prefab([nested_entities_root],
  32. TEST_PREFAB_FILE_NAME)
  33. nested_entities_root_on_instance = nested_entities_prefab_instance.get_direct_child_entities()[0]
  34. prefab_test_utils.validate_linear_nested_entities(nested_entities_root_on_instance, NUM_NESTED_ENTITIES_LEVELS,
  35. POSITION)
  36. # Detaches the nested entities prefab instance
  37. Prefab.detach_prefab(nested_entities_prefab_instance)
  38. # Test undo/redo on prefab detach
  39. general.undo()
  40. PrefabWaiter.wait_for_propagation()
  41. is_prefab = editor.EditorComponentAPIBus(bus.Broadcast, "HasComponentOfType",
  42. nested_entities_prefab_instance.container_entity.id,
  43. azlmbr.globals.property.EditorPrefabComponentTypeId)
  44. assert is_prefab, "Undo operation failed. Entity hierarchy is not recognized as a prefab."
  45. prefab_test_utils.validate_linear_nested_entities(nested_entities_root_on_instance, NUM_NESTED_ENTITIES_LEVELS,
  46. POSITION)
  47. general.redo()
  48. PrefabWaiter.wait_for_propagation()
  49. former_container_entity = EditorEntity.find_editor_entity(TEST_PREFAB_FILE_NAME)
  50. root_entity = EditorEntity.find_editor_entity("Entity_0")
  51. is_prefab = editor.EditorComponentAPIBus(bus.Broadcast, "HasComponentOfType", former_container_entity.id,
  52. azlmbr.globals.property.EditorPrefabComponentTypeId)
  53. prefab_test_utils.validate_linear_nested_entities(root_entity, NUM_NESTED_ENTITIES_LEVELS, POSITION)
  54. assert not is_prefab, "Redo operation failed. Entity is still recognized as a prefab."
  55. run_test()
  56. if __name__ == "__main__":
  57. from editor_python_test_tools.utils import Report
  58. Report.start_test(DetachPrefab_WithNestedEntities)