123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- """
- Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
- SPDX-License-Identifier: Apache-2.0 OR MIT
- File to assist with common hydra component functions used across various Atom tests.
- """
- import os
- from editor_python_test_tools.editor_test_helper import EditorTestHelper
- helper = EditorTestHelper(log_prefix="Atom_EditorTestHelper")
- def create_basic_atom_level(level_name):
- """
- Creates a new level inside the Editor matching level_name & adds the following:
- 1. "default_level" entity to hold all other entities.
- 2. Adds Grid, Global Skylight (IBL), ground Mesh, Directional Light, Sphere w/ material+mesh, & Camera components.
- 3. Each of these components has its settings tweaked slightly to match the ideal scene to test Atom rendering.
- :param level_name: name of the level to create and apply this basic setup to.
- :return: None
- """
- import azlmbr.asset as asset
- import azlmbr.bus as bus
- import azlmbr.camera as camera
- import azlmbr.editor as editor
- import azlmbr.entity as entity
- import azlmbr.legacy.general as general
- import azlmbr.math as math
- import azlmbr.object
- import editor_python_test_tools.hydra_editor_utils as hydra
- # Create a new level.
- new_level_name = level_name
- heightmap_resolution = 512
- heightmap_meters_per_pixel = 1
- terrain_texture_resolution = 412
- use_terrain = False
- # Return codes are ECreateLevelResult defined in CryEdit.h
- return_code = general.create_level_no_prompt(
- new_level_name, heightmap_resolution, heightmap_meters_per_pixel, terrain_texture_resolution, use_terrain)
- if return_code == 1:
- general.log(f"{new_level_name} level already exists")
- elif return_code == 2:
- general.log("Failed to create directory")
- elif return_code == 3:
- general.log("Directory length is too long")
- elif return_code != 0:
- general.log("Unknown error, failed to create level")
- else:
- general.log(f"{new_level_name} level created successfully")
- # Enable idle and update viewport.
- general.idle_enable(True)
- general.idle_wait(1.0)
- general.update_viewport()
- general.idle_wait(0.5) # half a second is more than enough for updating the viewport.
- # Close out problematic windows, FPS meters, and anti-aliasing.
- if general.is_helpers_shown(): # Turn off the helper gizmos if visible
- general.toggle_helpers()
- general.idle_wait(1.0)
- if general.is_pane_visible("Error Report"): # Close Error Report windows that block focus.
- general.close_pane("Error Report")
- if general.is_pane_visible("Error Log"): # Close Error Log windows that block focus.
- general.close_pane("Error Log")
- general.idle_wait(1.0)
- general.run_console("r_displayInfo=0")
- general.run_console("r_antialiasingmode=0")
- general.idle_wait(1.0)
- # Delete all existing entities & create default_level entity
- search_filter = azlmbr.entity.SearchFilter()
- all_entities = entity.SearchBus(azlmbr.bus.Broadcast, "SearchEntities", search_filter)
- editor.ToolsApplicationRequestBus(bus.Broadcast, "DeleteEntities", all_entities)
- default_level = hydra.Entity("default_level")
- default_position = math.Vector3(0.0, 0.0, 0.0)
- default_level.create_entity(default_position, ["Grid"])
- default_level.get_set_test(0, "Controller|Configuration|Secondary Grid Spacing", 1.0)
- # Set the viewport up correctly after adding the parent default_level entity.
- screen_width = 1280
- screen_height = 720
- degree_radian_factor = 0.0174533 # Used by "Rotation" property for the Transform component.
- general.set_viewport_size(screen_width, screen_height)
- general.update_viewport()
- helper.wait_for_condition(
- function=lambda: helper.isclose(a=general.get_viewport_size().x, b=screen_width, rel_tol=0.1)
- and helper.isclose(a=general.get_viewport_size().y, b=screen_height, rel_tol=0.1),
- timeout_in_seconds=4.0
- )
- result = helper.isclose(a=general.get_viewport_size().x, b=screen_width, rel_tol=0.1) and helper.isclose(
- a=general.get_viewport_size().y, b=screen_height, rel_tol=0.1)
- general.log(general.get_viewport_size().x)
- general.log(general.get_viewport_size().y)
- general.log(general.get_viewport_size().z)
- general.log(f"Viewport is set to the expected size: {result}")
- general.log("Basic level created")
- general.run_console("r_DisplayInfo = 0")
- # Create global_skylight entity and set the properties
- global_skylight = hydra.Entity("global_skylight")
- global_skylight.create_entity(
- entity_position=default_position,
- components=["HDRi Skybox", "Global Skylight (IBL)"],
- parent_id=default_level.id)
- global_skylight_asset_path = os.path.join(
- "LightingPresets", "greenwich_park_02_4k_iblskyboxcm_iblspecular.exr.streamingimage")
- global_skylight_asset_value = asset.AssetCatalogRequestBus(
- bus.Broadcast, "GetAssetIdByPath", global_skylight_asset_path, math.Uuid(), False)
- global_skylight.get_set_test(0, "Controller|Configuration|Cubemap Texture", global_skylight_asset_value)
- global_skylight.get_set_test(1, "Controller|Configuration|Diffuse Image", global_skylight_asset_value)
- global_skylight.get_set_test(1, "Controller|Configuration|Specular Image", global_skylight_asset_value)
- # Create ground_plane entity and set the properties
- ground_plane = hydra.Entity("ground_plane")
- ground_plane.create_entity(
- entity_position=default_position,
- components=["Material"],
- parent_id=default_level.id)
- azlmbr.components.TransformBus(azlmbr.bus.Event, "SetLocalUniformScale", ground_plane.id, 32.0)
- ground_plane_material_asset_path = os.path.join(
- "Materials", "Presets", "PBR", "metal_chrome.azmaterial")
- ground_plane_material_asset_value = asset.AssetCatalogRequestBus(
- bus.Broadcast, "GetAssetIdByPath", ground_plane_material_asset_path, math.Uuid(), False)
- ground_plane.get_set_test(0, "Default Material|Material Asset", ground_plane_material_asset_value)
- # Work around to add the correct Atom Mesh component
- mesh_type_id = azlmbr.globals.property.EditorMeshComponentTypeId
- ground_plane.components.append(
- editor.EditorComponentAPIBus(
- bus.Broadcast, "AddComponentsOfType", ground_plane.id, [mesh_type_id]
- ).GetValue()[0]
- )
- ground_plane_mesh_asset_path = os.path.join("Models", "plane.azmodel")
- ground_plane_mesh_asset_value = asset.AssetCatalogRequestBus(
- bus.Broadcast, "GetAssetIdByPath", ground_plane_mesh_asset_path, math.Uuid(), False)
- ground_plane.get_set_test(1, "Controller|Configuration|Mesh Asset", ground_plane_mesh_asset_value)
- # Create directional_light entity and set the properties
- directional_light = hydra.Entity("directional_light")
- directional_light.create_entity(
- entity_position=math.Vector3(0.0, 0.0, 10.0),
- components=["Directional Light"],
- parent_id=default_level.id)
- directional_light_rotation = math.Vector3(degree_radian_factor * -90.0, 0.0, 0.0)
- azlmbr.components.TransformBus(
- azlmbr.bus.Event, "SetLocalRotation", directional_light.id, directional_light_rotation)
- # Create sphere entity and set the properties
- sphere_entity = hydra.Entity("sphere")
- sphere_entity.create_entity(
- entity_position=math.Vector3(0.0, 0.0, 1.0),
- components=["Material"],
- parent_id=default_level.id)
- sphere_material_asset_path = os.path.join("Materials", "Presets", "PBR", "metal_brass_polished.azmaterial")
- sphere_material_asset_value = asset.AssetCatalogRequestBus(
- bus.Broadcast, "GetAssetIdByPath", sphere_material_asset_path, math.Uuid(), False)
- sphere_entity.get_set_test(0, "Default Material|Material Asset", sphere_material_asset_value)
- # Work around to add the correct Atom Mesh component
- sphere_entity.components.append(
- editor.EditorComponentAPIBus(
- bus.Broadcast, "AddComponentsOfType", sphere_entity.id, [mesh_type_id]
- ).GetValue()[0]
- )
- sphere_mesh_asset_path = os.path.join("Models", "sphere.azmodel")
- sphere_mesh_asset_value = asset.AssetCatalogRequestBus(
- bus.Broadcast, "GetAssetIdByPath", sphere_mesh_asset_path, math.Uuid(), False)
- sphere_entity.get_set_test(1, "Controller|Configuration|Mesh Asset", sphere_mesh_asset_value)
- # Create camera component and set the properties
- camera_entity = hydra.Entity("camera")
- camera_entity.create_entity(
- entity_position=math.Vector3(5.5, -12.0, 9.0),
- components=["Camera"],
- parent_id=default_level.id)
- rotation = math.Vector3(
- degree_radian_factor * -27.0, degree_radian_factor * -12.0, degree_radian_factor * 25.0
- )
- azlmbr.components.TransformBus(azlmbr.bus.Event, "SetLocalRotation", camera_entity.id, rotation)
- camera_entity.get_set_test(0, "Controller|Configuration|Field of view", 60.0)
- camera.EditorCameraViewRequestBus(azlmbr.bus.Event, "ToggleCameraAsActiveView", camera_entity.id)
|