@ -7,10 +7,14 @@ SPDX-License-Identifier: Apache-2.0 OR MIT
# fmt:off
# fmt:off
class Tests ( ) :
class Tests ( ) :
create_new_entity = ( " Entity: ' CreateNewEntity ' passed " , " Entity: ' CreateNewEntity ' failed " )
create_new_entity = ( " ' CreateNewEntity ' passed " , " ' CreateNewEntity ' failed " )
create_prefab = ( " Prefab: ' CreatePrefab ' passed " , " Prefab: ' CreatePrefab ' failed " )
create_prefab = ( " ' CreatePrefab ' passed " , " ' CreatePrefab ' failed " )
instantiate_prefab = ( " Prefab: ' InstantiatePrefab ' passed " , " Prefab: ' InstantiatePrefab ' failed " )
instantiate_prefab = ( " ' InstantiatePrefab ' passed " , " ' InstantiatePrefab ' failed " )
new_prefab_position = ( " Prefab: new prefab ' s position is at the expected position " , " Prefab: new prefab ' s position is *not* at the expected position " )
has_one_child = ( " instantiated prefab contains only one child as expected " , " instantiated prefab does *not* contain only one child as expected " )
instantiated_prefab_position = ( " instantiated prefab ' s position is at the expected position " , " instantiated prefab ' s position is *not* at the expected position " )
delete_prefab = ( " ' DeleteEntitiesAndAllDescendantsInInstance ' passed " , " ' DeleteEntitiesAndAllDescendantsInInstance ' failed " )
instantiated_prefab_removed = ( " instantiated prefab ' s container entity has been removed " , " instantiated prefab ' s container entity has *not* been removed " )
instantiated_child_removed = ( " instantiated prefab ' s child entity has been removed " , " instantiated prefab ' s child entity has *not* been removed " )
# fmt:on
# fmt:on
def PrefabLevel_BasicWorkflow ( ) :
def PrefabLevel_BasicWorkflow ( ) :
@ -18,6 +22,7 @@ def PrefabLevel_BasicWorkflow():
This test will help verify if the following functions related to Prefab work as expected :
This test will help verify if the following functions related to Prefab work as expected :
- CreatePrefab
- CreatePrefab
- InstantiatePrefab
- InstantiatePrefab
- DeleteEntitiesAndAllDescendantsInInstance
"""
"""
import os
import os
@ -35,31 +40,68 @@ def PrefabLevel_BasicWorkflow():
from azlmbr . math import Vector3
from azlmbr . math import Vector3
import azlmbr . legacy . general as general
import azlmbr . legacy . general as general
EXPECTED_NEW_PREFAB_POSITION = Vector3 ( 10.00 , 20.0 , 30.0 )
NEW_PREFAB_NAME = " new_prefab "
NEW_PREFAB_FILE_NAME = NEW_PREFAB_NAME + " .prefab "
NEW_PREFAB_FILE_PATH = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , NEW_PREFAB_FILE_NAME )
INSTANTIATED_PREFAB_POSITION = Vector3 ( 10.00 , 20.0 , 30.0 )
INSTANTIATED_PREFAB_NAME = " instantiated_prefab "
INSTANTIATED_CHILD_ENTITY_NAME = " child_1 "
TEST_LEVEL_FOLDER = " Prefab "
TEST_LEVEL_NAME = " Base "
def find_entity_by_name ( entity_name ) :
searchFilter = entity . SearchFilter ( )
searchFilter . names = [ entity_name ]
entityIds = entity . SearchBus ( bus . Broadcast , ' SearchEntities ' , searchFilter )
if entityIds and entityIds [ 0 ] . IsValid ( ) :
return entityIds [ 0 ]
return None
def print_error_if_failed ( prefab_operation_result ) :
if not prefab_operation_result . IsSuccess ( ) :
Report . info ( f ' Error message: { prefab_operation_result . GetError ( ) } ' )
# Open the test level
helper . init_idle ( )
helper . init_idle ( )
helper . open_level ( " Prefab " , " Base " )
helper . open_level ( TEST_LEVEL_FOLDER , TEST_LEVEL_NAME )
# Create a new Entity at the root level
# Create a new Entity at the root level
new_entity_id = editor . ToolsApplicationRequestBus ( bus . Broadcast , ' CreateNewEntity ' , EntityId ( ) )
new_entity_id = editor . ToolsApplicationRequestBus ( bus . Broadcast , ' CreateNewEntity ' , EntityId ( ) )
Report . result ( Tests . create_new_entity , new_entity_id . IsValid ( ) )
Report . result ( Tests . create_new_entity , new_entity_id . IsValid ( ) )
# Checks for prefab creation passed or not
# Checks for prefab creation passed or not
new_prefab_file_path = os . path . join ( os . path . dirname ( os . path . abspath ( __file__ ) ) , ' new_prefab.prefab ' )
create_prefab_result = prefab . PrefabPublicRequestBus ( bus . Broadcast , ' CreatePrefabInMemory ' , [ new_entity_id ] , NEW_PREFAB_FILE_PATH )
create_prefab_result = prefab . PrefabPublicRequestBus ( bus . Broadcast , ' CreatePrefabInMemory ' , [ new_entity_id ] , new_prefab_file_path )
Report. result ( Tests . create_prefab , create_prefab_result . IsSuccess ( ) )
Report . result ( Tests . create_prefab , create_prefab_result )
print_error_if_failed( create_prefab_result )
# Checks for prefab instantiation passed or not
# Checks for prefab instantiation passed or not
container_entity_id = prefab . PrefabPublicRequestBus ( bus . Broadcast , ' InstantiatePrefab ' , new_prefab_file_path , EntityId ( ) , EXPECTED_NEW_PREFAB_POSITION )
instantiate_prefab_result = prefab . PrefabPublicRequestBus ( bus . Broadcast , ' InstantiatePrefab ' , NEW_PREFAB_FILE_PATH , EntityId ( ) , INSTANTIATED_PREFAB_POSITION )
Report . result ( Tests . instantiate_prefab , container_entity_id . IsValid ( ) )
Report . result ( Tests . instantiate_prefab , instantiate_prefab_result . IsSuccess ( ) and instantiate_prefab_result . GetValue ( ) . IsValid ( ) )
print_error_if_failed ( instantiate_prefab_result )
container_entity_id = instantiate_prefab_result . GetValue ( )
editor . EditorEntityAPIBus ( bus . Event , ' SetName ' , container_entity_id , INSTANTIATED_PREFAB_NAME )
children_entity_ids = editor . EditorEntityInfoRequestBus ( bus . Event , ' GetChildren ' , container_entity_id )
Report . result ( Tests . has_one_child , len ( children_entity_ids ) is 1 )
child_entity_id = children_entity_ids [ 0 ]
editor . EditorEntityAPIBus ( bus . Event , ' SetName ' , child_entity_id , INSTANTIATED_CHILD_ENTITY_NAME )
# Checks if the new prefab is at the correct position and if it fails, it will provide the expected postion and the actual postion of the entity in the Editor log
# Checks if the new prefab is at the correct position and if it fails, it will provide the expected postion and the actual postion of the entity in the Editor log
new_prefab_position = azlmbr . components . TransformBus ( azlmbr . bus . Event , " GetWorldTranslation " , container_entity_id )
actual _prefab_position = azlmbr . components . TransformBus ( azlmbr . bus . Event , " GetWorldTranslation " , container_entity_id )
is_at_position = new_prefab_position . IsClose ( EXPECTED_NEW_PREFAB_POSITION )
is_at_position = actual_prefab_position. IsClose ( INSTANTIATED _PREFAB_POSITION)
Report . result ( Tests . new_prefab_position , is_at_position )
Report . result ( Tests . instantiated _prefab_position, is_at_position )
if not is_at_position :
if not is_at_position :
Report . info ( f ' Expected position: { EXPECTED_NEW_PREFAB_POSITION . ToString ( ) } , actual position: { new_prefab_position . ToString ( ) } ' )
Report . info ( f ' Expected position: { INSTANTIATED _PREFAB_POSITION. ToString ( ) } , actual position: { actual _prefab_position. ToString ( ) } ' )
# Checks for prefab deletion passed or not
delete_prefab_result = prefab . PrefabPublicRequestBus ( bus . Broadcast , ' DeleteEntitiesAndAllDescendantsInInstance ' , [ container_entity_id ] )
Report . result ( Tests . delete_prefab , delete_prefab_result . IsSuccess ( ) )
print_error_if_failed ( delete_prefab_result )
Report . result ( Tests . instantiated_prefab_removed , find_entity_by_name ( INSTANTIATED_PREFAB_NAME ) is None )
Report . result ( Tests . instantiated_child_removed , find_entity_by_name ( INSTANTIATED_CHILD_ENTITY_NAME ) is None )
if __name__ == " __main__ " :
if __name__ == " __main__ " :
from editor_python_test_tools . utils import Report
from editor_python_test_tools . utils import Report