For all Python lovers who struggle to connect your work to Unreal Engine

Madara Premawardhana
3 min readAug 5, 2023
Photo by Google DeepMind on Unsplash

If you start writing an algorithm which will do wonders in your python and if you are not sure how you can reflect or reuse the same in your game-engine based simulation, then this step by step guide is for you.

This guide is if you have some familiarity with Unreal Engine and the jargon. If not let me know, I will write a separate series that will take you from the basics.

For now, the steps are as follows:

Step 1: Set up Unreal Engine 5 Environment

  • Download and install Unreal Engine 5 from the official website.
  • Launch Unreal Engine 5 and create a new project or open an existing one.

Step 2: Create a New Blueprint Actor

  • In the Unreal Engine editor, go to the “Content Browser” tab.
  • Right-click in the content browser and select “Blueprint Class.”
  • Choose “Actor” as the parent class and give your Blueprint actor a name.

Step 3: Add Components to the Actor

  • In the Blueprint editor, you can add various components to your actor. For example, you can add a Skeletal Mesh Component, a Camera Component, or any other components required for your specific use case.

Step 4: Set Up Python Integration

  • In Unreal Engine 5, Python integration is achieved through the Python plugin. Make sure you have the Python plugin installed and enabled in your project.
  • Go to “Edit” in the main menu and select “Plugins.”
  • Find and enable the “Python Editor Script Plugin” and “Python Editor Script Plugin Helper” plugins.
  • Restart the Unreal Engine editor to apply the changes.

Step 5: Create Python Environment and Install Libraries

  • Open a terminal or command prompt and create a virtual environment for your Python-based machine learning code.
  • Activate the virtual environment and install the required libraries, such as TensorFlow, PyTorch, or any other machine learning library you intend to use.

Step 6: Write Python Machine Learning Code

  • Develop your machine learning algorithm in Python within the virtual environment. Save the Python code in a separate file (e.g., ml_algorithm.py).

Step 7: Create a C++ Wrapper for Python Code

  • To call Python code from C++ (Unreal Engine Blueprints are based on C++), you need to create a C++ wrapper for your Python machine learning code.
  • Create a new C++ class in Unreal Engine by extending the UObject class and include the necessary headers for Python integration.

Step 8: Implement Python Code Execution in C++

  • In the C++ class, use the Python API to call and execute the Python machine learning code. You will need to include the Python headers and set up the Python interpreter in your C++ code.
  • Pass any required input data to the Python function and receive the output from the Python code.

Step 9: Expose C++ Functions to Blueprint

  • In your C++ class, mark the functions you want to access from the Blueprint editor with the UFUNCTION(BlueprintCallable) macro. This will expose the functions to Unreal Engine Blueprints.

Step 10: Implement the Machine Learning Actor in Blueprints

  • In the Blueprint editor, open the Blueprint Actor you created earlier.
  • Add custom event nodes and connect them to the exposed C++ functions that call the Python machine learning code.

Step 11: Test and Debug

  • Test the machine learning actor within the Unreal Engine environment.
  • Debug any issues that may arise during the integration process.

Step 12: Iterate and Optimize

  • Iterate on your machine learning actor, making improvements and optimizations as needed.
  • Continue testing and refining the actor until it performs as expected.

--

--

Madara Premawardhana

PhD Student at the University of Buckingham, School of Computing