Communication entre Blender et Qt5 (point d'étape)

This commit is contained in:
Philippe Roy 2023-01-17 01:24:21 +01:00
parent 618c8250b8
commit 70125297bf
4 changed files with 107 additions and 36 deletions

View File

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

View File

@ -53,43 +53,53 @@ def plot(data):
scene.objects['System']['plot_data'].append(plot_config[obj][1][0]) scene.objects['System']['plot_data'].append(plot_config[obj][1][0])
# Démarrer le processus # Démarrer le processus
scene.objects['System']['plot_proc'] = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot_qt.py")], stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding = 'utf8') scene.objects['System']['plot_proc'] = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot_qt.py")], stdin=subprocess.PIPE, encoding = 'utf8', universal_newlines=True)
stout, sterr = scene.objects['System']['plot_proc'].communicate() # FIXME : attente du message retour pour lancer l'acquisition # scene.objects['System']['plot_proc'] = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot_qt.py")], stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding = 'utf8')
# stout = scene.objects['System']['plot_proc'].communicate() # FIXME : attente du message retour pour lancer l'acquisition
# print("Blender stout : ", stout)
scene.objects['System']['plot']=True scene.objects['System']['plot']=True
############################################################################### ###############################################################################
# Mise à jour du graphique # Mise à jour du graphique
############################################################################### ###############################################################################
def truncate(n, decimals=0):
multiplier = 10**decimals
return int(n* multiplier)/multiplier
def plot_maj(cont): def plot_maj(cont):
if cont.sensors['Plot'].positive : if cont.sensors['Plot'].positive :
# Affichage du graphique # Affichage du graphique
if scene.objects['System']['plot_draw']: if scene.objects['System']['plot_draw']:
if scene.objects['System']['plot_ticktime'] != truncate(scene.objects['System']['plot_time'], 0) or True:
# Préparation du message # Préparation du message
# FIXME : ajouter les valeurs réelles et valeurs numériques ('activated_real') # FIXME : ajouter les valeurs réelles et valeurs numériques ('activated_real')
# msg=str(round(scene.objects['System']['plot_time'], 2)) scene.objects['System']['plot_ticktime'] = truncate(scene.objects['System']['plot_time'], 0)
msg=format(scene.objects['System']['plot_time'],".2f") # msg=str(round(scene.objects['System']['plot_time'], 1))
for obj in scene.objects['System']['plot_data']: # msg=format(scene.objects['System']['plot_time'],".2f")
if scene.objects[obj]['activated']: msg=format(scene.objects['System']['plot_ticktime'],".2f")
msg = msg+",1" for obj in scene.objects['System']['plot_data']:
if scene.objects[obj]['activated']:
msg = msg+",1"
else:
msg = msg+",0"
msg = msg+"\n"
# Envoi (Pipe)
if scene.objects['System']['plot_proc'].poll()==None:
# scene.objects['System']['plot_proc'].communicate(input=time_send.encode())[0] # Communication bloquante
print ("twin : ", msg)
scene.objects['System']['plot_proc'].stdin.write(msg)
else: else:
msg = msg+",0" # Arrêt
msg = msg+"\n" print ("Stop")
scene.objects['System']['plot']=False
scene.objects['System']['plot_draw'] =False
scene.objects['System']['plot_proc'].terminate()
# Envoi (Pipe) # Arrêt de l'affichage du graphique
if scene.objects['System']['plot_proc'].poll()==None:
# scene.objects['System']['plot_proc'].communicate(input=time_send.encode())[0] # Communication bloquante
scene.objects['System']['plot_proc'].stdin.write(msg)
else:
print ("Stop")
scene.objects['System']['plot']=False
scene.objects['System']['plot_draw'] =False
scene.objects['System']['plot_proc'].terminate()
# Arret de l'affichage du graphique
else :
if scene.objects['System']['plot_proc'].poll()==None: if scene.objects['System']['plot_proc'].poll()==None:
pass pass
else: else:

View File

@ -50,11 +50,13 @@ class MainWindow(QtWidgets.QMainWindow):
self.show() self.show()
# Timer pour le update_plot # Timer pour le update_plot
fps = 60 fps = 120 # Blender est cadencé à 60 fps
self.timer = QtCore.QTimer() self.timer = QtCore.QTimer()
self.timer.setInterval(1000/fps) # self.timer.setInterval(int(1000/fps))
self.timer.setInterval(1)
self.timer.timeout.connect(self.update_plot) self.timer.timeout.connect(self.update_plot)
self.timer.start() self.timer.start()
print("Qt : Canvas ready\n")
# Lecture des données et mise à jour du graphique # Lecture des données et mise à jour du graphique
def update_plot(self): def update_plot(self):
@ -62,26 +64,85 @@ class MainWindow(QtWidgets.QMainWindow):
# Données # Données
# donnees = ([0,1,2,3,4], [10,1,20,3,40]) # Essai
# print (self.xdata[len(self.xdata)-1]+1)
# new_x=self.xdata[len(self.xdata)-1]+1 # new_x=self.xdata[len(self.xdata)-1]+1
# self.xdata.append(new_x) # self.xdata.append(new_x)
# self.ydata.append(random.randint(0, 10)) # self.ydata.append(random.randint(0, 10))
# Lecture du Pipe # Lecture du Pipe simple
msg="" msg=""
# print (sys.stdin)
# contents = sys.stdin.read(1)
# # contents = sys.stdin.buffer
# print ("contents :", contents)
for line in sys.stdin: for line in sys.stdin:
msg = line.rstrip() msg = line.rstrip()
break break
print(msg)
# X et Y # X et Y
# FIXME : temps et une valeur # FIXME : temps et une valeur
msg_list=msg.split(',') # msg_list=msg.split(',')
print(msg_list) # print(msg_list)
if msg_list[0] != "": # if msg_list[0] != "":
self.xdata = self.xdata + [float(msg_list[0])] # self.xdata = self.xdata + [float(msg_list[0])]
self.ydata = self.ydata + [float(msg_list[1])] # self.ydata = self.ydata + [float(msg_list[1])]
# # Lecture du Pipe
# # i=0
# # lines = sys.stdin.readlines()
# # print (lines)
# # print ("b")
# msg_line=""
# # msg_lines=[]
# for line in sys.stdin:
# # i+=1
# # print (i)
# msg_line = line.rstrip()
# # msg_lines.append(msg_line)
# # print("Qt msg_lines :", msg)
# # if i==2:
# # break
# break
# print("Qt :", msg_line)
# # print("Qt msg_lines :", msg_lines)
# # X et Y
# # FIXME : temps msg_list[0] et une seule valeur msg_list[1]
# msg_list=msg_line.split(',')
# # print(msg_list)
# if msg_list[0] != "":
# self.xdata = self.xdata + [float(msg_list[0])]
# self.ydata = self.ydata + [float(msg_list[1])]
# for line in msg_lines:
# msg_list=line.split(',')
# # print(msg_list)
# if msg_list[0] != "":
# self.xdata = self.xdata + [float(msg_list[0])]
# self.ydata = self.ydata + [float(msg_list[1])]
# for line in sys.stdin:
# msg_list=msg.split(',')
# print(msg_list)
# if msg_list[0] != "":
# self.xdata = self.xdata + [float(msg_list[0])]
# self.ydata = self.ydata + [float(msg_list[1])]
# self.ydata = self.ydata + [random.randint(0, 10)] # self.ydata = self.ydata + [random.randint(0, 10)]
# Drop off the first y element, append a new one. # Drop off the first y element, append a new one.