Skip to content

Commit 2e6edad

Browse files
author
Roberto De Ioris
committed
2 parents bb2a652 + 9df1993 commit 2e6edad

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tutorials/PlottingGraphsWithMatplotlibAndUnrealEnginePython.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,52 @@ We will use the third person template
2020

2121
## Installing the Python plugin
2222

23+
Ensure your project/editor has been closed.
24+
25+
Download the most recent embedded binary release from here: https://github.com/20tab/UnrealEnginePython/releases
26+
27+
You can use a non-embedded version if you already have python in your system and you are confident with it. The only difference in the tutorial is in where the matplotlib will be installed. For embedded version, the installation will happen in the plugin directory itself. For non-embedded it will be in the python system path. Obviously if your system python installation already includes matplotlib, you can simply skip the related paragraph below.
28+
29+
>Note: on Linux and Mac system, you will use the system installation (no embedded distributions are provided) or directly the source one. The rest of the tutorial will be platform independent.
30+
31+
Create a Plugins/ directory in your project and unzip the plugin archive into it.
32+
33+
Re-start your project and in the Edit/Plugins menu you will be able to enable the python plugin:
34+
2335
![Plugin Installation](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/PlottingGraphsWithMatplotlibAndUnrealEnginePython_Assets/enable_plugin.png)
2436

2537
## pip-installing matplotlib
2638

39+
If you installed the embedded distribution, just move to the Plugins/UnrealEnginePython/Binaries/Win64 directory in your terminal, and run:
40+
41+
```sh
42+
./python.exe -m pip install matplotlib
43+
```
44+
2745
![pip install matplotlib](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/PlottingGraphsWithMatplotlibAndUnrealEnginePython_Assets/pip_install.png)
2846

47+
If you are using a system python installation (or you are on Linux/Mac) just run:
48+
49+
```sh
50+
pip3 install matplotlib
51+
```
52+
53+
or (based on your config)
54+
55+
```sh
56+
pip install matplotlib
57+
```
58+
2959
## Testing matplotlib in Unreal Engine: generating a graph-texture
3060

61+
Time to check if all is working well.
62+
63+
We will start by generating a texture asset. The content of this texture will be generated by matplotlib.
64+
65+
You can use the included python editor to edit your scripts (it is under Wndow/Python Editor), or your favourite one SsublimeText, Vim, PyCharm...) just ensure scripts are under the Content/Scripts directory of your project.
66+
67+
The steps are generating a texture in memory, plotting the graph, transferring the graph data into texture memory, save it as an asset and opening the related editor:
68+
3169
```python
3270
import unreal_engine as ue
3371
# EPixelFormat defines the various pixel formats for a texture/image, we will use RGBA with 8bit per channel
@@ -71,6 +109,9 @@ ue.open_editor_for_asset(texture)
71109

72110
![Texture Created](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/PlottingGraphsWithMatplotlibAndUnrealEnginePython_Assets/texture_created.png)
73111

112+
113+
if you ended up with the texture editor opened on your new graph, you are ready for the next steps
114+
74115
## Our project: Plotting a Pie chart tracking overlap events
75116

76117
Our objective is to have a special blueprint in our level exposing 3 cube in 3 different colors.
@@ -95,7 +136,17 @@ We need 4 materials and 1 material instance for the project:
95136

96137
### The "pie chart carpet" blueprint
97138

139+
The first blueprint we are about to create, is the 'carpet' (we call it Graph_Blueprint)
140+
141+
![Graph Blueprint](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/PlottingGraphsWithMatplotlibAndUnrealEnginePython_Assets/graph_blueprint.png)
98142

143+
As you can see it is really simple:
144+
145+
* add a StaticMeshComponent (as root if you want) with a plane as the mesh and the material instance you created before
146+
* add a Python component with the module as 'plotter' and the class as 'PlotComponent' (this is where the matplotlib part will be)
147+
* add an event dispatcher called 'OnGraphDataUpdated' (this will be triggered whenever the character steps over a cube)
148+
149+
You can now create (again into Content/Scripts) the plotter.py python script
99150

100151
```python
101152
import unreal_engine as ue
@@ -133,8 +184,28 @@ class PlotComponent:
133184
self.texture.texture_set_data(self.fig.canvas.buffer_rgba())
134185
```
135186

187+
The only relevant part is the update_graph function that is triggered by the 'OnGraphDataUpdated' event dispatcher.
188+
189+
This function receives as the 'platform' argument the Actor triggering the event. This Actor (that we will create in the next phase), expose the counters of the overlapping cubes. We use that values to re-generate our pie chart and uploading it into texture memory whenever the event is triggered
190+
136191
### The "Plotter Platforms"
137192

193+
The 'PlotterPlatforms' blueprint, implements the actor containing the 3 cubes and manages the overlapping events as well as incrementing the 3 counters.
194+
195+
Let's start with the viewport, you need to add 3 static meshes, each with a cube and the related solid-color material we created earlier:
196+
197+
![PlotterPlatforms Blueprint Viewport](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/PlottingGraphsWithMatplotlibAndUnrealEnginePython_Assets/platforms_viewport.png)
198+
199+
then we define a blueprint function 'NotifyPlotter', that will trigger the event dispatcher on the Graph_Blueprint (our carpet):
200+
201+
![PlotterPlatforms Blueprint Notify Plotter](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/PlottingGraphsWithMatplotlibAndUnrealEnginePython_Assets/platforms_notify_plotter.png)
202+
203+
finally we have the Event Graph, here we simply increment the related counter variables and call the 'NotifyPlotter' function:
204+
205+
![PlotterPlatforms Blueprint Event Graph](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/PlottingGraphsWithMatplotlibAndUnrealEnginePython_Assets/platforms_event_graph.png)
206+
207+
Ensure to add the 3 Integer variables (check the lower-left side of the screenshots) for the counters !
208+
138209
### Playing it
139210

140211
### Writing a simple unit test

0 commit comments

Comments
 (0)