3
0

AddEntity_UnderUnfocusedInstanceAsOverride.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Undo_AddEntity():
  7. from editor_python_test_tools.wait_utils import PrefabWaiter
  8. # One of the undos is for entity selection.
  9. azlmbr.legacy.general.undo()
  10. PrefabWaiter.wait_for_propagation()
  11. azlmbr.legacy.general.undo()
  12. PrefabWaiter.wait_for_propagation()
  13. def Redo_AddEntity():
  14. from editor_python_test_tools.wait_utils import PrefabWaiter
  15. # One of the redos is for entity selection.
  16. azlmbr.legacy.general.redo()
  17. PrefabWaiter.wait_for_propagation()
  18. azlmbr.legacy.general.redo()
  19. PrefabWaiter.wait_for_propagation()
  20. def AddEntity_UnderUnfocusedInstanceAsOverride():
  21. """
  22. Test description:
  23. - Create a car prefab/instance with the following hierarchy, and focus on the car:
  24. CAR (car instance, focused)
  25. |- AXLE (axle instance)
  26. |- LeftWheel (wheel instance)
  27. |- WheelEntity
  28. |- RightWheel (wheel instance)
  29. |- WheelEntity
  30. - Create a new entity 'TireEntity' and add it under 'LeftWheel' as override of car instance.
  31. - Undo/Redo adding 'TireEntity' under 'LeftWheel'.
  32. - Check that 'TireEntity' is correctly added without accidentally modifying another wheel instance 'RightWheel' during prefab propagations.
  33. """
  34. from editor_python_test_tools.editor_entity_utils import EditorEntity
  35. from editor_python_test_tools.prefab_utils import Prefab
  36. from editor_python_test_tools.wait_utils import PrefabWaiter
  37. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  38. prefab_test_utils.open_base_tests_level()
  39. from pathlib import Path
  40. WHEEL_PREFAB_FILE_NAME = Path(__file__).stem + '_' + 'wheel_prefab'
  41. AXLE_PREFAB_FILE_NAME = Path(__file__).stem + '_' + 'axle_prefab'
  42. CAR_PREFAB_FILE_NAME = Path(__file__).stem + '_' + 'car_prefab'
  43. # Create wheel/axle/car prefabs and instances, and then focus on car instance.
  44. LEFT_WHEEL_INSTANCE_NAME = "LeftWheel"
  45. RIGHT_WHEEL_INSTANCE_NAME = "RightWheel"
  46. WHEEL_ENTITY_NAME = "WheelEntity"
  47. wheel_entity = EditorEntity.create_editor_entity(name=WHEEL_ENTITY_NAME)
  48. assert wheel_entity.id.IsValid(), f"Couldn't create entity '{WHEEL_ENTITY_NAME}'"
  49. wheel_prefab, left_wheel_instance = Prefab.create_prefab([wheel_entity], WHEEL_PREFAB_FILE_NAME, prefab_instance_name=LEFT_WHEEL_INSTANCE_NAME)
  50. right_wheel_instance = wheel_prefab.instantiate(name=RIGHT_WHEEL_INSTANCE_NAME)
  51. AXLE_INSTANCE_NAME = "AXLE"
  52. _, axle_instance = Prefab.create_prefab(
  53. [left_wheel_instance.container_entity, right_wheel_instance.container_entity], AXLE_PREFAB_FILE_NAME, prefab_instance_name=AXLE_INSTANCE_NAME)
  54. CAR_INSTANCE_NAME = "CAR"
  55. _, car_instance = Prefab.create_prefab([axle_instance.container_entity], CAR_PREFAB_FILE_NAME, prefab_instance_name=CAR_INSTANCE_NAME)
  56. car_instance.container_entity.focus_on_owning_prefab()
  57. # Find the container entity of 'LeftWheel', creates a new entity 'TireEntity', and adds the new entity under 'LeftWheel'.
  58. left_wheel_instance_container_entity = EditorEntity.find_editor_entity(entity_name=LEFT_WHEEL_INSTANCE_NAME, must_be_unique=True)
  59. assert left_wheel_instance_container_entity.id.IsValid(), f"Couldn't find valid entity '{LEFT_WHEEL_INSTANCE_NAME}'"
  60. TIRE_ENTITY_NAME = "TireEntity"
  61. tire_entity = EditorEntity.create_editor_entity(parent_id=left_wheel_instance_container_entity.id, name=TIRE_ENTITY_NAME)
  62. # Wait till prefab propagation finishes before validation.
  63. PrefabWaiter.wait_for_propagation()
  64. # Check if 'TireEntity' is added under 'LeftWheel' only correctly.
  65. assert tire_entity.id.IsValid(), f"Couldn't create entity '{TIRE_ENTITY_NAME}'' under prefab instance '{LEFT_WHEEL_INSTANCE_NAME}'"
  66. assert tire_entity.get_name() == TIRE_ENTITY_NAME, f"Entity '{tire_entity.get_name()}''s name should be {TIRE_ENTITY_NAME}"
  67. assert tire_entity.get_parent_id() == left_wheel_instance_container_entity.id, f"Entity '{LEFT_WHEEL_INSTANCE_NAME}' should be the parent of entity '{TIRE_ENTITY_NAME}'"
  68. prefab_test_utils.validate_expected_override_status(tire_entity, True)
  69. child_entity_ids = left_wheel_instance_container_entity.get_children()
  70. assert len(child_entity_ids) == 2, f"{len(child_entity_ids)} child entities found under entity '{LEFT_WHEEL_INSTANCE_NAME}'" \
  71. f" after add-entity operation, when there should have been 2 child entities"
  72. right_wheel_instance_container_entity = EditorEntity.find_editor_entity(entity_name=RIGHT_WHEEL_INSTANCE_NAME, must_be_unique=True)
  73. assert right_wheel_instance_container_entity.id.IsValid(), f"Couldn't find valid entity '{RIGHT_WHEEL_INSTANCE_NAME}'"
  74. child_entity_ids = right_wheel_instance_container_entity.get_children()
  75. assert len(child_entity_ids) == 1, f"{len(child_entity_ids)} child entities found under entity '{RIGHT_WHEEL_INSTANCE_NAME}'" \
  76. f" after add-entity operation, when there should have been 1 child entity"
  77. # Test undo/redo on adding 'TireEntity' under 'LeftWheel'.
  78. Undo_AddEntity()
  79. left_wheel_instance_container_entity = EditorEntity.find_editor_entity(entity_name=LEFT_WHEEL_INSTANCE_NAME, must_be_unique=True)
  80. assert left_wheel_instance_container_entity.id.IsValid(), f"Couldn't find valid entity '{LEFT_WHEEL_INSTANCE_NAME}'"
  81. child_entity_ids = left_wheel_instance_container_entity.get_children()
  82. assert len(child_entity_ids) == 1, f"{len(child_entity_ids)} child entities found under entity '{LEFT_WHEEL_INSTANCE_NAME}'" \
  83. f" after Undo operation, when there should have been 1 child entity"
  84. tire_entities = EditorEntity.find_editor_entities(entity_names=[TIRE_ENTITY_NAME])
  85. assert len(tire_entities) == 0, f"{len(tire_entities)} '{TIRE_ENTITY_NAME}' entities exist" \
  86. f" after Undo operation, when there shouldn't have been any"
  87. Redo_AddEntity()
  88. left_wheel_instance_container_entity = EditorEntity.find_editor_entity(entity_name=LEFT_WHEEL_INSTANCE_NAME, must_be_unique=True)
  89. assert left_wheel_instance_container_entity.id.IsValid(), f"Couldn't find valid entity '{LEFT_WHEEL_INSTANCE_NAME}'"
  90. tire_entity = EditorEntity.find_editor_entity(entity_name=TIRE_ENTITY_NAME, must_be_unique=True)
  91. assert tire_entity.id.IsValid(), f"Couldn't find valid entity '{TIRE_ENTITY_NAME}'"
  92. assert tire_entity.get_parent_id() == left_wheel_instance_container_entity.id, f"Entity '{LEFT_WHEEL_INSTANCE_NAME}' should be the parent of entity '{TIRE_ENTITY_NAME}'"
  93. child_entity_ids = left_wheel_instance_container_entity.get_children()
  94. assert len(child_entity_ids) == 2, f"{len(child_entity_ids)} child entities found under entity '{LEFT_WHEEL_INSTANCE_NAME}'" \
  95. f" after Redo operation, when there should have been 2 child entity"
  96. right_wheel_instance_container_entity = EditorEntity.find_editor_entity(entity_name=RIGHT_WHEEL_INSTANCE_NAME, must_be_unique=True)
  97. assert right_wheel_instance_container_entity.id.IsValid(), f"Couldn't find valid entity '{RIGHT_WHEEL_INSTANCE_NAME}'"
  98. child_entity_ids = right_wheel_instance_container_entity.get_children()
  99. assert len(child_entity_ids) == 1, f"{len(child_entity_ids)} child entities found under entity '{RIGHT_WHEEL_INSTANCE_NAME}'" \
  100. f" after Redo operation, when there should have been 1 child entity"
  101. # Revert the re-applied overrides
  102. tire_entity.revert_overrides()
  103. PrefabWaiter.wait_for_propagation()
  104. # Validate the revert
  105. tire_entities = EditorEntity.find_editor_entities(entity_names=[TIRE_ENTITY_NAME])
  106. assert len(tire_entities) == 0, f"Expected 0 '{TIRE_ENTITY_NAME}' entities after Revert Overrides operation, but " \
  107. f"found {len(tire_entities)}."
  108. if __name__ == "__main__":
  109. from editor_python_test_tools.utils import Report
  110. Report.start_test(AddEntity_UnderUnfocusedInstanceAsOverride)