UpdateSpawnableNodes.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # coding:utf-8
  2. #!/usr/bin/python
  3. #
  4. # Copyright (c) Contributors to the Open 3D Engine Project.
  5. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  6. #
  7. # SPDX-License-Identifier: Apache-2.0 OR MIT
  8. #
  9. #
  10. # -------------------------------------------------------------------------
  11. import sys
  12. import shutil
  13. from pathlib import Path
  14. import json
  15. def isSpawnableAsset(variableDatum):
  16. return (variableDatum.get('$type') == 'SpawnableAsset' or
  17. variableDatum.get('scriptCanvasType', {}).get('m_azType') == '{A96A5037-AD0D-43B6-9948-ED63438C4A52}')
  18. fullPath = sys.argv[1]
  19. path = Path(fullPath)
  20. fileName = path.stem
  21. pathToFile = path.parent.resolve()
  22. extension = path.suffix
  23. fileNameBackup = fileName + "_bckup"
  24. fullBackupPath = (path.parent).joinpath(fileNameBackup).with_suffix(extension)
  25. shutil.copy(fullPath, fullBackupPath)
  26. print("Backup saved to \'%s\'" % fullBackupPath)
  27. with open(fullPath, 'r') as file :
  28. filedata = file.read()
  29. prefab_dom = json.loads(filedata)
  30. # inspect DOM, do fixup - it'll be stored as a python dict
  31. components = prefab_dom['ClassData']['m_scriptCanvas']['Components']
  32. # replace 'Asset' with 'asset' in SpawnableScriptAssetRef variables
  33. for componentKey, componentValue in components.items():
  34. if 'm_variableData' in componentValue:
  35. variableData = componentValue['m_variableData']['m_nameVariableMap']
  36. for variable in variableData:
  37. variableDatum = variable.get('Value', {}).get('Datum', {})
  38. if isSpawnableAsset(variableDatum):
  39. if 'value' in variableDatum:
  40. asset = variableDatum['value'].pop('Asset')
  41. variableDatum['value']['asset'] = asset
  42. filedata = json.dumps(prefab_dom, indent=4)
  43. # finally replace all matching type Uuids and type names
  44. # SpawnTicketInstance -> EntitySpawnTicket
  45. filedata = filedata.replace('"$type": "SpawnTicketInstance"', '"$type": "AzFramework::EntitySpawnTicket"')
  46. filedata = filedata.replace('{2B5EB938-8962-4A43-A97B-112F398C604B}', '{BA62FF9A-A01E-4FEB-84C6-200881DF2B2B}')
  47. # TicketArray
  48. filedata = filedata.replace('{0F155764-DFEF-50FB-9B6E-E0EE9223A683}', '{7541F631-BA89-54F3-A6B8-33FF75B60192}')
  49. # StringToTicketMap
  50. filedata = filedata.replace('{C63B2684-DC8D-5C8F-B635-ABC248EEEF05}', '{1D4D0809-B9FD-5766-8F56-B6BD6F5CC16C}')
  51. # NumberToTicketMap
  52. filedata = filedata.replace('{C11E8C62-060A-5EC0-9370-412E43A66FED}', '{E7D8B6D5-ABF2-5067-86BA-B25CE38421C3}')
  53. # SpawnableAsset -> SpawnableScriptAssetRef (no need to replace Uuids, since this is only class rename)
  54. filedata = filedata.replace('"$type": "SpawnableAsset"', '"$type": "AzFramework::Scripts::SpawnableScriptAssetRef"')
  55. with open(fullPath, 'w') as file:
  56. file.write(filedata)
  57. print("Asset successfully updated")