Find Assets By Metadata
How to find Content browser assets using Metadata
Last updated
How to find Content browser assets using Metadata
Last updated
This is a more complicated topic, but one of the most powerful asset management tools available in Unreal is the ability to search for assets using Metadata and the Asset Registry. This allows us to use more than just asset paths to manage our assets, making tools more flexible and robust in production.
One of the events that runs during the Unreal Editor startup process is a scan of the project for all of the available assets. This information is collected in the Asset Registry and is relied upon by a number of systems, such as the dropdown menu when changing a Material property:
The Asset Registry collects the header information for all of the assets in the Unreal Project using the AssetData class. It doesn't load the entire asset, it just tracks some of the essentials:
The Asset Class
The Asset Name
The Asset Path
The Asset Metadata
That last bullet point is what we care about here: if an asset has metadata on it, and that metadata key is registered in the Asset Registry, we can search for assets using that metadata.
Because this is a bit of a complicated topic, we're going to first start with an asset search function that just returns all of the assets of the given class type:
This is our baseline setup, asking the Asset Registry for a list of assets of the given class.
While there are a few similar functions available in the Asset Registry, I prefer this function because it has a lot of flexibility thanks to the ARFilter it uses, which will be important as we expand our function.
Once we have a list of assets from the Asset Registry as AssetData objects we can use the following operation to only get those matching the desired metadata values:
The set_filter_tags_and_values() function is responsible for most of the magic here - it will create an ARFilter based on the provided metadata key:value pair.
Using an example asset from the Getting Metadata page we can use this to find any assets with "asset_name"
set to "eugene"
:
In a production setting we can often find ourselves wanting to find a file based on multiple metadata values, such as getting version 10 of an particular asset. Let's set up our function to expect our metadata query as a dictionary:
Expanding the earlier function, once we get the list of results from the base_filter we want to loop over each metadata pair, further reducing the results to only those that match:
To simplify the function, here's essentially what it's doing:
Get a list of assets in the project of the given class(es)
Create an Asset Registry Filter for each metadata key:value
pair
Pass the list of assets through each Metadata Filter, removing any assets that don't match
With this function we now have a simplified method of searching for assets based on metadata:
To test the performance of this function I created an Unreal Project with nearly 40,000 tagged assets in it (37,880 to be precise!).
Using the following logic, I tested from zero metadata queries up to two metadata queries timing the function:
This was the result:
An interesting observation here is that the first Asset Registry interaction collecting the list of assets isn't the most expensive check — it's the second interaction, which performs the first Metadata filter.
Something we can try in our function is to start with the first metadata query:
Incredibly, this actually made it faster to search by metadata:
That's 0.03
seconds to find a specific tagged asset in the Unreal Project of 37,800 assets.
In a production environment we might want more than just the metadata in our asset search function. One example of note is to also filter results based on a Parent Path, only returning assets that live under a specific folder path.
Thankfully this isn't too hard to add, the ARFilter class has a property called package_paths
that can help us here. This property along with recursive_paths
allow us to filter assets based on the folder they live in.
And here is an updated function with a new parent_path parameter:
Without a parent_path
we get every Level Sequence in the project:
With a parent_path
we only get the Level Sequences found in /Game/dev
:
This function is now capable of looking for assets based on:
Metadata
Asset Class
Asset Path
The Asset Registry is quite powerful in Unreal. With this approach our tools can be a lot more robust and no longer need to rely on strict folder pathing or naming conventions, we can treat and manage our assets like a database service.
There's also a lot more functionality that can be added to the function examples above, I definitely encourage giving metadata a try in Unreal