asset_builder_example.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #
  7. # Simple example asset builder that processes *.foo files
  8. #
  9. import azlmbr.math
  10. import azlmbr.asset.builder
  11. import os, shutil
  12. # the UUID must be unique amongst all the asset builders in Python or otherwise
  13. busIdString = '{E4DB381B-61A0-4729-ACD9-4C8BDD2D2282}'
  14. busId = azlmbr.math.Uuid_CreateString(busIdString)
  15. assetTypeScript = azlmbr.math.Uuid_CreateString('{82557326-4AE3-416C-95D6-C70635AB7588}')
  16. handler = None
  17. jobKeyPrefix = 'Foo Job Key'
  18. targetAssetFolder = 'foo_scripts'
  19. # creates a single job to compile for a 'pc' platform
  20. def on_create_jobs(args):
  21. request = args[0] # azlmbr.asset.builder.CreateJobsRequest
  22. response = azlmbr.asset.builder.CreateJobsResponse()
  23. # note: if the asset builder is going to handle more than one file pattern it might need to check out
  24. # the request.sourceFile to figure out what jobs need to be created
  25. jobDescriptorList = []
  26. for platformInfo in request.enabledPlatforms:
  27. # for each enabled platform like 'pc' or 'ios'
  28. platformId = platformInfo.identifier
  29. # set up unique job key
  30. jobKey = '{} {}'.format(jobKeyPrefix, platformId)
  31. # create job descriptor
  32. jobDesc = azlmbr.asset.builder.JobDescriptor()
  33. jobDesc.jobKey = jobKey
  34. jobDesc.set_platform_identifier(platformId)
  35. jobDescriptorList.append(jobDesc)
  36. print ('created a job for {} with key {}'.format(platformId, jobKey))
  37. response.createJobOutputs = jobDescriptorList
  38. response.result = azlmbr.asset.builder.CreateJobsResponse_ResultSuccess
  39. return response
  40. def get_target_name(sourceFullpath):
  41. lua_file = os.path.basename(sourceFullpath)
  42. lua_file = os.path.splitext(lua_file)[0]
  43. lua_file = lua_file + '.lua'
  44. return lua_file
  45. def copy_foo_file(srcFile, dstFile):
  46. try:
  47. dir_name = os.path.dirname(dstFile)
  48. if (os.path.exists(dir_name) is False):
  49. os.makedirs(dir_name)
  50. shutil.copyfile(srcFile, dstFile)
  51. return True
  52. except:
  53. return False
  54. # using the incoming 'request' find the type of job via 'jobKey' to determine what to do
  55. def on_process_job(args):
  56. request = args[0] # azlmbr.asset.builder.ProcessJobRequest
  57. response = azlmbr.asset.builder.ProcessJobResponse()
  58. # note: if possible to loop through incoming data a 'yeild' can be used to cooperatively
  59. # thread the processing of the assets so that shutdown and cancel can be handled
  60. if (request.jobDescription.jobKey.startswith(jobKeyPrefix)):
  61. targetFile = os.path.join(targetAssetFolder, get_target_name(request.fullPath))
  62. dstFile = os.path.join(request.tempDirPath, targetFile)
  63. if (copy_foo_file(request.fullPath, dstFile)):
  64. response.outputProducts = [azlmbr.asset.builder.JobProduct(dstFile, assetTypeScript, 0)]
  65. response.resultCode = azlmbr.asset.builder.ProcessJobResponse_Success
  66. response.dependenciesHandled = True
  67. return response
  68. def on_shutdown(args):
  69. # note: user should attempt to close down any processing job if any running
  70. global handler
  71. if (handler is not None):
  72. handler.disconnect()
  73. handler = None
  74. def on_cancel_job(args):
  75. # note: user should attempt to close down any processing job if any running
  76. print('>>> FOO asset builder - on_cancel_job <<<')
  77. # register asset builder for source assets
  78. def register_asset_builder():
  79. assetPattern = azlmbr.asset.builder.AssetBuilderPattern()
  80. assetPattern.pattern = '*.foo'
  81. assetPattern.type = azlmbr.asset.builder.AssetBuilderPattern_Wildcard
  82. builderDescriptor = azlmbr.asset.builder.AssetBuilderDesc()
  83. builderDescriptor.name = "Foo Asset Builder"
  84. builderDescriptor.patterns = [assetPattern]
  85. builderDescriptor.busId = busId
  86. builderDescriptor.version = 0
  87. outcome = azlmbr.asset.builder.PythonAssetBuilderRequestBus(azlmbr.bus.Broadcast, 'RegisterAssetBuilder', builderDescriptor)
  88. if outcome.IsSuccess():
  89. # created the asset builder handler to hook into the notification bus
  90. jobHandler = azlmbr.asset.builder.PythonBuilderNotificationBusHandler()
  91. jobHandler.connect(busId)
  92. jobHandler.add_callback('OnCreateJobsRequest', on_create_jobs)
  93. jobHandler.add_callback('OnProcessJobRequest', on_process_job)
  94. jobHandler.add_callback('OnShutdown', on_shutdown)
  95. jobHandler.add_callback('OnCancel', on_cancel_job)
  96. return jobHandler
  97. # note: the handler has to be retained since Python retains the object ref count
  98. # on_shutdown will clear the 'handler' to disconnect from the notification bus
  99. handler = register_asset_builder()