CreatePrefab_UnderAnotherPrefab.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_UnderAnotherPrefab():
  7. """
  8. Test description:
  9. - Creates an entity with a physx collider
  10. - Creates a prefab "Outer_prefab" and an instance based of that entity
  11. - Creates a prefab "Inner_prefab" inside "Outer_prefab" based the entity contained inside of it
  12. Checks that the 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
  18. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  19. OUTER_PREFAB_FILE_NAME = Path(__file__).stem + 'Outer_prefab'
  20. INNER_PREFAB_FILE_NAME = Path(__file__).stem + 'Inner_prefab'
  21. prefab_test_utils.open_base_tests_level()
  22. # Creates a new Entity at the root level
  23. # Asserts if creation didn't succeed
  24. entity = EditorEntity.create_editor_entity_at((100.0, 100.0, 100.0), name="TestEntity")
  25. assert entity.id.IsValid(), "Couldn't create entity"
  26. entity.add_component(PHYSX_PRIMITIVE_COLLIDER)
  27. assert entity.has_component(PHYSX_PRIMITIVE_COLLIDER), "Failed to add a PhysX Primitive Collider"
  28. # Create a prefab based on that entity and focus it
  29. outer_prefab, outer_instance = Prefab.create_prefab([entity], OUTER_PREFAB_FILE_NAME)
  30. outer_instance.container_entity.focus_on_owning_prefab()
  31. # The test should be now inside the outer prefab instance.
  32. entity = outer_instance.get_direct_child_entities()[0]
  33. # We track if that is the same entity by checking the name and if it still contains the component that we created before
  34. assert entity.get_name() == "TestEntity", f"Entity name inside outer_prefab doesn't match the original name, original:'TestEntity' current:'{entity.get_name()}'"
  35. assert entity.has_component(PHYSX_PRIMITIVE_COLLIDER), "Entity name inside outer_prefab doesn't have the collider component it should"
  36. # Now, create another prefab, based on the entity that is inside outer_prefab
  37. inner_prefab, inner_instance = Prefab.create_prefab([entity], INNER_PREFAB_FILE_NAME)
  38. # Test undo/redo on prefab creation
  39. prefab_test_utils.validate_undo_redo_on_prefab_creation(inner_instance, outer_instance.container_entity.id)
  40. # The test entity should now be inside the inner prefab instance
  41. entity = inner_instance.get_direct_child_entities()[0]
  42. # We track if that is the same entity by checking the name and if it still contains the component that we created before
  43. assert entity.get_name() == "TestEntity", f"Entity name inside inner_prefab doesn't match the original name, original:'TestEntity' current:'{entity.get_name()}'"
  44. assert entity.has_component(PHYSX_PRIMITIVE_COLLIDER), "Entity name inside inner_prefab doesn't have the collider component it should"
  45. # Verify hierarchy of entities:
  46. # Outer_prefab
  47. # |- Inner_prefab
  48. # | |- TestEntity
  49. assert entity.get_parent_id() == inner_instance.container_entity.id
  50. assert inner_instance.container_entity.get_parent_id() == outer_instance.container_entity.id
  51. if __name__ == "__main__":
  52. from editor_python_test_tools.utils import Report
  53. Report.start_test(CreatePrefab_UnderAnotherPrefab)