asset_utils.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. # Built-in Imports
  11. from __future__ import annotations
  12. # Lumberyard Imports
  13. import azlmbr.bus as bus
  14. import azlmbr.asset as azasset
  15. import azlmbr.math as math
  16. class Asset:
  17. """
  18. Used to find Asset Id by its path and path of asset by its Id
  19. If a component has any asset property, then this class object can be called as:
  20. asset_id = editor_entity_utils.EditorComponent.get_component_property_value(<arguments>)
  21. asset = asset_utils.Asset(asset_id)
  22. """
  23. def __init__(self, id: azasset.AssetId):
  24. self.id: azasset.AssetId = id
  25. # Creation functions
  26. @classmethod
  27. def find_asset_by_path(cls, path: str, RegisterType: bool = False) -> Asset:
  28. """
  29. :param path: Absolute file path of the asset
  30. :param RegisterType: Whether to register the asset if it's not in the database,
  31. default to false for the general case
  32. :return: Asset object associated with file path
  33. """
  34. asset_id = azasset.AssetCatalogRequestBus(bus.Broadcast, "GetAssetIdByPath", path, math.Uuid(), RegisterType)
  35. assert asset_id.is_valid(), f"Couldn't find Asset with path: {path}"
  36. asset = cls(asset_id)
  37. return asset
  38. # Methods
  39. def get_path(self) -> str:
  40. """
  41. :return: Absolute file path of Asset
  42. """
  43. assert self.id.is_valid(), "Invalid Asset Id"
  44. return azasset.AssetCatalogRequestBus(bus.Broadcast, "GetAssetPathById", self.id)