CreatePrefab_WithNestedEntitiesAndNestedPrefabs.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 CreatePrefab_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. Test passed if the the nested entities/prefabs under the newly instanced prefab
  13. have correct hierarchy and positions.
  14. """
  15. from pathlib import Path
  16. from editor_python_test_tools.editor_entity_utils import EditorEntity
  17. from editor_python_test_tools.prefab_utils import Prefab
  18. from consts.physics import PHYSX_PRIMITIVE_COLLIDER as PHYSX_PRIMITIVE_COLLIDER_NAME
  19. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  20. NESTED_ENTITIES_PREFAB_FILE_NAME = Path(__file__).stem + '_' + 'nested_entities_prefab'
  21. NESTED_ENTITIES_NAME_PREFIX = 'Entity_'
  22. NESTED_PREFABS_FILE_NAME_PREFIX = Path(__file__).stem + '_' + 'nested_prefabs_'
  23. NESTED_PREFABS_NAME_PREFIX = 'NestedPrefabs_Prefab_'
  24. FILE_NAME_OF_PREFAB_WITH_NESTED_ENTITIES_AND_NESTED_PREFABS = Path(__file__).stem + '_' + 'new_prefab'
  25. NESTED_PREFABS_TEST_ENTITY_NAME = 'TestEntity'
  26. CREATION_POSITION = azlmbr.math.Vector3(100.0, 100.0, 100.0)
  27. NUM_NESTED_ENTITIES_LEVELS = 3
  28. NUM_NESTED_PREFABS_LEVELS = 3
  29. prefab_test_utils.open_base_tests_level()
  30. # Creates new nested entities at the root level
  31. # Asserts if creation didn't succeed
  32. nested_entities_root = prefab_test_utils.create_linear_nested_entities(
  33. NESTED_ENTITIES_NAME_PREFIX, NUM_NESTED_ENTITIES_LEVELS, CREATION_POSITION)
  34. nested_entities_root_parent = nested_entities_root.get_parent_id()
  35. prefab_test_utils.validate_linear_nested_entities(nested_entities_root, NUM_NESTED_ENTITIES_LEVELS, CREATION_POSITION)
  36. nested_entities_root_name = nested_entities_root.get_name()
  37. # Creates a new nested prefabs at the root level
  38. # Asserts if creation didn't succeed
  39. entity = EditorEntity.create_editor_entity_at(CREATION_POSITION, name=NESTED_PREFABS_TEST_ENTITY_NAME)
  40. assert entity.id.IsValid(), "Couldn't create TestEntity"
  41. entity.add_component(PHYSX_PRIMITIVE_COLLIDER_NAME)
  42. assert entity.has_component(PHYSX_PRIMITIVE_COLLIDER_NAME), f"Failed to add a {PHYSX_PRIMITIVE_COLLIDER_NAME}"
  43. _, nested_prefab_instances = prefab_test_utils.create_linear_nested_prefabs(
  44. [entity], NESTED_PREFABS_FILE_NAME_PREFIX, NESTED_PREFABS_NAME_PREFIX, NUM_NESTED_PREFABS_LEVELS)
  45. prefab_test_utils.validate_linear_nested_prefab_instances_hierarchy(nested_prefab_instances)
  46. # Asserts if prefab creation doesn't succeed
  47. _, new_prefab = Prefab.create_prefab(
  48. [nested_entities_root, nested_prefab_instances[0].container_entity], FILE_NAME_OF_PREFAB_WITH_NESTED_ENTITIES_AND_NESTED_PREFABS)
  49. new_prefab_container_entity = new_prefab.container_entity
  50. nested_entities_root_on_instance = new_prefab.get_direct_child_entities()[0]
  51. nested_prefabs_root_container_entity_on_instance = new_prefab.get_direct_child_entities()[1]
  52. if new_prefab.get_direct_child_entities()[0].get_name() != nested_entities_root_name:
  53. nested_entities_root_on_instance, nested_prefabs_root_container_entity_on_instance = \
  54. nested_prefabs_root_container_entity_on_instance, nested_entities_root_on_instance
  55. assert nested_entities_root_on_instance.get_name() == nested_entities_root_name \
  56. and nested_entities_root_on_instance.get_parent_id() == new_prefab_container_entity.id, \
  57. f"The name of the first child entity of the new prefab '{new_prefab_container_entity.get_name()}' " \
  58. f"should be '{nested_entities_root_name}', " \
  59. f"not '{nested_entities_root_on_instance.get_name()}'"
  60. prefab_test_utils.validate_linear_nested_entities(nested_entities_root_on_instance, NUM_NESTED_ENTITIES_LEVELS, CREATION_POSITION)
  61. # Test undo/redo on prefab creation
  62. prefab_test_utils.validate_undo_redo_on_prefab_creation(new_prefab, nested_entities_root_parent)
  63. parent_prefab_container_entity_on_instance = new_prefab_container_entity
  64. child_entity_on_inner_instance = nested_prefabs_root_container_entity_on_instance
  65. for current_level in range(0, NUM_NESTED_PREFABS_LEVELS):
  66. assert child_entity_on_inner_instance.get_name() == prefab_test_utils.get_linear_nested_items_name(NESTED_PREFABS_NAME_PREFIX, current_level), \
  67. f"The name of a prefab inside the nested prefabs should be " \
  68. f"'{prefab_test_utils.get_linear_nested_items_name(NESTED_PREFABS_NAME_PREFIX, current_level)}', " \
  69. f"not '{child_entity_on_inner_instance.get_name()}'"
  70. assert child_entity_on_inner_instance.get_parent_id() == parent_prefab_container_entity_on_instance.id, \
  71. f"Prefab '{child_entity_on_inner_instance.get_name()}' should be the direct inner prefab of " \
  72. f"prefab with id '{parent_prefab_container_entity_on_instance.id.ToString()}', " \
  73. f"not '{child_entity_on_inner_instance.get_parent_id()}'"
  74. parent_prefab_container_entity_on_instance = child_entity_on_inner_instance
  75. child_entity_on_inner_instance = child_entity_on_inner_instance.get_children()[0]
  76. assert child_entity_on_inner_instance.id.IsValid(), \
  77. f"Entity '{child_entity_on_inner_instance.get_name()}' is not valid"
  78. assert child_entity_on_inner_instance.get_name() == NESTED_PREFABS_TEST_ENTITY_NAME, \
  79. f"The name of the entity inside the nested prefabs should be '{NESTED_PREFABS_TEST_ENTITY_NAME}', " \
  80. f"not '{child_entity_on_inner_instance.get_name()}'"
  81. assert child_entity_on_inner_instance.has_component(PHYSX_PRIMITIVE_COLLIDER_NAME), \
  82. "Entity inside inner_prefab doesn't have the collider component it should"
  83. assert child_entity_on_inner_instance.get_parent_id() == parent_prefab_container_entity_on_instance.id, \
  84. f"Entity '{child_entity_on_inner_instance.get_name()}' should be under " \
  85. f"prefab with id '{parent_prefab_container_entity_on_instance.id.ToString()}', " \
  86. f"not '{child_entity_on_inner_instance.get_parent_id()}'"
  87. if __name__ == "__main__":
  88. from editor_python_test_tools.utils import Report
  89. Report.start_test(CreatePrefab_WithNestedEntitiesAndNestedPrefabs)