InstantiatePrefab_WithNestedEntities.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_WithNestedEntities():
  7. """
  8. Test description:
  9. - Creates linear nested entities.
  10. - Creates a prefab from the nested entities.
  11. - Instantiates another copy of the prefab and validates the structure is intact.
  12. Test passes if the 3 entities inside the newly instanced prefab have correct hierarchy and positions.
  13. """
  14. from pathlib import Path
  15. import azlmbr.legacy.general as general
  16. import azlmbr.math as math
  17. from editor_python_test_tools.prefab_utils import Prefab
  18. from editor_python_test_tools.wait_utils import PrefabWaiter
  19. from editor_python_test_tools.editor_entity_utils import EditorEntity
  20. import Prefab.tests.PrefabTestUtils as prefab_test_utils
  21. NESTED_ENTITIES_PREFAB_FILE_NAME = Path(__file__).stem + 'nested_entities_prefab'
  22. NESTED_ENTITIES_NAME_PREFIX = 'Entity_'
  23. POSITION = math.Vector3(100.0, 100.0, 100.0)
  24. NUM_NESTED_ENTITIES_LEVELS = 3
  25. prefab_test_utils.open_base_tests_level()
  26. # Creates new nested entities at the root level
  27. # Asserts if creation didn't succeed
  28. nested_entities_root = prefab_test_utils.create_linear_nested_entities(NESTED_ENTITIES_NAME_PREFIX,
  29. NUM_NESTED_ENTITIES_LEVELS, POSITION)
  30. nested_entities_root_parent = EditorEntity(nested_entities_root.get_parent_id())
  31. prefab_test_utils.validate_linear_nested_entities(nested_entities_root, NUM_NESTED_ENTITIES_LEVELS, POSITION)
  32. # Asserts if prefab creation doesn't succeed
  33. nested_entities_prefab, nested_entities_prefab_instance = Prefab.create_prefab([nested_entities_root],
  34. NESTED_ENTITIES_PREFAB_FILE_NAME)
  35. nested_entities_root_on_instance = nested_entities_prefab_instance.get_direct_child_entities()[0]
  36. prefab_test_utils.validate_linear_nested_entities(nested_entities_root_on_instance, NUM_NESTED_ENTITIES_LEVELS,
  37. POSITION)
  38. # Instantiate another copy of the prefab, and validate its hierarchy and position
  39. nested_entities_prefab_instance_2 = nested_entities_prefab.instantiate(prefab_position=POSITION)
  40. nested_entities_root_on_instance_2 = nested_entities_prefab_instance_2.get_direct_child_entities()[0]
  41. prefab_test_utils.validate_linear_nested_entities(nested_entities_root_on_instance_2, NUM_NESTED_ENTITIES_LEVELS,
  42. POSITION)
  43. # Undo the instantiation
  44. general.undo()
  45. PrefabWaiter.wait_for_propagation()
  46. child_ids = nested_entities_root_parent.get_children_ids()
  47. assert nested_entities_prefab_instance_2.container_entity.id not in child_ids, \
  48. "Undo Failed: Instance was still found after undo."
  49. # Redo the instantiation
  50. general.redo()
  51. PrefabWaiter.wait_for_propagation()
  52. child_ids = nested_entities_root_parent.get_children_ids()
  53. assert nested_entities_prefab_instance_2.container_entity.id in child_ids, \
  54. "Redo Failed: Instance was not found after redo"
  55. prefab_test_utils.validate_linear_nested_entities(nested_entities_root_on_instance_2, NUM_NESTED_ENTITIES_LEVELS,
  56. POSITION)
  57. if __name__ == "__main__":
  58. from editor_python_test_tools.utils import Report
  59. Report.start_test(InstantiatePrefab_WithNestedEntities)