non_uniform_scale.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import sys
  9. import azlmbr
  10. from pathlib import Path
  11. def fixup_current_level(threshold):
  12. nonUniformScaleComponentId = azlmbr.editor.EditorNonUniformScaleComponentTypeId
  13. # iterate over all entities in the level
  14. entityIdList = azlmbr.entity.SearchBus(azlmbr.bus.Broadcast, 'SearchEntities', azlmbr.entity.SearchFilter())
  15. for entityId in entityIdList:
  16. name = azlmbr.editor.EditorEntityInfoRequestBus(azlmbr.bus.Event, 'GetName', entityId)
  17. local = azlmbr.components.TransformBus(azlmbr.bus.Event, 'GetLocalScale', entityId)
  18. # only process entities where the non-uniformity is greater than the threshold
  19. local_max = max(local.x, local.y, local.z)
  20. local_min = min(local.x, local.y, local.z)
  21. if local_max / local_min > 1 + threshold:
  22. # check if there is already a Non-uniform Scale component
  23. getComponentOutcome = azlmbr.editor.EditorComponentAPIBus(azlmbr.bus.Broadcast, 'GetComponentOfType', entityId, nonUniformScaleComponentId)
  24. if getComponentOutcome.IsSuccess():
  25. print(f"skipping {name} as it already has a Non-uniform Scale component")
  26. else:
  27. # add Non-uniform Scale component and set it to the non-uniform part of the local scale
  28. azlmbr.editor.EditorComponentAPIBus(azlmbr.bus.Broadcast,'AddComponentsOfType', entityId, [nonUniformScaleComponentId])
  29. vec = azlmbr.math.Vector3(local.x / local_max, local.y / local_max, local.z / local_max)
  30. azlmbr.entity.NonUniformScaleRequestBus(azlmbr.bus.Event, 'SetScale', entityId, vec)
  31. print(f"added non-uniform scale component for {name}: {local.x}, {local.y}, {local.z}")
  32. if __name__ == '__main__':
  33. # handle the arguments manually since argparse causes problems when run through EditorPythonBindings
  34. process_all_levels = "--all" in sys.argv
  35. # ignore entities where the relative difference between the min and max scale values is less than this threshold
  36. threshold = 0.001
  37. for i in range(len(sys.argv) - 1):
  38. if sys.argv[i] == "--threshold":
  39. try:
  40. threshold = float(sys.argv[i + 1])
  41. except ValueError:
  42. print(f"invalid threshold value {sys.argv[i + 1]}, using default value {threshold}")
  43. pass
  44. if process_all_levels:
  45. game_folder = Path(azlmbr.legacy.general.get_game_folder())
  46. level_folder = game_folder / 'Levels'
  47. levels = [str(level) for level in level_folder.rglob('*.ly')] + [str(level) for level in level_folder.rglob('*.cry')]
  48. for level in levels:
  49. if "_savebackup" not in level:
  50. print(f'loading level {level}')
  51. azlmbr.legacy.general.open_level_no_prompt(level)
  52. azlmbr.legacy.general.idle_wait(2.0)
  53. fixup_current_level(threshold)
  54. azlmbr.legacy.general.save_level()
  55. else:
  56. fixup_current_level(threshold)