My convenience function setup for getting metadata looks like this as a baseline:
defget_metadata(asset,key):"""Get the loaded object or AssetData's metadata"""ifisinstance(asset, unreal.AssetData):return asset.get_tag_value(key)else:return unreal.EditorAssetLibrary.get_metadata_tag(asset, key)
Note: In this setup, metadata is returned as a string - you'll need to convert it to another data type if desired
Extending Metadata Data Types in Python
While the default behavior of Unreal is to store and return strings, there's nothing to say we can't extend it in our Python API!
Declaring our Data Types
The first step is to create a dictionary in Python mapping our metadata names to their data types - anything that is not a string needs to be added:
Expanding The Get Function
We can then use this dictionary in our getter function to convert the string metadata value back to its desired type:
The new function above works like this:
Get the Metadata Value from the Unreal Asset
Make sure the data is valid (not None)
Get the Metadata's Data Type from the Type Map
Convert the Metadata Value to its intended type
Note: Certain data types, such as bools, require custom handling to convert back. Converting a String to a Bool only checks if the string has a length, bool("False") returns True
Demo
This is the metadata on our test asset:
The data is stored as a string on the Unreal Asset, but now our Python Function automatically returns them as their intended types:
METADATA_TYPE_MAP = {
"managed_asset": bool,
"asset_version": int
}
def get_metadata(asset, key):
"""Get the loaded object or AssetData's metadata as its expected type"""
# Get the unreal metadata value
if isinstance(asset, unreal.AssetData):
value = asset.get_tag_value(key)
else:
value = unreal.EditorAssetLibrary.get_metadata_tag(asset, key)
# If the metadata was found & valid let's convert it!
if value and value.lower() != "none":
# Get this metadata key's expected value type, default is str (as-is)
value_type = METADATA_TYPE_MAP.get(key, str)
if value_type == bool:
# bools are a special case as bool(str) only checks for length
return value.lower() == "true"
else:
# most singular value types may be directly converted
return value_type(value)