hydra_AtomMaterialEditor_BasicTests.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. # import azlmbr.materialeditor will fail with a ModuleNotFound error when using this script with Editor.exe
  7. # This is because azlmbr.materialeditor only binds to MaterialEditor.exe and not Editor.exe
  8. # You need to launch this script with MaterialEditor.exe in order for azlmbr.materialeditor to appear.
  9. import os
  10. import sys
  11. import time
  12. import azlmbr.math as math
  13. import azlmbr.paths
  14. sys.path.append(os.path.join(azlmbr.paths.projectroot, "Gem", "PythonTests"))
  15. import Atom.atom_utils.material_editor_utils as material_editor
  16. NEW_MATERIAL = "test_material.material"
  17. NEW_MATERIAL_1 = "test_material_1.material"
  18. NEW_MATERIAL_2 = "test_material_2.material"
  19. TEST_MATERIAL_1 = "001_DefaultWhite.material"
  20. TEST_MATERIAL_2 = "002_BaseColorLerp.material"
  21. TEST_MATERIAL_3 = "003_MetalMatte.material"
  22. TEST_DATA_PATH = os.path.join(
  23. azlmbr.paths.engroot, "Gems", "Atom", "TestData", "TestData", "Materials", "StandardPbrTestCases"
  24. )
  25. MATERIAL_TYPE_PATH = os.path.join(
  26. azlmbr.paths.engroot, "Gems", "Atom", "Feature", "Common", "Assets",
  27. "Materials", "Types", "StandardPBR.materialtype",
  28. )
  29. CACHE_FILE_EXTENSION = ".azmaterial"
  30. def run():
  31. """
  32. Summary:
  33. Material Editor basic tests including the below
  34. 1. Opening an Existing Asset
  35. 2. Creating a New Asset
  36. 3. Closing Selected Material
  37. 4. Closing All Materials
  38. 5. Closing all but Selected Material
  39. 6. Saving Material
  40. 7. Saving as a New Material
  41. 8. Saving as a Child Material
  42. 9. Saving all Open Materials
  43. Expected Result:
  44. All the above functions work as expected in Material Editor.
  45. :return: None
  46. """
  47. # 1) Test Case: Opening an Existing Asset
  48. document_id = material_editor.open_material(MATERIAL_TYPE_PATH)
  49. print(f"Material opened: {material_editor.is_open(document_id)}")
  50. # Verify if the test material exists initially
  51. target_path = os.path.join(azlmbr.paths.projectroot, "Materials", NEW_MATERIAL)
  52. print(f"Test asset doesn't exist initially: {not os.path.exists(target_path)}")
  53. # 2) Test Case: Creating a New Material Using Existing One
  54. material_editor.save_document_as_child(document_id, target_path)
  55. material_editor.wait_for_condition(lambda: os.path.exists(target_path), 2.0)
  56. print(f"New asset created: {os.path.exists(target_path)}")
  57. time.sleep(2.0)
  58. # Verify if the newly created document is open
  59. new_document_id = material_editor.open_material(target_path)
  60. material_editor.wait_for_condition(lambda: material_editor.is_open(new_document_id))
  61. print(f"New Material opened: {material_editor.is_open(new_document_id)}")
  62. # 3) Test Case: Closing Selected Material
  63. print(f"Material closed: {material_editor.close_document(new_document_id)}")
  64. # Open materials initially
  65. document1_id, document2_id, document3_id = (
  66. material_editor.open_material(os.path.join(TEST_DATA_PATH, material))
  67. for material in [TEST_MATERIAL_1, TEST_MATERIAL_2, TEST_MATERIAL_3]
  68. )
  69. # 4) Test Case: Closing All Materials
  70. print(f"All documents closed: {material_editor.close_all_documents()}")
  71. # 5) Test Case: Closing all but Selected Material
  72. document1_id, document2_id, document3_id = (
  73. material_editor.open_material(os.path.join(TEST_DATA_PATH, material))
  74. for material in [TEST_MATERIAL_1, TEST_MATERIAL_2, TEST_MATERIAL_3]
  75. )
  76. result = material_editor.close_all_except_selected(document1_id)
  77. print(f"Close All Except Selected worked as expected: {result and material_editor.is_open(document1_id)}")
  78. # 6) Test Case: Saving Material
  79. document_id = material_editor.open_material(os.path.join(TEST_DATA_PATH, TEST_MATERIAL_1))
  80. property_name = azlmbr.name.Name("baseColor.color")
  81. initial_color = material_editor.get_property(document_id, property_name)
  82. # Assign new color to the material file and save the actual material
  83. expected_color = math.Color(0.25, 0.25, 0.25, 1.0)
  84. material_editor.set_property(document_id, property_name, expected_color)
  85. material_editor.save_document(document_id)
  86. time.sleep(2.0)
  87. # 7) Test Case: Saving as a New Material
  88. # Assign new color to the material file and save the document as copy
  89. expected_color_1 = math.Color(0.5, 0.5, 0.5, 1.0)
  90. material_editor.set_property(document_id, property_name, expected_color_1)
  91. target_path_1 = os.path.join(azlmbr.paths.projectroot, "Materials", NEW_MATERIAL_1)
  92. cache_file_name_1 = os.path.splitext(NEW_MATERIAL_1) # Example output: ('test_material_1', '.material')
  93. cache_file_1 = f"{cache_file_name_1[0]}{CACHE_FILE_EXTENSION}"
  94. target_path_1_cache = os.path.join(azlmbr.paths.products, "materials", cache_file_1)
  95. material_editor.save_document_as_copy(document_id, target_path_1)
  96. material_editor.wait_for_condition(lambda: os.path.exists(target_path_1_cache), 4.0)
  97. # 8) Test Case: Saving as a Child Material
  98. # Assign new color to the material file save the document as child
  99. expected_color_2 = math.Color(0.75, 0.75, 0.75, 1.0)
  100. material_editor.set_property(document_id, property_name, expected_color_2)
  101. target_path_2 = os.path.join(azlmbr.paths.projectroot, "Materials", NEW_MATERIAL_2)
  102. cache_file_name_2 = os.path.splitext(NEW_MATERIAL_1) # Example output: ('test_material_2', '.material')
  103. cache_file_2 = f"{cache_file_name_2[0]}{CACHE_FILE_EXTENSION}"
  104. target_path_2_cache = os.path.join(azlmbr.paths.products, "materials", cache_file_2)
  105. material_editor.save_document_as_child(document_id, target_path_2)
  106. material_editor.wait_for_condition(lambda: os.path.exists(target_path_2_cache), 4.0)
  107. # Close/Reopen documents
  108. material_editor.close_all_documents()
  109. document_id = material_editor.open_material(os.path.join(TEST_DATA_PATH, TEST_MATERIAL_1))
  110. document1_id = material_editor.open_material(target_path_1)
  111. document2_id = material_editor.open_material(target_path_2)
  112. # Verify if the changes are saved in the actual document
  113. actual_color = material_editor.get_property(document_id, property_name)
  114. print(f"Actual Document saved with changes: {material_editor.compare_colors(actual_color, expected_color)}")
  115. # Verify if the changes are saved in the document saved as copy
  116. actual_color = material_editor.get_property(document1_id, property_name)
  117. result_copy = material_editor.compare_colors(actual_color, expected_color_1)
  118. print(f"Document saved as copy is saved with changes: {result_copy}")
  119. # Verify if the changes are saved in the document saved as child
  120. actual_color = material_editor.get_property(document2_id, property_name)
  121. result_child = material_editor.compare_colors(actual_color, expected_color_2)
  122. print(f"Document saved as child is saved with changes: {result_child}")
  123. # Revert back the changes in the actual document
  124. material_editor.set_property(document_id, property_name, initial_color)
  125. material_editor.save_document(document_id)
  126. material_editor.close_all_documents()
  127. # 9) Test Case: Saving all Open Materials
  128. # Open first material and make change to the values
  129. document1_id = material_editor.open_material(os.path.join(TEST_DATA_PATH, TEST_MATERIAL_1))
  130. property1_name = azlmbr.name.Name("metallic.factor")
  131. initial_metallic_factor = material_editor.get_property(document1_id, property1_name)
  132. expected_metallic_factor = 0.444
  133. material_editor.set_property(document1_id, property1_name, expected_metallic_factor)
  134. # Open second material and make change to the values
  135. document2_id = material_editor.open_material(os.path.join(TEST_DATA_PATH, TEST_MATERIAL_2))
  136. property2_name = azlmbr.name.Name("baseColor.color")
  137. initial_color = material_editor.get_property(document2_id, property2_name)
  138. expected_color = math.Color(0.4156, 0.0196, 0.6862, 1.0)
  139. material_editor.set_property(document2_id, property2_name, expected_color)
  140. # Save all and close all documents
  141. material_editor.save_all()
  142. material_editor.close_all_documents()
  143. # Reopen materials and verify values
  144. document1_id = material_editor.open_material(os.path.join(TEST_DATA_PATH, TEST_MATERIAL_1))
  145. result = material_editor.is_close(
  146. material_editor.get_property(document1_id, property1_name), expected_metallic_factor, 0.00001
  147. )
  148. document2_id = material_editor.open_material(os.path.join(TEST_DATA_PATH, TEST_MATERIAL_2))
  149. result = result and material_editor.compare_colors(
  150. expected_color, material_editor.get_property(document2_id, property2_name))
  151. print(f"Save All worked as expected: {result}")
  152. # Revert the changes made
  153. material_editor.set_property(document1_id, property1_name, initial_metallic_factor)
  154. material_editor.set_property(document2_id, property2_name, initial_color)
  155. material_editor.save_all()
  156. material_editor.close_all_documents()
  157. if __name__ == "__main__":
  158. run()