1
$\begingroup$

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')
$\endgroup$
  • $\begingroup$ have you tried using drivers or modal operators? $\endgroup$ – Tareyes Aug 11 '19 at 13:52
  • $\begingroup$ updated the client.py with your suggestion, but i'm running into an EOFError issue of pickle. $\endgroup$ – Manuel Aug 11 '19 at 14:55
  • $\begingroup$ Related blender.stackexchange.com/a/57087/15543 $\endgroup$ – batFINGER Aug 11 '19 at 15:52
  • $\begingroup$ tried both solutions, but i'm getting the same problem as i mentioned before with pickle. didn't think passing data through is such a big act ^^ $\endgroup$ – Manuel Aug 11 '19 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.