Python: wait for asset registry is loaded

Hey there,

I have the problem that my asset registry is not loading fast enough. I want to read all assets in a folder with a for loop and unreal.find_asset. The problem is, that I want to do this right after the start of unreal (the function is in a init_unreal.py). When starting Unreal, the assets can’t be accessed and it prints the error: The AssetRegistry is currently loading.
I tried several things to wait for the asset registry to be loaded, but Unreal freezes at this point (forever).
That’s one of my tries:



asset_registry = unreal.AssetRegistryHelpers.get_asset_registry()
while True:
    is_loading = asset_registry.is_loading_assets()
    unreal.log_warning(is_loading)
    if is_loading == False:
        break


is there a “unreal way” to wait for things to be loaded?

Thanks in advance for your time and help.

When the editor runs a Python script, it waits for that script to complete before doing anything else. So, when you do a while loop like that, your script blocks the editor from ever doing anything else. What you have to do is give control back to the editor so that it can continue initializing, but register your Python code to get run at a later time.

I was able to get this to work by registering a callback with the editor that gets invoked on every “tick” – that is, every time the editor UI is updated.

for example:


import unreal

tickhandle = None

def testRegistry(deltaTime):
    unreal.log_warning("ticking.")
    asset_registry = unreal.AssetRegistryHelpers.get_asset_registry()
    if asset_registry.is_loading_assets():
        unreal.log_warning("still loading...")
    else:
        unreal.log_warning("ready!")
        unreal.unregister_slate_pre_tick_callback(tickhandle)

tickhandle = unreal.register_slate_pre_tick_callback(testRegistry)


The “tick” only starts happening once everything is fully loaded, so in this case the “still loading” never happens. The first tick, the asset registry is ready. Then, I unregister the callback just to avoid having the code get triggered over and over again doing nothing.

2 Likes

Thank you again for your fast and great help!
Good to know that the editor ‘pauses’ in the background.

Hi!

I just wanted to point out that the slate will tick regardless of the state of the Asset Registry. is_loading_assets probably never returned True because your asset library is so small that it gets fully loaded before the first tick. So on bigger projects, you will see still loading....

This shouldn’t be an issue if you are using the snippet as is, but if you are like me and took a shortcut based on this statement

The “tick” only starts happening once everything is fully loaded, so in this case the “still loading” never happens.

and did not check is_loading_asset during the tick, you’ll be bitten once the library becomes big enough.