Browse by type
Embed Python in Unreal Engine 4
This is a plugin embedding a whole Python VM (versions 3.x [the default and suggested one] and 2.7) In Unreal Engine 4 (both the editor and runtime).
The Python VM tries to give easy access to all of the UE4 internal api + its reflection system. This means you can use the plugin to write other plugins, to automate tasks and to implement gameplay elements.
It is not meant as a way to avoid blueprints or c++ but as a good companion to them (albeit reducing the amount of c++ required for coding a game could be an interesting thing ;)
Another funny feature is that you can change your python code even after the project has been packaged. You can potentially build a completely new game from an already packaged one.
In addition to this, the plugin automatically adds an actor class (PyActor), a pawn class (PyPawn), a character class (PyCharacter) and a component class (PythonComponent) for "gentle" integration of python in your games.
In the development menu, you get access to the 'PythonConsole' too, you can use it to trigger python commands directly from the editor.
All of the exposed engine features are under the 'unreal_engine' virtual module (it is completely coded in c into the plugin, so do not expect to run 'import unreal_engine' from a standard python shell)
The currently supported Unreal Engine versions are 4.12, 4.13 and 4.14
Check in the releases page (https://github.com/20tab/UnrealEnginePython/releases) if there is a binary version that matches your configuration (otherwise open an issue asking us for it [please specify the python version too]) and download it.
Binary releases are in two forms: standard and embedded. Standard uses the python installation of your system, so ensure the python installation directory is in your system PATH environment variable (otherwise you will get an error while loading your project). Embedded releases include an embedded python installation so you do not need to have python in your system.
Create (if it does not already exist) a Plugins directory in your project root directory (at the same level of Content/ and the .uproject file) and unzip the plugin into it. If your project is named FooBar you will end with FooBar/Plugins/UnrealEnginePython.
Open your project and go to the Edit/Plugins menu. Go to the bottom and under "Project/Scripting Languages" enable UnrealEnginePython.
Restart your project and you should see the PythonConsole under the "Window/Developer Tools" menu
Binary releases are mainly useful for editor scripting, if you want to package your project for distribution and you need the python runtime, you need a source release (see below).
If instead, you want to package your project without python, just remember to change the UnrealEnginePython.uplugin to have this line: https://github.com/20tab/UnrealEnginePython/blob/master/UnrealEnginePython.uplugin#L20 set as "Editor" instead of "Runtime"
Currently python3.6, python3.5 and python2.7 are supported. It is highly suggested to have a python system wide installation (by default the official python distributions are installed in user's home directory) with the PATH environment variable including it (if you change the PATH variable remember to reboot the system before running the build procedure, this is not strictly required but will ensure the PATH is updated). If the PATH variable does not contain the path of your python installation you will see a warning in the build log/output.
Download a source official release or simply clone the repository for latest updates:
git clone https://github.com/20tab/UnrealEnginePython
By default the build procedure will try to discover your python installation looking at hardcoded known paths. If you want to specify a custom python installation (or the autodetection simply fails) you can change it in the Source/UnrealEnginePython/UnrealEnginePython.Build.cs file at this line: https://github.com/20tab/UnrealEnginePython/blob/master/Source/UnrealEnginePython/UnrealEnginePython.Build.cs#L10
choose a project you want to install the plugin into, open the file explorer (you can do it from the epic launcher too) and:
If all goes well, you will see 'Python Console' in the "Window/Developer Tools" menu
If you want to package your project (it is required only if you need to have a python VM at runtime, read: your game logic is programmed in python) ensure the Content/Scripts/ue_site.py file is in your project (it can be empty). At the end of the build procedure ensure to copy all of your required python scripts in the final directory. Remember that unless you add an embedded python in your final build, the final users of your project will require python installed in his/her system.
If you want to package without python, just remember to change the UnrealEnginePython.uplugin to have this line: https://github.com/20tab/UnrealEnginePython/blob/master/UnrealEnginePython.uplugin#L20 set as "Editor" instead of "Runtime"
git clone https://github.com/20tab/UnrealEnginePython
The build procedure will try to automatically discover python installations. If you need custom paths, just edit here:
https://github.com/20tab/UnrealEnginePython/blob/master/Source/UnrealEnginePython/UnrealEnginePython.Build.cs#L10
To upgrade to the latest development version of UnrealEnginePython: * move to the Plugins directory in the project directory and use git pull
git pull
rm *.dylib
Currently the suggested distribution is Ubuntu Xenial (LTS 16.04) 64bit. Obviously you need to already have an Unreal Engine build (note that on ubuntu xenial you need to install the clang-3.5 package to build the editor). Both python2.7 and python3.5 are supported and the default configuration assumes python3 (so ensure to install the python3-dev package).
git clone https://github.com/20tab/UnrealEnginePython
NOTE: always run your project from a terminal so you can see startup logs (they are really useful when building the plugin the first time, if you cannot build the plugin, open an issue on github pasting the related log lines).
If you want to use python2 just edit the Source/UnrealEnginePython/UnrealEnginePython.Build.cs file and change the pythonHome string to "python27" (ensure to have the python2.7-dev package installed).
If you want to use an alternative python installation, go to the end of UnrealEnginePython.Build.cs, and change the paths of includes and libpython accordingly
Just remove the .so files in Plugins/UnrealEnginePython/Binaries/Linux and pull the latest code.
At the next run the build procedure wil be started again.
Currently only Windows, MacOSX and Linux are supported. We are investigating Android support too via the kivy project.
If your objective is to script the editor, you can directly jump to
https://github.com/20tab/UnrealEnginePython/tree/master/docs
and
https://github.com/20tab/UnrealEnginePython/tree/master/examples
The first directory contains the official documentation for specific areas, while the second one is a collection of python scripts doing any sort of 'magic' with your project ;)
We are going to create a new Actor based on python (instead of C++ or blueprints)
This is the "gentle" approach, using a 'proxy' python class to speak with the UE4 api. Once you get familiar with the system, you can go further and start working withe native subclassing api (https://github.com/20tab/UnrealEnginePython/blob/master/docs/Subclassing_API.md)
In the content browser click on 'add new' and choose 'blueprint class'
In the classes menu choose 'PyActor':

You now have a new asset, give it a meaningful name, and double click on it to start configuring it in the blueprint editor

On the right (in the 'Details' tab) you will find the Python section.
For now only 'Python Module' and 'Python Class' are meaningful.
Go to the Content directory of your project and create a directory named 'Scripts'. This is where all of your python modules will reside. With your favourite text editor create a new python module (like funnygameclasses.py), and define a new class into it:
import unreal_engine as ue
ue.log('Hello i am a Python module')
class Hero:
# this is called on game start
def begin_play(self):
ue.log('Begin Play on Hero class')
# this is called at every 'tick'
def tick(self, delta_time):
# get current location
location = self.uobject.get_actor_location()
# increase Z honouring delta_time
location.z += 100 * delta_time
# set new location
self.uobject.set_actor_location(location)
Now, go back to the blueprint editor and set 'funnygameclasses' in the 'Python Module' field, and 'Hero' in 'Python Class'
As you can see the actor will simply move over the z axis, but we need to give it some kind of visual representation to have a feedback in the scene. In the blueprint editor click on 'add component' and add some shape (a sphere, or a cube, or whatever you want). Save and Compile your blueprint.
Now you can drag the bluprint from the content browser to the scene and just click 'Play'.
You should see your actor moving along the 'z' axis at a speed of 1 meter per second
By default a 'begin_play' and a 'tick' method are expected (they will be automatically taken into account if found). In addition to them an 'automagic' system for defining event is available:
def on_actor_begin_overlap(self, me, other_actor):
pass
def on_actor_end_overlap(self, me, other_actor):
pass
def on_actor_hit(self, me, other_actor, normal_impulse, hit_result):
pass
...
Basically for each method startwing with 'on_' the related delegate/event is automatically configured (if available).
If you instead prefer to manually setup events, the following functions are exposed:
class Ball:
def begin_play(self):
self.uobject.bind_event('OnActorBeginOverlap', self.manage_overlap)
self.uobject.bind_action('Jump', ue.IE_PRESSED, self.uobject.jump)
self.uobject.bind_key('K', ue.IE_PRESSED, self.you_pressed_K)
self.uobject.bind_axis('MoveForward', self.move_forward)
def manage_overlap(self, me, other):
ue.print_string('overlapping ' + other.get_name())
def you_pressed_K(self):
ue.log_warning('you pressed K')
def move_forward(self, amount):
ue.print_string('axis value: ' + str(amount))
To allow seamless Python integration, each UObject of the engine is automatically mapped to a special Python Object (ue_PyUObject).
Whenever you want to access a UObject from python, you effectively get a reference to a ue_PyUObject exposing (via its methods) the features of the UObject (properties, functions, ....)
This special python object is cached into a c++ map in memory. (The key is the UObject pointer, the value is the ue_PyUObject pointer)
To be more clear, a call to:
text_render_component = unreal_engine.find_class('TextRenderComponent')
will internally search for the 'TextRenderComponent' class (via unreal c++ reflection) and when found will check if it is available in the cache, otherwise it will create a new ue_PyUObject object that will be pla
$ claude mcp add UnrealEnginePython \
-- python -m otcore.mcp_server <graph>