ReparentEntity_UnderEntityHierarchies.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 ReparentEntity_UnderEntityHierarchies():
  7. import pyside_utils
  8. @pyside_utils.wrap_async
  9. async def run_test():
  10. """
  11. Tests reparenting of an entity under various entity hierarchies:
  12. 1. Reparents an entity under another entity
  13. 2. Reparents an entity under a large nested entity hierarchy
  14. 3. Validates Undo/Redo functionality for each use case
  15. """
  16. from editor_python_test_tools.editor_entity_utils import EditorEntity
  17. from editor_python_test_tools.wait_utils import PrefabWaiter
  18. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  19. import azlmbr.legacy.general as general
  20. import azlmbr.math as math
  21. async def reparent_entity_with_undo_redo(entity_to_reparent, new_parent):
  22. # Get id for original parent
  23. original_parent = EditorEntity(entity_to_reparent.get_parent_id())
  24. # Reparent to the new parent
  25. entity_to_reparent.set_parent_entity(new_parent.id)
  26. # Undo the reparent operation, and verify original parent is restored
  27. general.undo()
  28. PrefabWaiter.wait_for_propagation()
  29. original_parent_children_ids = original_parent.get_children_ids()
  30. new_parent_children_ids = new_parent.get_children_ids()
  31. assert entity_to_reparent.id in original_parent_children_ids, \
  32. "Undo failed: Failed to find entity as a child of the original parent."
  33. assert entity_to_reparent.id not in new_parent_children_ids, \
  34. "Undo failed: Unexpectedly still found entity as a child of the new parent."
  35. # Redo the reparent operation, and verify the new instance is not among the original parent's child entities
  36. general.redo()
  37. PrefabWaiter.wait_for_propagation()
  38. original_parent_children_ids = original_parent.get_children_ids()
  39. new_parent_children_ids = new_parent.get_children_ids()
  40. assert entity_to_reparent.id not in original_parent_children_ids, \
  41. "Redo failed: Unexpectedly found entity as a child of the original parent."
  42. assert entity_to_reparent.id in new_parent_children_ids, \
  43. "Redo failed: Failed to find entity as a child of the new parent."
  44. prefab_test_utils.open_base_tests_level()
  45. # Creates 2 new test entities at the root level
  46. test_entity = EditorEntity.create_editor_entity("TestEntity")
  47. single_parent_entity = EditorEntity.create_editor_entity("SingleParent")
  48. # Creates a large nested hierarchy of entities
  49. nested_hierarchy_pos = math.Vector3(0.0, 0.0, 0.0)
  50. nested_hierarchy_root_entity = prefab_test_utils.create_linear_nested_entities("NestedEntity", 50,
  51. nested_hierarchy_pos)
  52. # Reparents the test entity under the single parent entity
  53. await reparent_entity_with_undo_redo(test_entity, single_parent_entity)
  54. # Reparents the test entity under the nested hierarchy's root entity
  55. await reparent_entity_with_undo_redo(test_entity, nested_hierarchy_root_entity)
  56. # Find an entity within the large entity hierarchy, and reparent the test entity to it
  57. nested_entity = EditorEntity.find_editor_entity("NestedEntity33")
  58. await reparent_entity_with_undo_redo(test_entity, nested_entity)
  59. run_test()
  60. if __name__ == "__main__":
  61. from editor_python_test_tools.utils import Report
  62. Report.start_test(ReparentEntity_UnderEntityHierarchies)