3
0

EditEntity_UnderImmediateInstance.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 EditEntity_UnderImmediateInstance():
  7. """
  8. Test description:
  9. - Creates a test prefab in the level, and instantiates it twice
  10. - Edits the transform as an override on only the entity under the 2nd instance
  11. - Validates overrides/undo/redo/revert overrides all function properly
  12. - Adds a new component to the test prefab
  13. - Adds another component as an override only the entity under the 2nd instance
  14. - Validates overrides/undo/redo/revert overrides all function properly
  15. - Removes a component as an override only from the entity under the 1st instance
  16. - Validates overrides/undo/redo/revert overrides all function properly
  17. """
  18. from editor_python_test_tools.editor_entity_utils import EditorEntity, EditorComponent
  19. from editor_python_test_tools.prefab_utils import Prefab
  20. from editor_python_test_tools.wait_utils import PrefabWaiter
  21. import azlmbr.legacy.general as general
  22. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  23. import azlmbr.bus as bus
  24. import azlmbr.editor as editor
  25. import azlmbr.globals as globals
  26. import editor_python_test_tools.hydra_editor_utils as hydra
  27. CAR_PREFAB_FILE_NAME = Path(__file__).stem + 'car_prefab'
  28. CREATION_POSITION = azlmbr.math.Vector3(0.0, 0.0, 0.0)
  29. UPDATED_POSITION = azlmbr.math.Vector3(10.0, 0.0, 0.0)
  30. prefab_test_utils.open_base_tests_level()
  31. # Create a new entity at the root level
  32. car_entity = EditorEntity.create_editor_entity_at(CREATION_POSITION, "Car_Entity")
  33. car_prefab_entities = [car_entity]
  34. # Create a prefab from the new entity. Also creates first instance
  35. car_prefab, car_instance_1 = Prefab.create_prefab(car_prefab_entities, CAR_PREFAB_FILE_NAME)
  36. # Create a second instance
  37. car_instance_2 = car_prefab.instantiate()
  38. assert car_instance_2.is_valid(), "Failed to instantiate prefab"
  39. # Edit the second instance by changing the transform of the entity it owns
  40. car_entity_of_instance_2 = car_instance_2.get_direct_child_entities()[0]
  41. # Move the car entity to a new position.
  42. get_transform_component_outcome = editor.EditorComponentAPIBus(
  43. bus.Broadcast, "GetComponentOfType", car_entity_of_instance_2.id, globals.property.EditorTransformComponentTypeId
  44. )
  45. car_transform_component = EditorComponent(globals.property.EditorTransformComponentTypeId)
  46. car_transform_component.id = get_transform_component_outcome.GetValue()
  47. hydra.set_component_property_value(car_transform_component.id, "Values|Translate", UPDATED_POSITION)
  48. PrefabWaiter.wait_for_propagation()
  49. # Validate that only the second prefab instance has changed
  50. car_entity_of_instance_1 = car_instance_1.get_direct_child_entities()[0]
  51. car_entity_of_instance_1.validate_world_translate_position(CREATION_POSITION)
  52. car_entity_of_instance_2.validate_world_translate_position(UPDATED_POSITION)
  53. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, False)
  54. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, True)
  55. # Undo the override
  56. general.undo()
  57. PrefabWaiter.wait_for_propagation()
  58. # Validate the undo
  59. car_entity_of_instance_1.validate_world_translate_position(CREATION_POSITION)
  60. car_entity_of_instance_2.validate_world_translate_position(CREATION_POSITION)
  61. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, False)
  62. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, False)
  63. # Redo the override
  64. general.redo()
  65. PrefabWaiter.wait_for_propagation()
  66. # Validate the redo
  67. car_entity_of_instance_1.validate_world_translate_position(CREATION_POSITION)
  68. car_entity_of_instance_2.validate_world_translate_position(UPDATED_POSITION)
  69. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, False)
  70. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, True)
  71. # Revert the override, and validate
  72. car_entity_of_instance_2.revert_overrides()
  73. PrefabWaiter.wait_for_propagation()
  74. car_entity_of_instance_1.validate_world_translate_position(CREATION_POSITION)
  75. car_entity_of_instance_2.validate_world_translate_position(CREATION_POSITION)
  76. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, False)
  77. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, False)
  78. # Focus on the owning prefab and add a new component to both instances
  79. car_instance_1.container_entity.focus_on_owning_prefab()
  80. car_entity_of_instance_1.add_component("Mesh")
  81. # Focus on the level prefab, then add a different component as an override to entity in the second instance
  82. level_entity = EditorEntity(car_instance_1.container_entity.get_parent_id())
  83. level_entity.focus_on_owning_prefab()
  84. car_entity_of_instance_2.add_component("Comment")
  85. # Validate each instance has the expected components: Mesh on both, and Comment only on the 2nd instance
  86. prefab_test_utils.validate_expected_components(car_entity_of_instance_1, expected_components=["Mesh"],
  87. unexpected_components=["Comment"])
  88. prefab_test_utils.validate_expected_components(car_entity_of_instance_2, expected_components=["Mesh", "Comment"])
  89. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, False)
  90. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, True)
  91. # Undo each override
  92. general.undo() # Undo Comment component add
  93. general.undo() # Undo focus on level instance
  94. general.undo() # Undo Mesh component add
  95. PrefabWaiter.wait_for_propagation()
  96. # Validate each instance has reverted the addition of the new components with Undo
  97. prefab_test_utils.validate_expected_components(car_entity_of_instance_1, unexpected_components=["Mesh", "Comment"])
  98. prefab_test_utils.validate_expected_components(car_entity_of_instance_2, unexpected_components=["Mesh", "Comment"])
  99. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, False)
  100. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, False)
  101. # Redo each override
  102. general.redo() # Redo Mesh component add
  103. general.redo() # Redo focus on level instance
  104. general.redo() # Redo Comment component add
  105. PrefabWaiter.wait_for_propagation()
  106. # Validate each instance has the expected components: Mesh on both, and Comment only on the 2nd instance
  107. prefab_test_utils.validate_expected_components(car_entity_of_instance_1, expected_components=["Mesh"],
  108. unexpected_components=["Comment"])
  109. prefab_test_utils.validate_expected_components(car_entity_of_instance_2, expected_components=["Mesh", "Comment"])
  110. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, False)
  111. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, True)
  112. # Focus on the level prefab so overrides can be applied, then remove the Mesh component from the first instance
  113. level_entity.focus_on_owning_prefab()
  114. car_entity_of_instance_1.remove_component("Mesh")
  115. PrefabWaiter.wait_for_propagation()
  116. # Validate both instance entities now have overrides applied, and each has the proper components
  117. prefab_test_utils.validate_expected_components(car_entity_of_instance_1, unexpected_components=["Mesh", "Comment"])
  118. prefab_test_utils.validate_expected_components(car_entity_of_instance_2, expected_components=["Mesh", "Comment"])
  119. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, True)
  120. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, True)
  121. # Undo the Mesh component removal override
  122. general.undo()
  123. PrefabWaiter.wait_for_propagation()
  124. # Validate expected overrides and components after Undo
  125. prefab_test_utils.validate_expected_components(car_entity_of_instance_1, expected_components=["Mesh"],
  126. unexpected_components=["Comment"])
  127. prefab_test_utils.validate_expected_components(car_entity_of_instance_2, expected_components=["Mesh", "Comment"])
  128. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, False)
  129. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, True)
  130. # Redo the Mesh component removal override
  131. general.redo()
  132. PrefabWaiter.wait_for_propagation()
  133. # Validate expected overrides and components after Redo
  134. prefab_test_utils.validate_expected_components(car_entity_of_instance_1, unexpected_components=["Mesh", "Comment"])
  135. prefab_test_utils.validate_expected_components(car_entity_of_instance_2, expected_components=["Mesh", "Comment"])
  136. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, True)
  137. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, True)
  138. # Revert overrides on both entities
  139. car_entity_of_instance_1.revert_overrides()
  140. car_entity_of_instance_2.revert_overrides()
  141. PrefabWaiter.wait_for_propagation()
  142. # Validate expected overrides and component after reverting
  143. prefab_test_utils.validate_expected_components(car_entity_of_instance_1, expected_components=["Mesh"],
  144. unexpected_components=["Comment"])
  145. prefab_test_utils.validate_expected_components(car_entity_of_instance_2, expected_components=["Mesh"],
  146. unexpected_components=["Comment"])
  147. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_1, False)
  148. prefab_test_utils.validate_expected_override_status(car_entity_of_instance_2, False)
  149. if __name__ == "__main__":
  150. from editor_python_test_tools.utils import Report
  151. Report.start_test(EditEntity_UnderImmediateInstance)