Communication entre Blender et Qt5

This commit is contained in:
Philippe Roy 2023-01-15 10:05:41 +01:00
parent e7cd074cd7
commit 9dba4ac24e
4 changed files with 31 additions and 15 deletions

View File

@ -106,6 +106,7 @@ def stop():
if scene.objects['System']['twins']:
serial_close(scene.objects['System']['board'])
time.sleep(1)
scene.objects['System']['plot']=False
thread_cmd_stop()
# Fin naturelle
@ -113,6 +114,7 @@ def end():
if scene.objects['System']['twins']:
serial_close(scene.objects['System']['board'])
time.sleep(1)
scene.objects['System']['plot']=False
thread_cmd_end()
def fin():
@ -125,7 +127,19 @@ def quit():
# Visualisation des données
###############################################################################
# Grapheur
# Lancement du grapheur
def plot():
# subprocess.run([sys.executable, os.path.join(os.getcwd(), "twin_plot.py")]) # Process bloquant
subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot.py")])
# subprocess.run([sys.executable, os.path.join(os.getcwd(), "twin_plot.py")], , stdin=subprocess.PIPE) # Process bloquant
scene.objects['System']['plot_proc'] = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot.py")], stdin=subprocess.PIPE, encoding = 'utf8')
scene.objects['System']['plot']=True
# Envoi des données (Pipe)
def plot_maj(cont):
if cont.sensors['Plot'].positive :
time_send = str(scene.objects['System']['plot_time'])+'\n'
if scene.objects['System']['plot_proc'].poll()==None:
# scene.objects['System']['plot_proc'].communicate(input=time_send.encode())[0] # Bloquant
scene.objects['System']['plot_proc'].stdin.write(time_send)
else:
print ("Stop")
scene.objects['System']['plot']=False

View File

@ -1,7 +1,7 @@
<data>
<screen>
<width>1325</width>
<height>745</height>
<width>1609</width>
<height>905</height>
<quality>1</quality>
</screen>
</data>

View File

@ -1,12 +1,8 @@
# import bge # Bibliothèque Blender Game Engine (UPBGE)
# from twin_threading import thread_plot_start, thread_plot_end # Multithreading
import sys
import random
import matplotlib
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
from PyQt5 import QtCore, QtGui, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
@ -38,7 +34,6 @@ class MplCanvas(FigureCanvasQTAgg):
self.axes = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
class MainWindow(QtWidgets.QMainWindow):
# Création du graphique
@ -65,22 +60,29 @@ class MainWindow(QtWidgets.QMainWindow):
# Données
# donnees = ([0,1,2,3,4], [10,1,20,3,40])
print (self.xdata[len(self.xdata)-1]+1)
# print (self.xdata[len(self.xdata)-1]+1)
new_x=self.xdata[len(self.xdata)-1]+1
self.xdata.append(new_x)
self.ydata.append(random.randint(0, 10))
# self.ydata = self.ydata + [random.randint(0, 10)]
# Lecture du Pipe
for line in sys.stdin:
print('Output:', line.rstrip())
break
# self.ydata = self.ydata + [random.randint(0, 10)]
# Drop off the first y element, append a new one.
# self.ydata = self.ydata[1:] + [random.randint(0, 10)]
self.canvas.axes.cla() # Clear the canvas.
# Redraw
self.canvas.axes.cla()
self.canvas.axes.plot(self.xdata, self.ydata, 'r')
# Trigger the canvas to update and redraw.
self.canvas.draw()
###############################################################################
# Application
###############################################################################
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()