atom_component_helper.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. """
  2. 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.
  3. SPDX-License-Identifier: Apache-2.0 OR MIT
  4. File to assist with common hydra component functions used across various Atom tests.
  5. """
  6. import os
  7. from editor_python_test_tools.editor_test_helper import EditorTestHelper
  8. helper = EditorTestHelper(log_prefix="Atom_EditorTestHelper")
  9. def create_basic_atom_level(level_name):
  10. """
  11. Creates a new level inside the Editor matching level_name & adds the following:
  12. 1. "default_level" entity to hold all other entities.
  13. 2. Adds Grid, Global Skylight (IBL), ground Mesh, Directional Light, Sphere w/ material+mesh, & Camera components.
  14. 3. Each of these components has its settings tweaked slightly to match the ideal scene to test Atom rendering.
  15. :param level_name: name of the level to create and apply this basic setup to.
  16. :return: None
  17. """
  18. import azlmbr.asset as asset
  19. import azlmbr.bus as bus
  20. import azlmbr.camera as camera
  21. import azlmbr.editor as editor
  22. import azlmbr.entity as entity
  23. import azlmbr.legacy.general as general
  24. import azlmbr.math as math
  25. import azlmbr.object
  26. import editor_python_test_tools.hydra_editor_utils as hydra
  27. # Create a new level.
  28. new_level_name = level_name
  29. heightmap_resolution = 512
  30. heightmap_meters_per_pixel = 1
  31. terrain_texture_resolution = 412
  32. use_terrain = False
  33. # Return codes are ECreateLevelResult defined in CryEdit.h
  34. return_code = general.create_level_no_prompt(
  35. new_level_name, heightmap_resolution, heightmap_meters_per_pixel, terrain_texture_resolution, use_terrain)
  36. if return_code == 1:
  37. general.log(f"{new_level_name} level already exists")
  38. elif return_code == 2:
  39. general.log("Failed to create directory")
  40. elif return_code == 3:
  41. general.log("Directory length is too long")
  42. elif return_code != 0:
  43. general.log("Unknown error, failed to create level")
  44. else:
  45. general.log(f"{new_level_name} level created successfully")
  46. # Enable idle and update viewport.
  47. general.idle_enable(True)
  48. general.idle_wait(1.0)
  49. general.update_viewport()
  50. general.idle_wait(0.5) # half a second is more than enough for updating the viewport.
  51. # Close out problematic windows, FPS meters, and anti-aliasing.
  52. if general.is_helpers_shown(): # Turn off the helper gizmos if visible
  53. general.toggle_helpers()
  54. general.idle_wait(1.0)
  55. if general.is_pane_visible("Error Report"): # Close Error Report windows that block focus.
  56. general.close_pane("Error Report")
  57. if general.is_pane_visible("Error Log"): # Close Error Log windows that block focus.
  58. general.close_pane("Error Log")
  59. general.idle_wait(1.0)
  60. general.run_console("r_displayInfo=0")
  61. general.run_console("r_antialiasingmode=0")
  62. general.idle_wait(1.0)
  63. # Delete all existing entities & create default_level entity
  64. search_filter = azlmbr.entity.SearchFilter()
  65. all_entities = entity.SearchBus(azlmbr.bus.Broadcast, "SearchEntities", search_filter)
  66. editor.ToolsApplicationRequestBus(bus.Broadcast, "DeleteEntities", all_entities)
  67. default_level = hydra.Entity("default_level")
  68. default_position = math.Vector3(0.0, 0.0, 0.0)
  69. default_level.create_entity(default_position, ["Grid"])
  70. default_level.get_set_test(0, "Controller|Configuration|Secondary Grid Spacing", 1.0)
  71. # Set the viewport up correctly after adding the parent default_level entity.
  72. screen_width = 1280
  73. screen_height = 720
  74. degree_radian_factor = 0.0174533 # Used by "Rotation" property for the Transform component.
  75. general.set_viewport_size(screen_width, screen_height)
  76. general.update_viewport()
  77. helper.wait_for_condition(
  78. function=lambda: helper.isclose(a=general.get_viewport_size().x, b=screen_width, rel_tol=0.1)
  79. and helper.isclose(a=general.get_viewport_size().y, b=screen_height, rel_tol=0.1),
  80. timeout_in_seconds=4.0
  81. )
  82. result = helper.isclose(a=general.get_viewport_size().x, b=screen_width, rel_tol=0.1) and helper.isclose(
  83. a=general.get_viewport_size().y, b=screen_height, rel_tol=0.1)
  84. general.log(general.get_viewport_size().x)
  85. general.log(general.get_viewport_size().y)
  86. general.log(general.get_viewport_size().z)
  87. general.log(f"Viewport is set to the expected size: {result}")
  88. general.log("Basic level created")
  89. general.run_console("r_DisplayInfo = 0")
  90. # Create global_skylight entity and set the properties
  91. global_skylight = hydra.Entity("global_skylight")
  92. global_skylight.create_entity(
  93. entity_position=default_position,
  94. components=["HDRi Skybox", "Global Skylight (IBL)"],
  95. parent_id=default_level.id)
  96. global_skylight_asset_path = os.path.join(
  97. "LightingPresets", "greenwich_park_02_4k_iblskyboxcm_iblspecular.exr.streamingimage")
  98. global_skylight_asset_value = asset.AssetCatalogRequestBus(
  99. bus.Broadcast, "GetAssetIdByPath", global_skylight_asset_path, math.Uuid(), False)
  100. global_skylight.get_set_test(0, "Controller|Configuration|Cubemap Texture", global_skylight_asset_value)
  101. global_skylight.get_set_test(1, "Controller|Configuration|Diffuse Image", global_skylight_asset_value)
  102. global_skylight.get_set_test(1, "Controller|Configuration|Specular Image", global_skylight_asset_value)
  103. # Create ground_plane entity and set the properties
  104. ground_plane = hydra.Entity("ground_plane")
  105. ground_plane.create_entity(
  106. entity_position=default_position,
  107. components=["Material"],
  108. parent_id=default_level.id)
  109. azlmbr.components.TransformBus(azlmbr.bus.Event, "SetLocalUniformScale", ground_plane.id, 32.0)
  110. ground_plane_material_asset_path = os.path.join(
  111. "Materials", "Presets", "PBR", "metal_chrome.azmaterial")
  112. ground_plane_material_asset_value = asset.AssetCatalogRequestBus(
  113. bus.Broadcast, "GetAssetIdByPath", ground_plane_material_asset_path, math.Uuid(), False)
  114. ground_plane.get_set_test(0, "Default Material|Material Asset", ground_plane_material_asset_value)
  115. # Work around to add the correct Atom Mesh component
  116. mesh_type_id = azlmbr.globals.property.EditorMeshComponentTypeId
  117. ground_plane.components.append(
  118. editor.EditorComponentAPIBus(
  119. bus.Broadcast, "AddComponentsOfType", ground_plane.id, [mesh_type_id]
  120. ).GetValue()[0]
  121. )
  122. ground_plane_mesh_asset_path = os.path.join("Models", "plane.azmodel")
  123. ground_plane_mesh_asset_value = asset.AssetCatalogRequestBus(
  124. bus.Broadcast, "GetAssetIdByPath", ground_plane_mesh_asset_path, math.Uuid(), False)
  125. ground_plane.get_set_test(1, "Controller|Configuration|Mesh Asset", ground_plane_mesh_asset_value)
  126. # Create directional_light entity and set the properties
  127. directional_light = hydra.Entity("directional_light")
  128. directional_light.create_entity(
  129. entity_position=math.Vector3(0.0, 0.0, 10.0),
  130. components=["Directional Light"],
  131. parent_id=default_level.id)
  132. directional_light_rotation = math.Vector3(degree_radian_factor * -90.0, 0.0, 0.0)
  133. azlmbr.components.TransformBus(
  134. azlmbr.bus.Event, "SetLocalRotation", directional_light.id, directional_light_rotation)
  135. # Create sphere entity and set the properties
  136. sphere_entity = hydra.Entity("sphere")
  137. sphere_entity.create_entity(
  138. entity_position=math.Vector3(0.0, 0.0, 1.0),
  139. components=["Material"],
  140. parent_id=default_level.id)
  141. sphere_material_asset_path = os.path.join("Materials", "Presets", "PBR", "metal_brass_polished.azmaterial")
  142. sphere_material_asset_value = asset.AssetCatalogRequestBus(
  143. bus.Broadcast, "GetAssetIdByPath", sphere_material_asset_path, math.Uuid(), False)
  144. sphere_entity.get_set_test(0, "Default Material|Material Asset", sphere_material_asset_value)
  145. # Work around to add the correct Atom Mesh component
  146. sphere_entity.components.append(
  147. editor.EditorComponentAPIBus(
  148. bus.Broadcast, "AddComponentsOfType", sphere_entity.id, [mesh_type_id]
  149. ).GetValue()[0]
  150. )
  151. sphere_mesh_asset_path = os.path.join("Models", "sphere.azmodel")
  152. sphere_mesh_asset_value = asset.AssetCatalogRequestBus(
  153. bus.Broadcast, "GetAssetIdByPath", sphere_mesh_asset_path, math.Uuid(), False)
  154. sphere_entity.get_set_test(1, "Controller|Configuration|Mesh Asset", sphere_mesh_asset_value)
  155. # Create camera component and set the properties
  156. camera_entity = hydra.Entity("camera")
  157. camera_entity.create_entity(
  158. entity_position=math.Vector3(5.5, -12.0, 9.0),
  159. components=["Camera"],
  160. parent_id=default_level.id)
  161. rotation = math.Vector3(
  162. degree_radian_factor * -27.0, degree_radian_factor * -12.0, degree_radian_factor * 25.0
  163. )
  164. azlmbr.components.TransformBus(azlmbr.bus.Event, "SetLocalRotation", camera_entity.id, rotation)
  165. camera_entity.get_set_test(0, "Controller|Configuration|Field of view", 60.0)
  166. camera.EditorCameraViewRequestBus(azlmbr.bus.Event, "ToggleCameraAsActiveView", camera_entity.id)