> For the complete documentation index, see [llms.txt](https://bkortbus.gitbook.io/unreal-python-recipe-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bkortbus.gitbook.io/unreal-python-recipe-book/metadata/getting-metadata.md).

# 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 ](https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/AssetData?application_version=5.3#unreal-assetdata)reference (lightweight header info of an asset):

```python
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:

```python
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:

```python
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)
```

{% hint style="info" %} <mark style="color:yellow;">Note</mark>: In this setup, metadata is returned as a `string` - you'll need to convert it to another data type if desired
{% endhint %}

***

## 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

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FRLKckioLT11OUuCmPev0%2Fimage.png?alt=media&amp;token=a90ae81a-298f-4369-af83-e816a4d16b80" alt=""><figcaption></figcaption></figure>

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:

```python
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:

```python
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

{% hint style="info" %} <mark style="color:yellow;">Note</mark>: 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`&#x20;
{% endhint %}

### Demo

This is the metadata on our test asset:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FAb4kcN4V6MIa6vTvTawT%2Fimage.png?alt=media&amp;token=80104d51-318c-4088-9cc1-58233eedff5f" alt=""><figcaption></figcaption></figure>

The data is stored as a <mark style="color:green;">string</mark> on the Unreal Asset, but now our Python Function automatically returns them as their intended types:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FiU3bl3LKr5KVTDVkYjOu%2Fimage.png?alt=media&amp;token=e8355c4b-169a-4ef3-9f1c-4acf90f94019" alt=""><figcaption></figcaption></figure>
