Python Module Example

The Editor Startup / Shutdown code examples as one big module-style example

import unreal

asset_registry_helper = unreal.AssetRegistryHelpers()
asset_registry        = asset_registry_helper.get_asset_registry()

startup_id = None
shutdown_id = None

def on_pre_startup():
    """Scripts that can run immediately on Editor Startup"""
    print("Running Pre Startup Scripts")
    # Import and run Pre Startup scripts here


def on_post_startup():
    """Scripts that should run after the Asset Registry is fully loaded"""
    print("Running Post Startup Scripts")
    # Import and run Post Startup scripts here


def on_editor_shutdown():
    print("Running Editor Shutdown code")
    # NOTE: This is triggered AFTER the Editor UI and any Editor Utility Widgets
    #       have closed. It's best to 
    #       the Editor World state and any environment variables / settings


def wait_for_asset_registry(ignore=None):
    """Wait until the Asset Registry is ready before continuing"""
    if asset_registry.is_loading_assets():
        print(f"Still waiting on the Asset Registry...")
        return
    
    print(f"Asset Registry is ready!")
    
    # We can now remove the callback and continue
    global startup_id 
    unreal.unregister_slate_post_tick_callback(startup_id)
    
    # Run Post Startup
    on_post_startup()
    

def run():
    """run Pre Startup and then schedule Post Startup"""
    
    # Pre Startup can run right away
    on_pre_startup()
    
    # Schedule Post Startup
    global startup_id
    if not startup_id:
        startup_id = unreal.register_slate_post_tick_callback(wait_for_asset_registry)
    else:
        unreal.log_warning("Unreal Startup has already been run!")
    
    # Add Shutdown Hook
    global shutdown_id 
    shutdown_id = unreal.register_python_shutdown_callback(on_editor_shutdown)

Last updated