> 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/python-module-example.md).

# Python Module Example

<pre class="language-python" data-line-numbers data-full-width="true"><code class="lang-python"><strong>import unreal
</strong>
# metadata constants
# NOTE: metadata needs to be registered in the Asset Registry
#       in order to work correctly
META_MANAGED_ASSET = "managed_asset"
META_ASSET_NAME = "asset_name"
META_ASSET_VERSION = "asset_version"
<strong>
</strong><strong># mapping our metadata
</strong><strong>METADATA_TYPE_MAP = {
</strong>    META_MANAGED_ASSET : bool,
    META_ASSET_VERSION : int
}

# the Asset Registry
asset_registry_helper = unreal.AssetRegistryHelpers()
asset_registry        = asset_registry_helper.get_asset_registry()


# If the C++ function was exposed we can declare our metadata tags from Python
if hasattr(unreal, "PythonUtilsLibrary"):
    unreal.PythonUtilsLibrary.register_metadata_tags(
        [META_MANAGED_ASSET, META_ASSET_NAME , META_ASSET_VERSION]
    )


def set_metadata(asset, key, value):
    """set the asset's metadata value"""
    unreal.EditorAssetLibrary.set_metadata_tag(asset, key, str(value))
    
    
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 &#x26; 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)


def find_assets_by_metadata(meta_dict, class_names=["object"], parent_path = ""):
    """
    Find assets in the Content Browser based on 
    a given dict of metadata {key:value} pairs
    
    use 'parent_path' to only return results that live under that asset path
    """

    # create a basic Asset Registry filter based on the class_names
    # include the parent path if provided
    base_filter = unreal.ARFilter(
        class_names=class_names,
        recursive_classes=True,
        package_paths=[parent_path] if parent_path else [],
        recursive_paths=True,
    )
    
    # Start with the first Metadata Query if provided
    first_filter = base_filter
    if meta_dict:
        first_key = list(meta_dict.keys())[0]
        first_value = meta_dict.pop(first_key)
        query = [unreal.TagAndValue(first_key, str(first_value))]
        first_filter = asset_registry_helper.set_filter_tags_and_values(base_filter, query)

    # Get the initial class-based search results
    results = asset_registry.get_assets(first_filter) or []

    for key, value in meta_dict.items():
        # Create a new ARFilter based on the {key:value} pair
        query = [unreal.TagAndValue(key, str(value))]
        meta_filter = asset_registry_helper.set_filter_tags_and_values(base_filter, query)

        # reduce the results to only those matching the given metadata
        results = asset_registry.run_assets_through_filter(results, meta_filter) or []

    # return the results as a Python list as it might currently be an unreal.Array
    return list(results)
</code></pre>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bkortbus.gitbook.io/unreal-python-recipe-book/metadata/python-module-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
