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$
4
  • $\begingroup$ have you tried using drivers or modal operators? $\endgroup$
    – Tareyes
    Commented Aug 11, 2019 at 13:52
  • $\begingroup$ updated the client.py with your suggestion, but i'm running into an EOFError issue of pickle. $\endgroup$
    – Manuel
    Commented Aug 11, 2019 at 14:55
  • $\begingroup$ Related blender.stackexchange.com/a/57087/15543 $\endgroup$
    – batFINGER
    Commented Aug 11, 2019 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
    Commented Aug 11, 2019 at 20:51

0

You must log in to answer this question.

Browse other questions tagged .