InstantiatePrefab_FromCreatedPrefabWithSingleEntity.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_FromCreatedPrefabWithSingleEntity():
  7. from pathlib import Path
  8. import azlmbr.legacy.general as general
  9. from editor_python_test_tools.editor_entity_utils import EditorEntity
  10. from editor_python_test_tools.prefab_utils import Prefab
  11. from editor_python_test_tools.wait_utils import PrefabWaiter
  12. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  13. CAR_PREFAB_FILE_NAME = Path(__file__).stem + 'car_prefab'
  14. prefab_test_utils.open_base_tests_level()
  15. # Creates a new entity at the root level
  16. car_entity = EditorEntity.create_editor_entity()
  17. car_prefab_entities = [car_entity]
  18. # Creates a prefab from the new entity
  19. car_prefab, car_prefab_instance = Prefab.create_prefab(car_prefab_entities, CAR_PREFAB_FILE_NAME)
  20. # Instantiate another instance and verify
  21. car_prefab_instance_2 = car_prefab.instantiate()
  22. assert car_prefab_instance_2.is_valid(), "Failed to instantiate prefab"
  23. # Get parent entity and container id for verifying successful Undo/Redo operations
  24. instance_parent_id = EditorEntity(car_prefab_instance_2.container_entity.get_parent_id())
  25. instance = car_prefab_instance_2.container_entity
  26. # Undo the instantiation
  27. general.undo()
  28. PrefabWaiter.wait_for_propagation()
  29. child_ids = instance_parent_id.get_children_ids()
  30. assert instance.id not in child_ids, "Undo Failed: Instance was still found after undo."
  31. # Redo the instantiation
  32. general.redo()
  33. PrefabWaiter.wait_for_propagation()
  34. child_ids = instance_parent_id.get_children_ids()
  35. instance_children = instance.get_children()
  36. assert instance.id in child_ids, "Redo Failed: Instance was not found after redo"
  37. assert len(instance_children) == 1, \
  38. "Redo Failed: Did not find expected child entities in the prefab instance"
  39. if __name__ == "__main__":
  40. from editor_python_test_tools.utils import Report
  41. Report.start_test(InstantiatePrefab_FromCreatedPrefabWithSingleEntity)