LegacyActorComponentConverter.py 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. Lumberyard Actor Component to Actor+Material Component Conversion Script
  6. """
  7. from LegacyConversionHelpers import *
  8. from LegacyMaterialComponentConverter import *
  9. class Actor_Component_Converter(Component_Converter):
  10. """
  11. Converts point lights
  12. """
  13. def __init__(self, assetCatalogHelper, statsCollector, normalizedProjectDir):
  14. Component_Converter.__init__(self, assetCatalogHelper, statsCollector)
  15. # These are constant for every component in the file
  16. self.materialComponentConverter = Material_Component_Converter(assetCatalogHelper)
  17. self.normalizedProjectDir = normalizedProjectDir
  18. # These need to be reset between each component
  19. self.oldMaterialRelativePath = ""
  20. self.oldFbxRelativePathWithoutExtension = ""
  21. def is_this_the_component_im_looking_for(self, xmlElement, parent):
  22. if "name" in xmlElement.keys() and xmlElement.get("name") == "EditorActorComponent":
  23. for sibling in list(parent):
  24. if "name" in sibling.keys() and sibling.get("name") == "EditorMaterialComponent":
  25. # There is already a material component on this actor, so we don't need to convert it again
  26. return False
  27. # Found an actor that doesn't have a material component
  28. return True
  29. # Not an actor
  30. return False
  31. def gather_info_for_conversion(self, xmlElement, parent):
  32. # We don't need to modify the actor component, but we do need to add a material component
  33. for possibleActorAssetComponent in xmlElement.iter():
  34. if "field" in possibleActorAssetComponent.keys() and possibleActorAssetComponent.get("field") == "ActorAsset" and "value" in possibleActorAssetComponent.keys():
  35. assetId = possibleActorAssetComponent.get("value")
  36. actorPathStartIndex = assetId.find("hint={")
  37. actorPathStartIndex += len("hint={")
  38. actorPathEndIndex = assetId.find(".actor")
  39. self.oldFbxRelativePathWithoutExtension = assetId[actorPathStartIndex: actorPathEndIndex]
  40. elif "field" in possibleActorAssetComponent.keys() and possibleActorAssetComponent.get("field") == "MaterialPerActor":
  41. # TODO - support the actor component's "MaterialPerLOD"
  42. for simpleAssetReferenceChild in possibleActorAssetComponent:
  43. if "name" in simpleAssetReferenceChild.keys() and simpleAssetReferenceChild.get("name") == "SimpleAssetReferenceBase":
  44. for simpleAssetReferenceBaseChild in simpleAssetReferenceChild:
  45. if "field" in simpleAssetReferenceBaseChild.keys() and simpleAssetReferenceBaseChild.get("field") == "AssetPath" and "value" in simpleAssetReferenceBaseChild.keys():
  46. # We've found the material override
  47. self.oldMaterialRelativePath = simpleAssetReferenceBaseChild.get("value")
  48. def convert(self, xmlElement, parent):
  49. """
  50. Adds a sibling material component
  51. """
  52. if len(self.oldMaterialRelativePath) == 0 or self.oldMaterialRelativePath[0:self.oldMaterialRelativePath.find(".mtl")] == self.oldFbxRelativePathWithoutExtension:
  53. # There was no material override
  54. self.statsCollector.noMaterialOverrideCount += 1
  55. else:
  56. # There was a material override
  57. print("Material Override: fbx {0} override {1}".format(self.oldFbxRelativePathWithoutExtension, self.oldMaterialRelativePath))
  58. self.statsCollector.materialOverrideCount += 1
  59. # We don't modify the xmlElement here because we do not want to replace the old ActorComponet, we just want to add a material component
  60. atomMaterialInDefaultSlot = self.materialComponentConverter.convert_legacy_mtl_relative_path_to_atom_material_assetid(self.normalizedProjectDir, self.oldMaterialRelativePath, self.oldFbxRelativePathWithoutExtension)
  61. isActor = True
  62. atomMaterialList = self.materialComponentConverter.convert_legacy_mtl_relative_path_to_atom_material_list(self.normalizedProjectDir, self.oldMaterialRelativePath, self.oldFbxRelativePathWithoutExtension, isActor)
  63. parent.append(self.materialComponentConverter.create_material_component_with_material_assignments(atomMaterialInDefaultSlot, atomMaterialList))
  64. # TODO - protect against running this more than once on an actor? Kind of handled by the 'already converted' log file. Not a problem with meshes because the legacy mesh component gets stripped, but it is an issue with actors since they don't get removed
  65. def reset(self):
  66. self.oldMaterialRelativePath = ""
  67. self.oldFbxRelativePathWithoutExtension = ""