DuplicateEntity_WithNestedEntitiesAndNestedPrefabs.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 DuplicateEntity_WithNestedEntitiesAndNestedPrefabs():
  7. """
  8. Test description:
  9. - Creates a root entity.
  10. - Creates linear nested entities with the root entity as a parent.
  11. - Creates linear nested prefabs with the root entity as a parent.
  12. - Duplicates the entire hierarchy, and validates.
  13. - Validates Undo/Redo operations on the duplication.
  14. """
  15. from pathlib import Path
  16. import azlmbr.bus as bus
  17. import azlmbr.legacy.general as general
  18. import azlmbr.prefab as prefab
  19. from editor_python_test_tools.editor_entity_utils import EditorEntity
  20. from editor_python_test_tools.wait_utils import PrefabWaiter
  21. from consts.physics import PHYSX_PRIMITIVE_COLLIDER as PHYSX_PRIMITIVE_COLLIDER_NAME
  22. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  23. NESTED_ENTITIES_NAME_PREFIX = 'NestedEntity_'
  24. NESTED_PREFABS_FILE_NAME_PREFIX = Path(__file__).stem + '_' + 'nested_prefabs_'
  25. NESTED_PREFABS_NAME_PREFIX = 'NestedPrefabs_Prefab_'
  26. NESTED_PREFABS_TEST_ENTITY_NAME = 'TestEntity'
  27. PARENT_CREATION_POSITION = azlmbr.math.Vector3(0.0, 0.0, 0.0)
  28. CHILDREN_CREATION_POSITION = azlmbr.math.Vector3(100.0, 100.0, 100.0)
  29. NUM_NESTED_ENTITIES_LEVELS = 3
  30. NUM_NESTED_PREFABS_LEVELS = 3
  31. def validate_duplicated_hierarchy():
  32. root_entities = EditorEntity.find_editor_entities(["Parent"])
  33. assert len(root_entities) == 2, "Failed to find expected duplicated entity hierarchy root"
  34. for root_entity in root_entities:
  35. expected_children_names = [f"{NESTED_PREFABS_NAME_PREFIX}0", f"{NESTED_ENTITIES_NAME_PREFIX}0"]
  36. child_entities = root_entity.get_children()
  37. for child in child_entities:
  38. assert child.get_name() in expected_children_names, "Failed to find expected children of root entity"
  39. # Validate the duplicated child nested entity hierarchy
  40. if child.get_name() == f"{NESTED_ENTITIES_NAME_PREFIX}0":
  41. prefab_test_utils.validate_linear_nested_entities(child, NUM_NESTED_ENTITIES_LEVELS,
  42. CHILDREN_CREATION_POSITION)
  43. # Validate the duplicated child nested prefab hierarchy
  44. elif child.get_name() == f"{NESTED_PREFABS_NAME_PREFIX}0":
  45. first_child_prefab = child.get_children()[0]
  46. assert first_child_prefab.get_name() == f"{NESTED_PREFABS_NAME_PREFIX}1", \
  47. f"Found unexpected child after duplication. Expected {NESTED_PREFABS_NAME_PREFIX}1, " \
  48. f"found {first_child_prefab.get_name()}."
  49. second_child_prefab = first_child_prefab.get_children()[0]
  50. assert second_child_prefab.get_name() == f"{NESTED_PREFABS_NAME_PREFIX}2", \
  51. f"Found unexpected child after duplication. Expected {NESTED_PREFABS_NAME_PREFIX}2, " \
  52. f"found {second_child_prefab.get_name()}."
  53. prefab_test_utils.open_base_tests_level()
  54. # Creates a new entity to serve as the parent for the nested entity and nested prefab hierarchies
  55. parent_entity = EditorEntity.create_editor_entity_at(PARENT_CREATION_POSITION, "Parent")
  56. # Creates new nested entities as children of the parent entity
  57. # Asserts if creation didn't succeed
  58. nested_entities_root = prefab_test_utils.create_linear_nested_entities(
  59. NESTED_ENTITIES_NAME_PREFIX, NUM_NESTED_ENTITIES_LEVELS, CHILDREN_CREATION_POSITION, parent_id=parent_entity.id)
  60. prefab_test_utils.validate_linear_nested_entities(nested_entities_root, NUM_NESTED_ENTITIES_LEVELS,
  61. CHILDREN_CREATION_POSITION)
  62. # Creates new nested prefabs at the root level
  63. # Asserts if creation didn't succeed
  64. nested_prefab_root = EditorEntity.create_editor_entity_at(CHILDREN_CREATION_POSITION, name=NESTED_PREFABS_TEST_ENTITY_NAME,
  65. parent_id=parent_entity.id)
  66. assert nested_prefab_root.id.IsValid(), "Couldn't create TestEntity"
  67. nested_prefab_root.add_component(PHYSX_PRIMITIVE_COLLIDER_NAME)
  68. assert nested_prefab_root.has_component(PHYSX_PRIMITIVE_COLLIDER_NAME), f"Failed to add a {PHYSX_PRIMITIVE_COLLIDER_NAME}"
  69. nested_prefabs, nested_prefab_instances = prefab_test_utils.create_linear_nested_prefabs(
  70. [nested_prefab_root], NESTED_PREFABS_FILE_NAME_PREFIX, NESTED_PREFABS_NAME_PREFIX, NUM_NESTED_PREFABS_LEVELS)
  71. prefab_test_utils.validate_linear_nested_prefab_instances_hierarchy(nested_prefab_instances)
  72. # Duplicates the entity hierarchy and validates
  73. duplicate_outcome = prefab.PrefabPublicRequestBus(bus.Broadcast, 'DuplicateEntitiesInInstance',
  74. [parent_entity.id])
  75. assert duplicate_outcome.IsSuccess(), \
  76. f"Failed to duplicate nested entity hierarchy with root entity {parent_entity.get_name()}"
  77. PrefabWaiter.wait_for_propagation()
  78. validate_duplicated_hierarchy()
  79. # Test undo/redo on entity hierarchy duplication
  80. general.undo()
  81. PrefabWaiter.wait_for_propagation()
  82. root_entities = EditorEntity.find_editor_entities(["Parent"])
  83. assert len(root_entities) == 1, "Undo Failed: Found unexpected duplicated entity hierarchy root after Undo"
  84. prefab_test_utils.validate_linear_nested_entities(nested_entities_root, NUM_NESTED_ENTITIES_LEVELS,
  85. CHILDREN_CREATION_POSITION)
  86. prefab_test_utils.validate_linear_nested_prefab_instances_hierarchy(nested_prefab_instances)
  87. general.redo()
  88. PrefabWaiter.wait_for_propagation()
  89. validate_duplicated_hierarchy()
  90. if __name__ == "__main__":
  91. from editor_python_test_tools.utils import Report
  92. Report.start_test(DuplicateEntity_WithNestedEntitiesAndNestedPrefabs)