CreatePrefab_UnderAnEntity.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_UnderAnEntity():
  7. """
  8. Test description:
  9. - Creates two entities, parent and child. Child entity has Parent entity as its parent.
  10. - Creates a prefab of the child entity.
  11. Test is successful if the new instanced prefab of the child has the parent entity id
  12. """
  13. from pathlib import Path
  14. CAR_PREFAB_FILE_NAME = Path(__file__).stem + 'car_prefab'
  15. from editor_python_test_tools.editor_entity_utils import EditorEntity
  16. from editor_python_test_tools.prefab_utils import Prefab
  17. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  18. prefab_test_utils.open_base_tests_level()
  19. # Creates a new Entity at the root level
  20. # Asserts if creation didn't succeed
  21. parent_entity = EditorEntity.create_editor_entity_at((100.0, 100.0, 100.0))
  22. assert parent_entity.id.IsValid(), "Couldn't create parent entity"
  23. child_entity = EditorEntity.create_editor_entity(parent_id=parent_entity.id)
  24. assert child_entity.id.IsValid(), "Couldn't create child entity"
  25. assert child_entity.get_world_translation().IsClose(parent_entity.get_world_translation()), f"Child entity position{child_entity.get_world_translation().ToString()}" \
  26. f" is not located at the same position as the parent{parent_entity.get_world_translation().ToString()}"
  27. # Asserts if prefab creation doesn't succeed
  28. child_prefab, child_instance = Prefab.create_prefab([child_entity], CAR_PREFAB_FILE_NAME)
  29. # Test undo/redo on prefab creation
  30. prefab_test_utils.validate_undo_redo_on_prefab_creation(child_instance, parent_entity.id)
  31. child_entity_on_child_instance = child_instance.get_direct_child_entities()[0]
  32. assert child_instance.container_entity.get_parent_id().IsValid(), "Newly instanced entity has no parent"
  33. assert child_instance.container_entity.get_parent_id() == parent_entity.id, "Newly instanced entity parent does not match the expected parent"
  34. assert child_instance.container_entity.get_world_translation().IsClose(parent_entity.get_world_translation()), "Newly instanced entity position is not located at the same position as the parent"
  35. # Move the parent position, it should update the child position
  36. parent_entity.set_world_translation((200.0, 200.0, 200.0))
  37. child_instance_translation = child_instance.container_entity.get_world_translation()
  38. assert child_instance_translation.IsClose(azlmbr.math.Vector3(200.0, 200.0, 200.0)), f"Instance position position{child_instance_translation.ToString()} didn't get updated" \
  39. f" to the same position as the parent{parent_entity.get_world_translation().ToString()}"
  40. child_translation = child_entity_on_child_instance.get_world_translation()
  41. assert child_translation.IsClose(azlmbr.math.Vector3(200.0, 200.0, 200.0)), f"Entity position{child_translation.ToString()} of the instance didn't get updated" \
  42. f" to the same position as the parent{parent_entity.get_world_translation().ToString()}"
  43. if __name__ == "__main__":
  44. from editor_python_test_tools.utils import Report
  45. Report.start_test(CreatePrefab_UnderAnEntity)