UnrealEnginePython Tutorial

The following tutorial demonstrates the power of UnrealEnginePython to manipulate objects in the running game with Python, i.e. import unreal_engine. For more tutorials on how to use UnrealEnginePython (UEPy) see their docs.

Hello world

Here, we'll show how to print to the Unreal Logs, get the ego vehicle (your car), and move the ego vehicle around.

Find your sim binaries by opening up ~/Deepdrive and sorting by date

Find sim bin

Drill down to the Deepdrive binary

Drill down

Open a terminal, drag the file into the terminal, and press enter to open the sim in the terminal and see the logs.

Terminal open sim

Once the sim is open and you see the, press M to drive the car manually.

Within your binary folder, i.e. something like deepdrive-sim-linux-3.0.20190528000032 open your DeepDrive/Content/Scripts in your favorite editor and create a new Python file named move_car_tutorial.py and enter:

print('hello world')
Editor open

Now hit the backtick ` key to open the Unreal Console and enter

py.exec move_car_tutorial.py
Unreal Console Hello World

You should then see "hello world" printed to the logs in the terminal:

Terminal Hello World

Get and set the ego's position

Now you know how to run Python within Unreal Engine. Let's do something more interesting!

Paste the following into move_car_tutorial.py

import json
from api_methods import Api
api = Api()
ego = api.get_ego_agent()
location = ego.get_actor_location()
ego.set_actor_location(location.x, location.y, location.z + 1e4) # +100m

Now open the Unreal Console again in the simulator with ` and hit the up arrow to rerun the previous command which should be

py.exec move_car_tutorial.py

Happy flying!!!

Introspecting the Unreal API

Often when developing a UEPy script, you won't know the exact name of the object you want to manipulate. Some general methods for introspecting the games internal state are demonstrated with the following script

import json
from api_methods import Api, best_effort_serialize
api = Api()
ego = api.get_ego_agent()
print('Methods -------------------------------')
for m in dir(ego):
print(m)
print('Properties --------------------------- ')
print(json.dumps(best_effort_serialize(ego, levels=5), indent=2))

Example output

Here you can see the wealth of functionality and information UnrealEnginePython provides, including the get_actor_location and set_actor_location methods we've just demonstrated. Imagine learning the game's state information with the input remapping trick!