0
$\begingroup$

I run the following in Blender to receive coordinates over sockets, but the system hangs (the server is created, meaning the script runs, but then blender application tends to stop showing any output).

import socket,json,bpy

host = "127.0.0.1"
port = 1234
buffer_size = 1024

my_socket = socket.socket()
my_socket.bind( (host, port) )

my_socket.listen(1);        # listen indefinately
conn, addr = my_socket.accept()
print("Connected to client at :"+str(addr))

while True:
    data = conn.recv(buffer_size).decode()
    if not data:
        break
    data = json.loads(data)
    conn.send("ACK".encode())       # for synchronization
    bpy.data.objects['Sphere.005'].location = (data['x1_coord'],data['y1_coord'],0)

conn.close()
$\endgroup$
7
  • 2
    $\begingroup$ it is an infinite loop, you need to make it asynchronous or the only thing happening is the loop. $\endgroup$
    – rob
    Commented Jul 6, 2018 at 13:59
  • $\begingroup$ So a for loop would fix it ? $\endgroup$
    – lenix lobo
    Commented Jul 6, 2018 at 14:00
  • 1
    $\begingroup$ I have never tried to do this sort of programming in Python, but in many other languages you setup an event handler and each time the server receives data the event handler is called. at all other times everything just keeps running. I Googled asynchronous Python sockets and there appear to be a number ways to do this, but I am afraid you are on your own. $\endgroup$
    – rob
    Commented Jul 6, 2018 at 14:04
  • $\begingroup$ I think Python socket calls like recv() and accept() will block Blender unless you run the socket code asynchronously or on another thread. As rob pointed out, the while True loop by itself will freeze Blender as well. $\endgroup$
    – Tavi Kohn
    Commented Jul 6, 2018 at 17:18
  • $\begingroup$ But how exactly do I resolve this. ? Could you be abit more specific? $\endgroup$
    – lenix lobo
    Commented Jul 6, 2018 at 17:19

0

You must log in to answer this question.

Browse other questions tagged .