Getting Metadata

How to retrieve metadata from an asset

Getting Metadata

There are two options when retrieving Metadata values on an asset:

A) From an AssetData reference (lightweight header info of an asset):

asset_path = "/Game/some/asset/path"
asset_data = unreal.EditorAssetLibrary.find_asset_data(asset_path)

value = asset_data.get_tag_value("asset_name")

B) From a loaded asset reference:

asset_path = "/Game/some/asset/path"
asset = unreal.load_asset(asset_path)

value = unreal.EditorAssetLibrary.get_metadata_tag(asset, "asset_name") 

My convenience function setup for getting metadata looks like this as a baseline:

def get_metadata(asset, key):
    """Get the loaded object or AssetData's metadata"""
    if isinstance(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:

METADATA_TYPE_MAP = {
    "managed_asset": bool,
    "asset_version": int
}

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:

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)

The new function above works like this:

  1. Get the Metadata Value from the Unreal Asset

  2. Make sure the data is valid (not None)

  3. Get the Metadata's Data Type from the Type Map

  4. 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:

Last updated