Python in Unreal Engine — The undocumented parts

Some python features are not documented at all. Such as declaring struct or enum.
  • how to get a subsystem in python
  • how to return struct or array from a python function
  • how to pip install package into Unreal Engine

Single-purpose script — How to automatically import a whole folder of assets

import unreal
from pathlib import Path
IMPORT_DIR = Path(r"G:\path\to\fbx_folder")
assert IMPORT_DIR.exists()
tasks = []
for fbx in IMPORT_DIR.glob("*.fbx"):
asset_import_task = unreal.AssetImportTask()
asset_import_task.filename = str(fbx)
asset_import_task.destination_path = '/Game/Geometry'
asset_import_task.automated = True
tasks.append(asset_import_task)

asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
asset_tools.import_asset_tasks(tasks)
You can run a python script using the command “py”. This means you can also run python script in Movie Render Pipeline before or after render using Start/End commands

How to run python automatically on engine startup

How to get a reference to a subsystem in Python

Subsystems are accessed via special nodes in blueprints
aes = unreal.get_editor_subsystem(unreal.AssetEditorSubsystem)

Defining struct, enum and class in python and use it in a blueprint

MyPythonFunction declared lower, returning custom struct PythonUnrealStruct (output pin splitted)
  • When declaring ENUM, you should use values of numbers from 0 to N. You can skip numbers but Unreal expects that there is always an enum value of 0.
  • Struct must inherit from unreal.StructBase or its subtype. It didn't work from me if I inherited from the unreal.Struct. Fields in a struct are defined by unreal.uproperty
  • Don’t forget to mark your blueprint library functions as static

Using C++ types in python

How to use pip install with Unreal Engine

Installing directly in Unreal Engine

How to call python function through REST API

import unreal@unreal.uclass()
class RemoteClass(unreal.BlueprintFunctionLibrary):

@unreal.ufunction(static=True)
def remote():
print("Hello from remote!")
{
"objectPath": "/Engine/PythonTypes.Default__RemoteClass",
"functionName": "remote"
}

How to call python asynchronously from C++

FString PythonCommand(TEXT("SomePythonFunction()"));Future = FFunctionGraphTask::CreateAndDispatchWhenReady([=]
{
IPythonScriptPlugin::Get()->ExecPythonCommand(*PythonCommand);
});

Debugging python running in Unreal Engine

Python internals

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Designing Dynamic Environment Properties with Kubernetes ConfigMaps and Secrets(Nedx 2019)

Minecraft Vr Demo

Minecraft

Litecoin MimbleWimble July Update.

Bubble.io - How I Integrated the facebook login

2021 Report: State of Developer Relations

PyCharm!

CA Assistant,Scanner and Video Background Exchanger

On Technical Debt

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Filip Sivák

Filip Sivák

More from Medium

10 Important Standard Python Library Modules

Scheduling jobs in Python

A simple Python Module is a Singleton Instance

Python String Casefold() Method with Example — pythonpip.com