Unreal Engine in custom context with Python

Hello devs,

I need to build custom tools for UE with Python. I launched UE for the first time a week ago, so I need a high-level understanding of how I can achieve my goals.

For Maya and Nuke, I am deploying a set of Python tools to a folder on the network drive. Then I launch apps in a certain context with Python so they “know” where those tools are and I have custom menus with my tools. So I can run applications (with Python or bat file) for any project with a custom environment and also provide additional context for different projects/shots.

For example:

def launch_maya(maya_exe, command, shot_id):

    os.environ['MAYA_SHELF_PATH'] = '{0}/tools/at_maya/settings/shelves'.format(root)
    os.environ['PYTHONPATH'] += ';{0}/tools/at_maya/settings'.format(root)
    os.environ['SHOT_ID'] = shot_id

    # Run MAYA
    cmd = [maya_exe, '-hideConsole',  '-command', command]
    subprocess.Popen(cmd)

What would be the workflow with UE in such a case?

Pretty much the same thing as there, except in UE (Assuming UE4 as I have exp with that but I imagine it must be close to the same in UE5) you have to set your startup script in the python plugin and then you can have it run anything to setup your PATH and continue on.

Read this but what I just talked about is “the init_unreal.py File” section

1 Like

Something we’ve been doing for a while and you can apply to ANY executable, is run it with env vars set at the “application” level. This is as simple as launching a shell, set env vars and then your app. Once the shell (aka application) closes, those env variables no longer exist. On windows you can do this through command prompt or powershell. We use batch scripts or ps1 scripts.

The idea is that each project can have its own set of env vars and scripts. Or you can “bootstrap” a single script to point to specific env vars based on a simple data file before launching the app in question. This extends to UE4/UE5 as it applies to any app.

1 Like

How to get existing Variant Manager asset?

I know how to create a new Variant Manager:

import unreal

# Create Variant Manager asset
variant_manager = unreal.VariantManagerLibrary.create_level_variant_sets_asset('VariantManager', "/Game/assets/")

But how to get this object if it already exists?

PS. I figured out :slight_smile:

variant_set_name = 'trims'
variant_name = '_PRESET_HBO_UA_300C'
asset_path = '/Game/asset_data/USD/variant_manager'  # Variant Manager uaasset file
variant_sets = unreal.EditorAssetLibrary.load_asset(asset_path)
variant_set = variant_sets.get_variant_set_by_name(variant_set_name)
variant = variant_set.get_variant_by_name(variant_name)
variant.switch_on()