DetachPrefab_UnderAnotherPrefab.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_UnderAnotherPrefab():
  7. from pathlib import Path
  8. CAR_PREFAB_FILE_NAME = Path(__file__).stem + 'car_prefab'
  9. WHEEL_PREFAB_FILE_NAME = Path(__file__).stem + 'wheel_prefab'
  10. import pyside_utils
  11. @pyside_utils.wrap_async
  12. async def run_test():
  13. import azlmbr.bus as bus
  14. import azlmbr.editor as editor
  15. import azlmbr.entity as entity
  16. import azlmbr.legacy.general as general
  17. from editor_python_test_tools.editor_entity_utils import EditorEntity
  18. from editor_python_test_tools.prefab_utils import Prefab
  19. from editor_python_test_tools.wait_utils import PrefabWaiter
  20. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  21. def validate_entity_hierarchy(condition_checked):
  22. search_filter = entity.SearchFilter()
  23. search_filter.names = [WHEEL_PREFAB_FILE_NAME]
  24. wheel_prefab_root = EditorEntity(entity.SearchBus(bus.Broadcast, "SearchEntities", search_filter)[0])
  25. search_filter.names = ["Wheel"]
  26. wheel_entity = EditorEntity(entity.SearchBus(bus.Broadcast, "SearchEntities", search_filter)[0])
  27. car_prefab_children = car.get_direct_child_entities()
  28. car_prefab_children_ids = [child.id for child in car_prefab_children]
  29. wheel_prefab_root_children = wheel_prefab_root.get_children()
  30. wheel_prefab_root_children_ids = [child.id for child in wheel_prefab_root_children]
  31. assert wheel_entity.id in wheel_prefab_root_children_ids and \
  32. wheel_prefab_root.id in car_prefab_children_ids, \
  33. f"Entity hierarchy does not match expectations after {condition_checked}"
  34. prefab_test_utils.open_base_tests_level()
  35. # Creates a new car entity at the root level
  36. car_entity = EditorEntity.create_editor_entity("Car")
  37. car_prefab_entities = [car_entity]
  38. # Creates a prefab from the car entity
  39. _, car = Prefab.create_prefab(
  40. car_prefab_entities, CAR_PREFAB_FILE_NAME)
  41. # Creates another new wheel entity at the root level
  42. wheel_entity = EditorEntity.create_editor_entity("Wheel")
  43. wheel_prefab_entities = [wheel_entity]
  44. # Creates another prefab from the wheel entity
  45. _, wheel = Prefab.create_prefab(
  46. wheel_prefab_entities, WHEEL_PREFAB_FILE_NAME)
  47. # Ensure focus gets set on the prefab you want to parent under. This mirrors how users would do
  48. # reparenting in the editor.
  49. car.container_entity.focus_on_owning_prefab()
  50. # Reparents the wheel prefab instance to the container entity of the car prefab instance
  51. await wheel.ui_reparent_prefab_instance(car.container_entity.id)
  52. # Detaches the wheel prefab instance
  53. Prefab.detach_prefab(wheel)
  54. # Test undo/redo on prefab detach
  55. general.undo()
  56. PrefabWaiter.wait_for_propagation()
  57. is_prefab = editor.EditorComponentAPIBus(bus.Broadcast, "HasComponentOfType", wheel.container_entity.id,
  58. azlmbr.globals.property.EditorPrefabComponentTypeId)
  59. assert is_prefab, "Undo operation failed. Entity is not recognized as a prefab."
  60. # Verify entity hierarchy is the same after undoing prefab detach
  61. validate_entity_hierarchy("Undo")
  62. general.redo()
  63. PrefabWaiter.wait_for_propagation()
  64. is_prefab = editor.EditorComponentAPIBus(bus.Broadcast, "HasComponentOfType", wheel.container_entity.id,
  65. azlmbr.globals.property.EditorPrefabComponentTypeId)
  66. assert not is_prefab, "Redo operation failed. Entity is still recognized as a prefab."
  67. # Verify entity hierarchy is unchanged after redoing the prefab detach
  68. validate_entity_hierarchy("Redo")
  69. run_test()
  70. if __name__ == "__main__":
  71. from editor_python_test_tools.utils import Report
  72. Report.start_test(DetachPrefab_UnderAnotherPrefab)