DuplicatePrefab_ContainingNestedEntitiesAndNestedPrefabs.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 DuplicatePrefab_ContainingNestedEntitiesAndNestedPrefabs():
  7. """
  8. Test description:
  9. - Creates linear nested entities.
  10. - Creates linear nested prefabs based of an entity with a physx collider.
  11. - Creates a prefab from the nested entities and the nested prefabs.
  12. - Duplicates the prefab.
  13. - Checks that the prefab is correctly duplicated.
  14. - Checks Undo/Redo operations.
  15. """
  16. from pathlib import Path
  17. import azlmbr.legacy.general as general
  18. from editor_python_test_tools.editor_entity_utils import EditorEntity
  19. from editor_python_test_tools.prefab_utils import Prefab
  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_PREFAB_FILE_NAME = Path(__file__).stem + '_' + 'nested_entities_prefab'
  24. NESTED_ENTITIES_NAME_PREFIX = 'Entity_'
  25. NESTED_PREFABS_FILE_NAME_PREFIX = Path(__file__).stem + '_' + 'nested_prefabs_'
  26. NESTED_PREFABS_NAME_PREFIX = 'NestedPrefabs_Prefab_'
  27. FILE_NAME_OF_PREFAB_WITH_NESTED_ENTITIES_AND_NESTED_PREFABS = Path(__file__).stem + '_' + 'new_prefab'
  28. NESTED_PREFABS_TEST_ENTITY_NAME = 'TestEntity'
  29. CREATION_POSITION = azlmbr.math.Vector3(100.0, 100.0, 100.0)
  30. NUM_NESTED_ENTITIES_LEVELS = 3
  31. NUM_NESTED_PREFABS_LEVELS = 3
  32. prefab_test_utils.open_base_tests_level()
  33. # Creates new nested entities at the root level
  34. # Asserts if creation didn't succeed
  35. nested_entities_root = prefab_test_utils.create_linear_nested_entities(
  36. NESTED_ENTITIES_NAME_PREFIX, NUM_NESTED_ENTITIES_LEVELS, CREATION_POSITION)
  37. prefab_test_utils.validate_linear_nested_entities(nested_entities_root, NUM_NESTED_ENTITIES_LEVELS,
  38. CREATION_POSITION)
  39. nested_entities_root_name = nested_entities_root.get_name()
  40. # Creates new nested prefabs at the root level
  41. # Asserts if creation didn't succeed
  42. entity_to_nest = EditorEntity.create_editor_entity_at(CREATION_POSITION, name=NESTED_PREFABS_TEST_ENTITY_NAME)
  43. assert entity_to_nest.id.IsValid(), "Couldn't create TestEntity"
  44. entity_to_nest.add_component(PHYSX_PRIMITIVE_COLLIDER_NAME)
  45. assert entity_to_nest.has_component(PHYSX_PRIMITIVE_COLLIDER_NAME), f"Failed to add a {PHYSX_PRIMITIVE_COLLIDER_NAME}"
  46. _, nested_prefab_instances = prefab_test_utils.create_linear_nested_prefabs(
  47. [entity_to_nest], NESTED_PREFABS_FILE_NAME_PREFIX, NESTED_PREFABS_NAME_PREFIX, NUM_NESTED_PREFABS_LEVELS)
  48. prefab_test_utils.validate_linear_nested_prefab_instances_hierarchy(nested_prefab_instances)
  49. # Creates a new prefab containing the nested entities and the nested prefab instances
  50. # Asserts if prefab creation doesn't succeed
  51. _, new_prefab = Prefab.create_prefab(
  52. [nested_entities_root, nested_prefab_instances[0].container_entity],
  53. FILE_NAME_OF_PREFAB_WITH_NESTED_ENTITIES_AND_NESTED_PREFABS)
  54. new_prefab_container_entity = new_prefab.container_entity
  55. nested_entities_root_on_instance = new_prefab.get_direct_child_entity_by_name(nested_entities_root_name)
  56. assert nested_entities_root_on_instance.get_name() == nested_entities_root_name \
  57. and nested_entities_root_on_instance.get_parent_id() == new_prefab_container_entity.id, \
  58. f"The name of the first child entity of the new prefab '{new_prefab_container_entity.get_name()}' " \
  59. f"should be '{nested_entities_root_name}', " \
  60. f"not '{nested_entities_root_on_instance.get_name()}'"
  61. prefab_test_utils.validate_linear_nested_entities(nested_entities_root_on_instance, NUM_NESTED_ENTITIES_LEVELS,
  62. CREATION_POSITION)
  63. # Gather information on prefab structure to validate against Undo/Redo
  64. common_parent = EditorEntity(new_prefab.container_entity.get_parent_id())
  65. common_parent_children_ids_before_duplicate = set([child_id.ToString() for child_id in
  66. common_parent.get_children_ids()])
  67. # Duplicates the prefab instance and asserts if duplication doesn't succeed
  68. duplicated_instance = Prefab.duplicate_prefabs([new_prefab])
  69. # Gather more information on prefab structure after duplication to validate against Undo/Redo
  70. common_parent_children_ids_after_duplicate = set([child_id.ToString() for child_id in
  71. common_parent.get_children_ids()])
  72. duplicate_container_entity_ids = [duplicated_instance[0].container_entity.id]
  73. # Test undo/redo on prefab duplication
  74. general.undo()
  75. PrefabWaiter.wait_for_propagation()
  76. common_parent_children_ids_after_duplicate_undo = set([child_id.ToString() for child_id in
  77. common_parent.get_children_ids()])
  78. assert common_parent_children_ids_before_duplicate == common_parent_children_ids_after_duplicate_undo, \
  79. "Undo Failed: Found unexpected children of common parent after Undo"
  80. general.redo()
  81. PrefabWaiter.wait_for_propagation()
  82. Prefab.validate_duplicated_prefab([new_prefab], common_parent_children_ids_before_duplicate,
  83. common_parent_children_ids_after_duplicate, duplicate_container_entity_ids,
  84. common_parent)
  85. if __name__ == "__main__":
  86. from editor_python_test_tools.utils import Report
  87. Report.start_test(DuplicatePrefab_ContainingNestedEntitiesAndNestedPrefabs)