3
0

EditEntity_UnderNestedInstance.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_UnderNestedInstance():
  7. """
  8. Test description:
  9. - Creates two motorcycle prefab instances where each motorcycle has two wheel instances with Mesh components.
  10. - Puts focus on the first motorcycle instance, changes the position of one of the wheels, and adds a Comment
  11. component.
  12. - Validates that after propagation, only one wheel of the focused motorcycle is changed.
  13. - Validates that after propagation, the wheel positions and expected components of the second motorcycle match those
  14. of the first.
  15. - Validates Undo/Redo of all the overrides.
  16. - Validates revert overrides of all the overrides.
  17. - Level
  18. -- Motorcycle (first instance) <-- focus
  19. --- Wheel (nested instance)
  20. ---- Wheel_Entity <-- change this entity's name to "Front_Wheel", edit its position, and add a "Comment" component.
  21. --- Wheel (nested instance)
  22. ---- Wheel_Entity <-- change this entity's name to "Back_Wheel"
  23. -- Motorcycle (second instance)
  24. --- Wheel (nested instance)
  25. ---- Wheel_Entity
  26. --- Wheel (nested instance)
  27. ---- Wheel_Entity
  28. """
  29. from editor_python_test_tools.editor_entity_utils import EditorEntity, EditorComponent
  30. from editor_python_test_tools.prefab_utils import Prefab
  31. from editor_python_test_tools.wait_utils import PrefabWaiter
  32. from pathlib import Path
  33. import azlmbr.legacy.general as general
  34. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  35. import azlmbr.bus as bus
  36. import azlmbr.editor as editor
  37. import azlmbr.globals as globals
  38. import editor_python_test_tools.hydra_editor_utils as hydra
  39. prefab_test_utils.open_base_tests_level()
  40. WHEEL_PREFAB_FILE_NAME = Path(__file__).stem + '_wheel_prefab'
  41. MOTORCYCLE_PREFAB_FILE_NAME = Path(__file__).stem + '_motorcycle_prefab'
  42. CREATION_POSITION = azlmbr.math.Vector3(0.0, 0.0, 0.0)
  43. UPDATED_POSITION = azlmbr.math.Vector3(10.0, 0.0, 0.0)
  44. # Create a wheel prefab/instance from a wheel entity
  45. wheel_entity = EditorEntity.create_editor_entity_at(CREATION_POSITION, "Wheel_Entity")
  46. wheel_prefab, wheel_instance_1 = Prefab.create_prefab([wheel_entity], WHEEL_PREFAB_FILE_NAME)
  47. wheel_entity = wheel_instance_1.get_direct_child_entities()[0]
  48. # Focus on the new instance and add a component, then focus back on the level prefab
  49. wheel_instance_1.container_entity.focus_on_owning_prefab()
  50. wheel_entity.add_component("Mesh")
  51. level_entity = EditorEntity(wheel_instance_1.container_entity.get_parent_id())
  52. level_entity.focus_on_owning_prefab()
  53. # Create a second wheel instance
  54. wheel_instance_2 = wheel_prefab.instantiate()
  55. # Create a motorcycle prefab with two wheel instances. Also creates first motorcycle instance
  56. motorcycle_prefab, motorcycle_instance_1 = Prefab.create_prefab(
  57. [wheel_instance_1.container_entity, wheel_instance_2.container_entity], MOTORCYCLE_PREFAB_FILE_NAME)
  58. # Create a second motorcycle instance
  59. motorcycle_instance_2 = motorcycle_prefab.instantiate()
  60. # Set focused prefab to first motorcycle instance
  61. motorcycle_instance_1.container_entity.focus_on_owning_prefab()
  62. # Change the names of the focused motorcycle's wheel entities
  63. wheel_instance_1_1 = motorcycle_instance_1.get_direct_child_entities()[0]
  64. front_wheel_entity = wheel_instance_1_1.get_children()[0]
  65. front_wheel_entity.set_name("Front_Wheel")
  66. wheel_instance_1_2 = motorcycle_instance_1.get_direct_child_entities()[1]
  67. back_wheel_entity = wheel_instance_1_2.get_children()[0]
  68. back_wheel_entity.set_name("Back_Wheel")
  69. # Change the position of the focused motorcycle's front wheel entity, and add another component as overrides.
  70. get_transform_component_outcome = editor.EditorComponentAPIBus(
  71. bus.Broadcast, "GetComponentOfType", front_wheel_entity.id, globals.property.EditorTransformComponentTypeId
  72. )
  73. entity_transform_component = EditorComponent(globals.property.EditorTransformComponentTypeId)
  74. entity_transform_component.id = get_transform_component_outcome.GetValue()
  75. hydra.set_component_property_value(entity_transform_component.id, "Values|Translate", UPDATED_POSITION)
  76. front_wheel_entity.add_component("Comment")
  77. PrefabWaiter.wait_for_propagation()
  78. # Validate that the front wheels of both motorcycle instances have
  79. # the updated position, and that the back wheels remain unchanged
  80. front_wheels = EditorEntity.find_editor_entities(["Front_Wheel"])
  81. assert len(front_wheels) == 2, "Expected to find a total of two front wheels."
  82. for front_wheel in front_wheels:
  83. front_wheel.validate_world_translate_position(UPDATED_POSITION)
  84. back_wheels = EditorEntity.find_editor_entities(["Back_Wheel"])
  85. assert len(back_wheels) == 2, "Expected to find a total of two back wheels."
  86. for back_wheel in back_wheels:
  87. back_wheel.validate_world_translate_position(CREATION_POSITION)
  88. # Validate the expected entities are flagged for overrides on each Motorcycle instance, and have the expected
  89. # components
  90. # Instance 1, which is already in focus
  91. prefab_test_utils.validate_expected_override_status(front_wheel_entity, True)
  92. prefab_test_utils.validate_expected_components(front_wheel_entity, expected_components=["Mesh", "Comment"])
  93. prefab_test_utils.validate_expected_override_status(back_wheel_entity, True)
  94. prefab_test_utils.validate_expected_components(back_wheel_entity, expected_components=["Mesh"],
  95. unexpected_components=["Comment"])
  96. # Focus instance 2, and validate
  97. motorcycle_instance_2.container_entity.focus_on_owning_prefab()
  98. front_wheel_entity_2 = motorcycle_instance_2.get_child_entity_by_name("Front_Wheel")
  99. prefab_test_utils.validate_expected_components(front_wheel_entity_2, expected_components=["Mesh", "Comment"])
  100. back_wheel_entity_2 = motorcycle_instance_2.get_child_entity_by_name("Back_Wheel")
  101. prefab_test_utils.validate_expected_components(back_wheel_entity_2, expected_components=["Mesh"],
  102. unexpected_components=["Comment"])
  103. prefab_test_utils.validate_expected_override_status(front_wheel_entity_2, True)
  104. prefab_test_utils.validate_expected_override_status(back_wheel_entity_2, True)
  105. # Undo the override
  106. general.undo() # Undo the focus change
  107. general.undo() # Undo the Comment component addition
  108. general.undo() # Undo the transform override
  109. PrefabWaiter.wait_for_propagation()
  110. # Validate the undo
  111. front_wheels = EditorEntity.find_editor_entities(["Front_Wheel"])
  112. for front_wheel in front_wheels:
  113. front_wheel.validate_world_translate_position(CREATION_POSITION)
  114. back_wheels = EditorEntity.find_editor_entities(["Back_Wheel"])
  115. for back_wheel in back_wheels:
  116. back_wheel.validate_world_translate_position(CREATION_POSITION)
  117. prefab_test_utils.validate_expected_components(front_wheel_entity, expected_components=["Mesh"],
  118. unexpected_components=["Comment"])
  119. prefab_test_utils.validate_expected_components(back_wheel_entity, expected_components=["Mesh"],
  120. unexpected_components=["Comment"])
  121. # Redo the override
  122. general.redo() # Redo the transform override
  123. general.redo() # Redo the Comment component addition
  124. PrefabWaiter.wait_for_propagation()
  125. # Validate the redo
  126. front_wheels = EditorEntity.find_editor_entities(["Front_Wheel"])
  127. for front_wheel in front_wheels:
  128. front_wheel.validate_world_translate_position(UPDATED_POSITION)
  129. back_wheels = EditorEntity.find_editor_entities(["Back_Wheel"])
  130. for back_wheel in back_wheels:
  131. back_wheel.validate_world_translate_position(CREATION_POSITION)
  132. # Revert all overrides on each entity
  133. front_wheel_entity.revert_overrides()
  134. back_wheel_entity.revert_overrides()
  135. PrefabWaiter.wait_for_propagation()
  136. # Validate the revert. All wheel entities should now have the initial name "Wheel_Entity" and be at the
  137. # initial position.
  138. wheels = EditorEntity.find_editor_entities(["Wheel_Entity"])
  139. assert len(wheels) == 4, f"Expected to find a total of four wheels. Found {len(wheels)}"
  140. for wheel in wheels:
  141. wheel.validate_world_translate_position(CREATION_POSITION)
  142. prefab_test_utils.validate_expected_override_status(wheel, False)
  143. if __name__ == "__main__":
  144. from editor_python_test_tools.utils import Report
  145. Report.start_test(EditEntity_UnderNestedInstance)