CreatePrefab_UnderChildEntityOfAnotherPrefab.py 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_UnderChildEntityOfAnotherPrefab():
  7. """
  8. Test description:
  9. - Creates parent/child entities with the entities having a physx collider
  10. - Creates a prefab "Outer_prefab" and an instance based of the entities
  11. - Creates a prefab "Inner_prefab" inside "Outer_prefab" based the child entity contained inside of it
  12. Checks that the parent/child entity is correctly handled by the prefab system checking the name and that it contains the physx collider
  13. """
  14. from pathlib import Path
  15. from editor_python_test_tools.editor_entity_utils import EditorEntity
  16. from editor_python_test_tools.prefab_utils import Prefab
  17. from consts.physics import PHYSX_PRIMITIVE_COLLIDER as PHYSX_PRIMITIVE_COLLIDER_NAME
  18. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  19. OUTER_PREFAB_NAME = 'Outer_prefab'
  20. INNER_PREFAB_NAME = 'Inner_prefab'
  21. OUTER_PREFAB_FILE_NAME = Path(__file__).stem + OUTER_PREFAB_NAME
  22. INNER_PREFAB_FILE_NAME = Path(__file__).stem + INNER_PREFAB_NAME
  23. PARENT_ENTITY_NAME = 'ParentEntity'
  24. CHILD_ENTITY_NAME = 'ChildEntity'
  25. prefab_test_utils.open_base_tests_level()
  26. # Creates new parent/child Entities at the root level
  27. # Asserts if creation didn't succeed
  28. parent_entity = EditorEntity.create_editor_entity_at((100.0, 100.0, 100.0), name=PARENT_ENTITY_NAME)
  29. assert parent_entity.id.IsValid(), "Couldn't create parent entity"
  30. parent_entity.add_component(PHYSX_PRIMITIVE_COLLIDER_NAME)
  31. assert parent_entity.has_component(PHYSX_PRIMITIVE_COLLIDER_NAME), f"Failed to add a {PHYSX_PRIMITIVE_COLLIDER_NAME}"
  32. child_entity = EditorEntity.create_editor_entity(parent_id=parent_entity.id, name=CHILD_ENTITY_NAME)
  33. assert child_entity.id.IsValid(), "Couldn't create child entity"
  34. child_entity.add_component(PHYSX_PRIMITIVE_COLLIDER_NAME)
  35. assert child_entity.has_component(PHYSX_PRIMITIVE_COLLIDER_NAME), f"Failed to add a {PHYSX_PRIMITIVE_COLLIDER_NAME}"
  36. # Create a prefab based on that entity and focus it
  37. _, outer_instance = Prefab.create_prefab([parent_entity], OUTER_PREFAB_FILE_NAME)
  38. outer_instance.container_entity.focus_on_owning_prefab()
  39. # The entities should be now inside the outer prefab instance.
  40. parent_entity_on_outer_instance = outer_instance.get_direct_child_entities()[0]
  41. child_entity_on_outer_instance = parent_entity_on_outer_instance.get_children()[0]
  42. # We track if the parent entity the same one by checking the name and if it still contains the component that we created before
  43. assert parent_entity_on_outer_instance.get_name() == PARENT_ENTITY_NAME, \
  44. f"Entity name inside '{OUTER_PREFAB_NAME}' doesn't match the original name, original:'{PARENT_ENTITY_NAME}' " \
  45. f"current:'{parent_entity_on_outer_instance.get_name()}'"
  46. assert parent_entity_on_outer_instance.has_component(PHYSX_PRIMITIVE_COLLIDER_NAME), \
  47. "Entity inside outer_prefab doesn't have the collider component it should"
  48. # Now, create another prefab, based on the child entity that is inside outer_prefab
  49. _, inner_instance = Prefab.create_prefab([child_entity_on_outer_instance], INNER_PREFAB_FILE_NAME)
  50. # Test undo/redo on prefab creation
  51. prefab_test_utils.validate_undo_redo_on_prefab_creation(inner_instance, parent_entity_on_outer_instance.id)
  52. # The child entity should now be inside the inner prefab instance
  53. child_entity_on_inner_instance = inner_instance.get_direct_child_entities()[0]
  54. # We track if the child entity is the same entity by checking the name and if it still contains the component that we created before
  55. assert child_entity_on_inner_instance.get_name() == CHILD_ENTITY_NAME, \
  56. f"Entity name inside '{INNER_PREFAB_NAME}' doesn't match the original name, original:'{CHILD_ENTITY_NAME}' " \
  57. f"current:'{child_entity_on_inner_instance.get_name()}'"
  58. assert child_entity_on_inner_instance.has_component(PHYSX_PRIMITIVE_COLLIDER_NAME), \
  59. "Entity inside inner_prefab doesn't have the collider component it should"
  60. # Verify hierarchy of entities:
  61. # Outer_prefab
  62. # |- ParentEntity
  63. # | |- Inner_prefab
  64. # | | |- ChildEntity
  65. assert child_entity_on_inner_instance.get_parent_id() == inner_instance.container_entity.id
  66. assert inner_instance.container_entity.get_parent_id() == parent_entity_on_outer_instance.id
  67. assert parent_entity_on_outer_instance.get_parent_id() == outer_instance.container_entity.id
  68. if __name__ == "__main__":
  69. from editor_python_test_tools.utils import Report
  70. Report.start_test(CreatePrefab_UnderChildEntityOfAnotherPrefab)