plotter.py 2.03 KB
Newer Older
Jan Kremer's avatar
Jan Kremer committed
1
import unreal_engine as ue
Jan Kremer's avatar
Jan Kremer committed
2
3
4
from unreal_engine.enums import EPixelFormat
import matplotlib
matplotlib.use('Agg')
Jan Kremer's avatar
Jan Kremer committed
5
import matplotlib.pyplot as plt
Jan Kremer's avatar
Jan Kremer committed
6
import numpy as np
Jan Kremer's avatar
Jan Kremer committed
7

Jan Kremer's avatar
Jan Kremer committed
8
9
10
11
class Hero:

    # this is called on game start
    def begin_play(self):    
Jan Kremer's avatar
Jan Kremer committed
12
13
14
        width = 1024
        height = 1024
        dpi = 72.0
Jan Kremer's avatar
Jan Kremer committed
15
16
        matplotlib.rcParams.update({'font.size': 32})
        matplotlib.rcParams.update({'axes.linewidth': 6})
Jan Kremer's avatar
Jan Kremer committed
17
18
19
20
        self.texture = ue.create_transient_texture(width, height, EPixelFormat.PF_R8G8B8A8)

        # self.uobject.get_owner().StaticMeshComponent.getMaterial(0).set_material_texture_parameter('Graph', self.texture)
        # self.uobject.get_owner().StaticMeshComponent.createDynamicMaterialInstance()
Jan Kremer's avatar
Jan Kremer committed
21
        self.uobject.get_owner().StaticMeshComponent.OverrideMaterials[0].set_material_texture_parameter('Graph', self.texture)
Jan Kremer's avatar
Jan Kremer committed
22
23
24
25
26
27
28
        # matinst = self.uobject.get_owner().StaticMeshComponent.CreateAndSetMaterialInstanceDynamic(0)
        # matinst.set_material_texture_parameter('Graph', self.texture)

        self.fig = plt.figure(1)
        self.fig.set_dpi(dpi)
        self.fig.set_figwidth(width/dpi)
        self.fig.set_figheight(height/dpi)
Jan Kremer's avatar
Jan Kremer committed
29
30
        self.uobject.get_owner().bind_event('OnRedraw', self.redraw)
        self.uobject.get_owner().bind_event('OnDrawPoint', self.draw_point)
Jan Kremer's avatar
Jan Kremer committed
31

Jan Kremer's avatar
Jan Kremer committed
32
33
34
35
36
    def redraw(self, x, y, c):       
         # clear the current plot data
        plt.clf()     
    
        plt.scatter(x, y, marker='o', c=c)
Jan Kremer's avatar
Jan Kremer committed
37

Jan Kremer's avatar
Jan Kremer committed
38
39
40
41
42
        bbox = plt.gca().get_position()
        [xmin, xmax] = plt.gca().get_xlim()
        [ymin, ymax] = plt.gca().get_ylim()
        [[x0, y0], [x1, y1]] = bbox.get_points()
        self.uobject.get_owner().call_function('UpdatePlotBounds', x0, y0, x1, y1, xmin, xmax, ymin, ymax)
Jan Kremer's avatar
Jan Kremer committed
43

Jan Kremer's avatar
Jan Kremer committed
44
45
46
47
48
49
50
        self.fig.canvas.draw()
        self.texture.texture_set_data(self.fig.canvas.buffer_rgba())
        
    def draw_point(self, x, y):
        ue.log('plotter draw point')
        #plt.clf()
        plt.plot(x, y, 'bo')
Jan Kremer's avatar
Jan Kremer committed
51
52
        self.fig.canvas.draw()
        self.texture.texture_set_data(self.fig.canvas.buffer_rgba())