-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblender_server.py
63 lines (49 loc) · 1.9 KB
/
blender_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import socket
import sys
import bpy
HOST = '127.0.0.1'
PORT = 65432
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
# listen() marks the socket referred to by sockfd as a passive socket (awaits for an incoming connection,
# which will spawn a new active socket once a connection is established), that is, as a socket that
# will be used to accept incoming connection requests using accept(2).
s.listen()
# Extracts the first connection request on the queue of pending connections for the listening socket,
# sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket.
# The newly created socket is not in the listening state. The original socket sockfd is unaffected by
# this call.
conn, addr = s.accept()
conn.settimeout(0.0)
print("New client: ", addr)
def handle_data():
interval = 0.5
data = None
# In non-blocking mode blocking operations error out with OS specific errors.
# https://docs.python.org/3/library/socket.html#notes-on-socket-timeouts
try:
data = conn.recv(1024)
except:
pass
if not data:
pass
else:
print("Received data: " + data.decode("utf-8"))
# Fetch the 'Sockets' collection or create one. Anything created via sockets will be linked
# to that collection.
collection = None
try:
collection = bpy.data.collections["Sockets"]
except:
collection = bpy.data.collections.new("Sockets")
bpy.context.scene.collection.children.link(collection)
try:
imported_object = bpy.ops.import_scene.obj(filepath=data.decode("utf-8"), filter_glob="*.obj;*.OBJ;*.mtl")
conn.sendall(b"ack_")
except:
conn.sendall(b"nack")
if "quit" in data.decode("utf-8"):
conn.close()
s.close()
return interval
bpy.app.timers.register(handle_data)