InstantiatePrefab_WithNestedEntitiesandNestedPrefabs.py 5.8 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 InstantiatePrefab_WithNestedEntitiesAndNestedPrefabs():
  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. - Instantiates another copy of the prefab.
  13. - Checks that the prefab is correctly instantiated.
  14. - Checks Undo/Redo operations.
  15. """
  16. from pathlib import Path
  17. import azlmbr.legacy.general as general
  18. import azlmbr.math as math
  19. from editor_python_test_tools.editor_entity_utils import EditorEntity
  20. from editor_python_test_tools.prefab_utils import Prefab
  21. from editor_python_test_tools.wait_utils import PrefabWaiter
  22. from consts.physics import PHYSX_PRIMITIVE_COLLIDER as PHYSX_PRIMITIVE_COLLIDER_NAME
  23. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  24. NESTED_ENTITIES_NAME_PREFIX = 'Entity_'
  25. NESTED_PREFABS_FILE_NAME_PREFIX = Path(__file__).stem + '_nested_prefab_'
  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 = 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(), f"Couldn't create {NESTED_PREFABS_TEST_ENTITY_NAME}"
  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, new_prefab_instance = 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_instance.container_entity
  55. nested_entities_root_on_instance = new_prefab_instance.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_instance.container_entity.get_parent_id())
  65. common_parent_children_ids_before_new_instance = set([child_id.ToString() for child_id in
  66. common_parent.get_children_ids()])
  67. # Instantiates another copy of the prefab
  68. new_prefab_instance_2 = Prefab.instantiate(new_prefab)
  69. assert len(new_prefab.instances) == 2, "Failed to instantiate another copy of the prefab"
  70. # Gather more information on new entity structure to validate against Undo/Redo
  71. common_parent_children_ids_after_new_instance = set([child_id.ToString() for child_id in
  72. common_parent.get_children_ids()])
  73. # Test undo/redo on new instantiation
  74. general.undo()
  75. PrefabWaiter.wait_for_propagation()
  76. common_parent_children_ids_after_instantiate_undo = set([child_id.ToString() for child_id in
  77. common_parent.get_children_ids()])
  78. assert common_parent_children_ids_before_new_instance == common_parent_children_ids_after_instantiate_undo, \
  79. "Undo Failed: Found unexpected children of common parent after Undo"
  80. general.redo()
  81. PrefabWaiter.wait_for_propagation()
  82. # Use duplicate prefab validation to validate the structure of new instance
  83. Prefab.validate_duplicated_prefab([new_prefab_instance], common_parent_children_ids_before_new_instance,
  84. common_parent_children_ids_after_new_instance,
  85. [new_prefab_instance_2.container_entity.id],
  86. common_parent)
  87. if __name__ == "__main__":
  88. from editor_python_test_tools.utils import Report
  89. Report.start_test(InstantiatePrefab_WithNestedEntitiesAndNestedPrefabs)