Startup Script Management
going beyond the init_unreal.py file
Covered in Organizing Startup Scripts, my init_unreal.py
file usually looks something like this:
The startup
module is where I organize everything I want to happen on Unreal startup, split into two main sections:
Pre Startup - Python code that can is safe to run immediately the moment Python is loaded
Examples might include:
Initializing a custom Python logger
Register metadata names
Environment setup, such as fixing the sys.path if required as part of a pipeline
Initialize Python classes other code might rely on
Loading external config data or packages
Post Startup - Python code to run after the Editor is fully loaded
I consider this step ready once the Asset Registry has scanned the entire UE Project
Examples might include:
Initializing the Python-defined Editor Menus
Opening Editor Utility Widget startup tools (such as a homepage tool listing all ShotGrid tasks)
Running any tools that rely on the Asset Registry
Waiting for the Asset Registry
The main separator of my Pre and Post Startup logic is whether the Asset Registry has finished loading the Unreal Project.
Thankfully, there is an easy enough function to reference on the Asset Registry: is_loading_assets()
We can use this with register_slate_post_tick_callback() to track the Asset Registry on startup to wait until it's ready:
And here's an example of running this during startup on a large project:
Note: The Asset Registry data does get cached on disk. Depending on PC specs, project size, and the last time the project was opened, the Asset Registry might be fully loaded by the time Python initializes
Full Startup Example
building off of the wait_for_asset_registry function, my startup module generally has the following in it:
A dedicated function that contains all Pre Startup commands
A dedicated function that contains all Post Startup commands
The Wait For Asset Registry callback function, which calls Post Startup when complete
A run() function that calls Pre Startup and sets up the Wait For Asset Registry callback
the
run()
function is the main entry point in this module, and is the only function that should be called externally
And here's what that code looks like:
In this setup, the only thing that needs to be updated / maintained are the on_pre_startup()
and on_post_startup()
functions - we shouldn't need to ever touch run()
or wait_for_asset_registry()
.
Last updated