CreatePrefab_ComponentConfigurationRetained.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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_ComponentConfigurationRetained():
  7. """
  8. Test description:
  9. - Creates an entity with components set to specific values.
  10. - Creates a prefab of the entity.
  11. Test is successful if the new instanced prefab retains the expected component values
  12. """
  13. from pathlib import Path
  14. import azlmbr.math as math
  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. TEST_PREFAB_FILE_NAME = Path(__file__).stem + '_prefab'
  19. TEST_ENTITY_NAME = "TestEntity"
  20. prefab_test_utils.open_base_tests_level()
  21. # Creates a new Entity at the root level
  22. # Asserts if creation didn't succeed
  23. test_entity = EditorEntity.create_editor_entity(TEST_ENTITY_NAME)
  24. original_parent_id = test_entity.get_parent_id()
  25. assert test_entity.id.IsValid(), "Couldn't create parent entity"
  26. # Add Box Shape component to the entity
  27. test_entity.add_component("Box Shape")
  28. # Set the Box Shape component's properties
  29. box_shape_dimensions = math.Vector3(16.0, 16.0, 16.0)
  30. test_entity.components[0].set_component_property_value("Box Shape|Box Configuration|Dimensions",
  31. box_shape_dimensions)
  32. # Asserts if prefab creation doesn't succeed
  33. test_prefab, test_instance = Prefab.create_prefab([test_entity], TEST_PREFAB_FILE_NAME)
  34. # Validate that the Box Shape component properties persist after prefab creation
  35. test_entity = test_instance.get_direct_child_entity_by_name(TEST_ENTITY_NAME)
  36. box_shape_component = test_entity.get_components_of_type(["Box Shape"])[0]
  37. prefab_box_shape_dimensions = box_shape_component.get_component_property_value(
  38. "Box Shape|Box Configuration|Dimensions")
  39. assert box_shape_dimensions == prefab_box_shape_dimensions, \
  40. f"Found unexpected values on the Box Shape component after prefab creation. Expected {box_shape_dimensions}, " \
  41. f"Found {prefab_box_shape_dimensions}."
  42. # Test undo/redo on prefab creation
  43. prefab_test_utils.validate_undo_redo_on_prefab_creation(test_instance, original_parent_id)
  44. if __name__ == "__main__":
  45. from editor_python_test_tools.utils import Report
  46. Report.start_test(CreatePrefab_ComponentConfigurationRetained)