-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblender_server.py
135 lines (106 loc) · 3.88 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import bpy
from mathutils import *
import math
import socket
import threading
import queue
from time import sleep
running = True
Q = queue.Queue()
class Server:
def __init__(self, host="", port=10001):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# self.socket.setblocking(False)
self.socket.bind((host,port))
print(self.socket.getsockname())
def receive(self):
global running
while running:
try:
data, addr = self.socket.recvfrom(1024)
print('Am primit')
print(data)
Q.put(data.decode())
except socket.error:
print(socket.error)
continue
self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close()
def execute(self):
self.thread = threading.Thread(target=self.receive)
self.thread.start()
class Transformer():
def _execute(self):
rover = bpy.data.objects["Cube"]
angle = math.radians(0.5)
x_pos_rot = Euler((angle, 0, 0))
y_pos_rot = Euler((0, angle, 0))
z_pos_rot = Euler((0, 0, angle))
x_neg_rot = Euler((-angle, 0, 0))
y_neg_rot = Euler((0, -angle, 0))
z_neg_rot = Euler((0, 0, -angle))
scale_factor = 1.01
delta_loc = 0.1
global running
while running:
command = Q.get()
print(command)
# Object rotations
if command == "X_UP":
rover.rotation_euler.rotate(x_pos_rot)
if command == "X_DOWN":
rover.rotation_euler.rotate(x_neg_rot)
if command == "Y_LEFT":
rover.rotation_euler.rotate(y_neg_rot)
if command == "Y_RIGHT":
rover.rotation_euler.rotate(y_pos_rot)
if command == "Z_FRONT":
rover.rotation_euler.rotate(z_pos_rot)
if command == "Z_BACK":
rover.rotation_euler.rotate(z_neg_rot)
# Object translations
if command == "RIGHT":
rover.location += Vector((0, delta_loc, 0))
if command == "LEFT":
rover.location += Vector((0, -delta_loc, 0))
if command == "FRONT":
rover.location += Vector((delta_loc, 0, 0))
if command == "BACK":
rover.location += Vector((-delta_loc, 0, 0))
if command == "UP":
rover.location += Vector((0, 0, delta_loc))
if command == "BACK":
rover.location += Vector((0, 0, -delta_loc))
# Object all axis scale
if command == "SCALE_UP":
rover.scale *= scale_factor
if command == "SCALE_DOWN":
rover.scale /= scale_factor
# Position, Orientation and Scale Reset
if command == "RESET":
rover.scale *= 0
rover.location *= 0
rover.rotation_euler = Euler((0,0,0))
# Command that closes the communication
# and shuts down the server
if command == "SHUTDOWN":
running = False
def execute(self):
self.thread = threading.Thread(target=self._execute)
self.thread.start()
def register():
bpy.types.Scene.watcher_running = bpy.props.BoolProperty(default=False)
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
del bpy.types.Scene.watcher_running
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == '__main__':
server = Server()
server.execute()
trans = Transformer()
trans.execute()
# server.thread.join()
# trans.thread.join()
# print("FINISH SCRIPT")