Organizing Startup Scripts

going beyond the init_unreal.py file

This section is especially subjective, while it's perfectly fine to put a lot of stuff in the init_unreal.py I don't like to do that from an organizational standpoint. I typically make a separate startup.py file that the init_unreal.py file impots and executes.

The two main reasons I do this is that:

  1. There may be multiple init_unreal.py files in the Python Path, making it ambiguous if there are any errors

  2. This provides a dedicated module I can inspect / reference elsewhere as needed

Examining init_unreal

Because Unreal executes every init_unreal.py file it fins in sys.path, it means there could be any number of competing python modules of the same name

To demonstrate this, here is a code snippet that lists the available init_unreal files and then shows which one is currently importable:

import sys
from pathlib import Path

print("init_unreal modules available:")
for folder_path in sys.path:
    for init_file in Path(folder_path).glob("init_unreal.py"):
        print(f"\t{init_file.resolve()}")

print("Location of imported init_unreal:")
import init_unreal
print(f"\t{init_unreal.__file__}")

In this situation there are several init_unreal files, while Unreal does execute them all it's tricky to debug or inspect a specific startup script after Unreal opens.


Namespacing Our Code / Startup

To make startup (and the rest of my code) clearer to reference and debug I generally do two things:

  1. Create a separate module for my startup script

  2. Create a folder for everything but the init_unreal module

This guarantees my startup module can be accessed after Unreal opens and makes it clearer that all of my modules are coming from the same folder:

With this setup my init_unreal.py file usually looks something like this:

from recipe_book import startup
startup.run()

Last updated