ComponentAssetCommands_test_case.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """
  2. All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. its licensors.
  4. For complete copyright and license terms please see the LICENSE at the root of this
  5. distribution (the "License"). All use of this software is governed by the License,
  6. or, if provided, by the license below or the license accompanying this file. Do not
  7. remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. """
  10. # Tests a portion of the Component Property Get/Set Python API while the Editor is running
  11. import azlmbr.bus as bus
  12. import azlmbr.editor as editor
  13. import azlmbr.entity as entity
  14. import azlmbr.math as math
  15. import azlmbr.asset as asset
  16. # Open a level (any level should work)
  17. editor.EditorToolsApplicationRequestBus(bus.Broadcast, 'OpenLevelNoPrompt', 'WaterSample')
  18. def GetSetCompareTest(component, path, assetId):
  19. # Test Get/Set (get old value, set new value, check that new value was set correctly)
  20. oldObj = editor.EditorComponentAPIBus(bus.Broadcast, 'GetComponentProperty', component, path)
  21. if(oldObj.IsSuccess()):
  22. oldValue = oldObj.GetValue()
  23. oldValueCompared = editor.EditorComponentAPIBus(bus.Broadcast, 'CompareComponentProperty', component, path, oldValue)
  24. editor.EditorComponentAPIBus(bus.Broadcast, 'SetComponentProperty', component, path, assetId)
  25. newObj = editor.EditorComponentAPIBus(bus.Broadcast, 'GetComponentProperty', component, path)
  26. if(newObj.IsSuccess()):
  27. newValue = newObj.GetValue()
  28. newValueCompared = editor.EditorComponentAPIBus(bus.Broadcast, 'CompareComponentProperty', component, path, newValue)
  29. isOldNewValueSame = editor.EditorComponentAPIBus(bus.Broadcast, 'CompareComponentProperty', component, path, oldValue)
  30. if not(newValue == oldValue) and oldValueCompared and newValueCompared and not isOldNewValueSame:
  31. print("GetSetCompare Test " + path + ": SUCCESS")
  32. else:
  33. print("GetSetCompare Test " + path + ": FAILURE")
  34. # Test Clear (set an invalid AssetId, check that the field was cleared correctly)
  35. editor.EditorComponentAPIBus(bus.Broadcast, 'SetComponentProperty', component, path, asset.AssetId())
  36. clearObj = editor.EditorComponentAPIBus(bus.Broadcast, 'GetComponentProperty', component, path)
  37. if(clearObj.IsSuccess()):
  38. clearValue = clearObj.GetValue()
  39. clearedValueCompared = editor.EditorComponentAPIBus(bus.Broadcast, 'CompareComponentProperty', component, path, clearValue)
  40. isNewClearedValueSame = editor.EditorComponentAPIBus(bus.Broadcast, 'CompareComponentProperty', component, path, newValue)
  41. if (clearValue == asset.AssetId()) and clearedValueCompared and not isNewClearedValueSame:
  42. print("GetSetCompare Clear " + path + ": SUCCESS")
  43. else:
  44. print("GetSetCompare Clear " + path + ": FAILURE")
  45. def PteTest(pte, path, value):
  46. # Test Get/Set (get old value, set new value, check that new value was set correctly)
  47. oldObj = pte.get_value(path)
  48. if(oldObj.IsSuccess()):
  49. oldValue = oldObj.GetValue()
  50. oldValueCompared = pte.compare_value(path, oldValue)
  51. pte.set_value(path, value)
  52. newObj = pte.get_value(path)
  53. if(newObj.IsSuccess()):
  54. newValue = newObj.GetValue()
  55. newValueCompared = pte.compare_value(path, newValue)
  56. isOldNewValueSame = pte.compare_value(path, oldValue)
  57. if not(newValue == oldValue) and oldValueCompared and newValueCompared and not isOldNewValueSame:
  58. print("PTE Test " + path + ": SUCCESS")
  59. else:
  60. print("PTE Test " + path + ": FAILURE")
  61. # Test Clear (set an invalid AssetId, check that the field was cleared correctly)
  62. pte.set_value(path, asset.AssetId())
  63. clearObj = pte.get_value(path)
  64. if(clearObj.IsSuccess()):
  65. clearValue = clearObj.GetValue()
  66. clearedValueCompared = pte.compare_value(path, clearValue)
  67. isNewClearedValueSame = pte.compare_value(path, newValue)
  68. if (clearValue == asset.AssetId()) and clearedValueCompared and not isNewClearedValueSame:
  69. print("PTE Clear " + path + ": SUCCESS")
  70. else:
  71. print("PTE Clear " + path + ": FAILURE")
  72. # Create new Entity
  73. entityId = editor.ToolsApplicationRequestBus(bus.Broadcast, 'CreateNewEntity', entity.EntityId())
  74. if (entityId):
  75. print("New entity with no parent created: SUCCESS")
  76. # Get Component Type for Mesh
  77. typeIdsList = editor.EditorComponentAPIBus(bus.Broadcast, 'FindComponentTypeIdsByEntityType', ["Mesh"], entity.EntityType().Game)
  78. componentOutcome = editor.EditorComponentAPIBus(bus.Broadcast, 'AddComponentsOfType', entityId, typeIdsList)
  79. if (componentOutcome.IsSuccess()):
  80. print("Mesh component added to entity: SUCCESS")
  81. components = componentOutcome.GetValue()
  82. component = components[0]
  83. hasComponent = editor.EditorComponentAPIBus(bus.Broadcast, 'HasComponentOfType', entityId, typeIdsList[0])
  84. if(hasComponent):
  85. print("Entity has a Mesh component: SUCCESS")
  86. # Get the PTE from the Mesh Component
  87. pteObj = editor.EditorComponentAPIBus(bus.Broadcast, 'BuildComponentPropertyTreeEditor', component)
  88. if(pteObj.IsSuccess()):
  89. pte = pteObj.GetValue()
  90. # Tests for the Asset<> case
  91. testAssetId = asset.AssetCatalogRequestBus(bus.Broadcast, 'GetAssetIdByPath', 'assets/objects/foliage/cedar.azmodel', math.Uuid(), False)
  92. GetSetCompareTest(component, "Controller|Configuration|Mesh Asset", testAssetId)
  93. PteTest(pte, "Controller|Configuration|Mesh Asset", testAssetId)
  94. editor.EditorToolsApplicationRequestBus(bus.Broadcast, 'ExitNoPrompt')