asset_utils.py 1.7 KB

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