old.py 2.26 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

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

    # 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

32
33
34
35
36
        self.fig.canvas.draw()
        array=self.fig.canvas.buffer_rgba()
        print(type(array))
        self.texture.texture_set_data(array)

Jan Kremer's avatar
Jan Kremer committed
37
38
39
40
41
    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
42

Jan Kremer's avatar
Jan Kremer committed
43
44
45
46
47
        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
48

Jan Kremer's avatar
Jan Kremer committed
49
        self.fig.canvas.draw()
50
51
52
        array=self.fig.canvas.buffer_rgba()
        print(type(array))
        self.texture.texture_set_data(array)
Jan Kremer's avatar
Jan Kremer committed
53
54
55
56
57
        
    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
58
        self.fig.canvas.draw()
59
60
61
        array=self.fig.canvas.buffer_rgba()
        print(type(array))
        self.texture.texture_set_data(array)