I want to send the changing outputs of a python script to Blender and display the results (moving Handjoints). Via Socket i achieved to send the data to blender, but i want to send it continuously. However Blender freezes, because of the infinite loop.
How can i bypass the infinite loop and send the data continuously?
Server.py
from socket import *
from time import ctime
from threading import Thread
import random
import pickle
class ClientHandler(Thread):
"""Handles a client request."""
def __init__(self, client):
Thread.__init__(self)
self._client = client
def run(self):
rand = random.uniform(1.0,5.0)
d = {1: rand, 2: 3.0}
msg = pickle.dumps(d)
self._client.send(msg)
self._client.close()
HOST = "localhost"
PORT = 15200 # Port number was changed here
BUFSIZE = 1024
ADDRESS = ('', PORT)
server = socket(AF_INET, SOCK_STREAM)
server.bind(ADDRESS)
server.listen(5)
# The server now just waits for connections from clients
# and hands sockets off to client handlers
while True:
print('Waiting for connection')
client, address = server.accept()
print('...connected from:', address)
handler = ClientHandler(client)
handler.start()
Client.py
import bpy
import time
import socket
import pickle
class ModalOperator(bpy.types.Operator):
bl_idname = "object.modal_operator"
bl_label = "Simple Modal Operator"
timer = None
def modal(self, context, event):
print("HELP")
if event.type == "TIMER":
msg = self.s.recv(1024)
try:
d = pickle.loads(msg)
print(d)
except EOFError:
print("Raising Error")
#print(pickle.loads(msg))
return {'PASS_THROUGH'}
def invoke(self, context, event):
self.timer = context.window_manager.event_timer_add(0.1, window=context.window)
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect(('localhost', 15200))
context.window_manager.modal_handler_add(self)
print("INVOKE")
return {'RUNNING_MODAL'}
if __name__ == "__main__":
bpy.utils.register_class(ModalOperator)
bpy.ops.object.modal_operator('INVOKE_DEFAULT')