-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
268 lines (233 loc) · 9.35 KB
/
__init__.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import bpy, http, threading, pprint, types, os
import http.server
import socketserver
from mathutils import Euler
from bpy.props import *
from bpy.types import Operator
from .websocket_server import websocket_server
bl_info = {
"name": "Remote 3D Navigation ",
"author": "Laxminarayan Kamath <[email protected]>",
"version": (1, 2),
"blender": (2, 57, 0),
"location": "View3D > Tool Shelf > 3D Remote",
"description": "Navigate the Camera & 3D View from any html5 capable device with accelerometer",
"warning": "Uses Multi-Threading! Toy add-on!! Do Not use on production blender!",
"wiki_url": "",
"tracker_url": "",
"category": "3D View"}
rpcserver_localvars=types.ModuleType('main').__dict__
rpcserver_globalvars=globals()
rpcserver = None
tcpserver = None
wstserver = None
commands = {}
def rotate_view(x_angle,y_angle,z_angle):
x_angle = float(x_angle)
y_angle = float(y_angle)
z_angle = float(z_angle)
try:
for view in rpcserver_localvars['views2control']:
view.region_3d.view_rotation.rotate(Euler((x_angle,y_angle,z_angle)))
return True
except Exception as E:
return pprint.pformat(E)
def move_view(x_distance,y_distance,z_distance):
x_distance = float(x_distance)
y_distance = float(y_distance)
z_distance = float(z_distance)
print ('distances {0}_{1}_{2}'.format(x_distance, y_distance, z_distance))
try:
for view in rpcserver_localvars['views2control']:
view.region_3d.view_location += view.region_3d.view_location.__class__((x_distance, y_distance, z_distance))
#view.region_3d.view_location.x+=x_distance
#view.region_3d.view_location.y+=y_distance
#view.region_3d.view_location.z+=z_distance
return True
except Exception as E:
return pprint.pformat(E)
def move_camera(x_distance,y_distance,z_distance):
x_distance = float(x_distance)
y_distance = float(y_distance)
z_distance = float(z_distance)
try:
for view in rpcserver_localvars['views2control']:
view.camera.location+=view.camera.location.__class__((x_distance,y_distance,z_distance))
return True
except Exception as E:
return pprint.pformat(E)
def rotate_camera(x_angle,y_angle,z_angle):
x_angle = float(x_angle)
y_angle = float(y_angle)
z_angle = float(z_angle)
try:
for view in rpcserver_localvars['views2control']:
view.camera.rotation_euler.x+=x_angle
view.camera.rotation_euler.y+=y_angle
view.camera.rotation_euler.z+=z_angle
return True
except Exception as E:
return pprint.pformat(E)
def rotateabsolute_camera(x_angle,y_angle,z_angle):
x_angle = float(x_angle)
y_angle = float(y_angle)
z_angle = float(z_angle)
try:
for view in rpcserver_localvars['views2control']:
view.camera.rotation_euler = view.camera.rotation_euler.__class__((x_angle,y_angle,z_angle))
return True
except Exception as E:
return pprint.pformat(E)
def move_selection(x_distance,y_distance,z_distance):
x_distance = float(x_distance)
y_distance = float(y_distance)
z_distance = float(z_distance)
try:
for scene in rpcserver_localvars['scenes2control']:
scene.objects.active.location+=scene.objects.active.location.__class__(x_distance,y_distance,z_distance)
return True
except Exception as E:
return pprint.pformat(E)
def rotate_selection(x_angle,y_angle,z_angle):
x_angle = float(x_angle)
y_angle = float(y_angle)
z_angle = float(z_angle)
try:
for scene in rpcserver_localvars['scenes2control']:
scene.objects.active.rotation_euler.x+=x_angle
scene.objects.active.rotation_euler.y+=y_angle
scene.objects.active.rotation_euler.z+=z_angle
return True
except Exception as E:
return pprint.pformat(E)
def rotateabsolute_selection(x_angle,y_angle,z_angle):
x_angle = float(x_angle)
y_angle = float(y_angle)
z_angle = float(z_angle)
try:
for scene in rpcserver_localvars['scenes2control']:
scene.objects.active.rotation_euler = scene.objects.active.rotation_euler.__class__((x_angle,y_angle,z_angle))
return True
except Exception as E:
return pprint.pformat(E)
def handle_wscmdline(client,cmdline):
if (str(cmdline) == 'PING'):
wstserver.send_message(client,'PONG')
else:
handle_cmdline(cmdline)
def handle_cmdline(cmdline):
# space = bytes(' ','ascii')
print (cmdline)
try:
if (cmdline):
params = str(cmdline).strip().split(' ')
command=params.pop(0)
commands[command](*params)
except KeyError:
print ("Warning: Unimplemented command called : {0}".format(command))
except Exception as e:
pprint.pprint (e)
class TCPCommandHandler(socketserver.StreamRequestHandler):
def handle(self):
while True:
cmdline = self.rfile.readline()
if (cmdline):
handle_cmdline(cmdline)
else:
break
class ServerThread(threading.Thread):
def __init__(self,server):
super(ServerThread,self).__init__()
self.server = server
self.rpcserver_localvars = rpcserver_localvars
self.rpcserver_globalvars = rpcserver_globalvars
def run(self):
self.server.serve_forever()
class VIEW3D_PT_RemotePanel(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_label = "Remote Nav"
def draw(self, context):
self.layout.prop(context.window_manager,'view_subscribed_to_remote')
self.layout.prop(context.scene,'scene_subscribed_to_remote')
def get_view_subscription(self):
global rpcserver_localvars
return bpy.context.area.spaces[0] in rpcserver_localvars['views2control']
def set_view_subscription(self,value):
global rpcserver_localvars
if value and not get_view_subscription(self):
rpcserver_localvars['views2control'].append(bpy.context.area.spaces[0])
if not value and get_view_subscription(self):
rpcserver_localvars['views2control'].remove(bpy.context.area.spaces[0])
def get_scene_subscription(self):
global rpcserver_localvars
return bpy.context.scene in rpcserver_localvars['scenes2control']
def set_scene_subscription(self,value):
global rpcserver_localvars
if value and not get_scene_subscription(self):
rpcserver_localvars['scenes2control'].append(bpy.context.scene)
if not value and get_scene_subscription(self):
rpcserver_localvars['scenes2control'].remove(bpy.context.scene)
def register():
global rpcserver
global tcpserver
global wstserver
global rpcserver_localvars
global rpcserver_globalvars
global commands
commands[b'rotateabsolute_camera'] = rotateabsolute_camera
commands[b'rotate_camera'] = rotate_camera
commands[b'move_camera'] = move_camera
commands[b'rotate_view'] = rotate_view
commands[b'move_view'] = move_view
commands['rotateabsolute_camera'] = rotateabsolute_camera
commands['rotate_camera'] = rotate_camera
commands['rotate_selection'] = rotate_selection
commands['move_camera'] = move_camera
commands['rotate_view'] = rotate_view
commands['move_view'] = move_view
def debug(*args):
print (args)
commands['debug'] = debug
bpy.types.WindowManager.view_subscribed_to_remote = BoolProperty(
name="Control View and camera",
description="Subscribe current view to the remote",
default=0,
subtype="UNSIGNED",
get = get_view_subscription,
set = set_view_subscription)
bpy.types.Scene.scene_subscribed_to_remote = BoolProperty(
name="Control Selection ",
description="Subscribe current view to the remote",
default=0,
subtype="UNSIGNED",
get = get_scene_subscription,
set = set_scene_subscription)
bpy.utils.register_module(__name__)
if (not rpcserver):
os.chdir(os.path.dirname(__file__))
rpcserver = http.server.HTTPServer(('0.0.0.0',9000), http.server.SimpleHTTPRequestHandler)
tcpserver = socketserver.ThreadingTCPServer(('0.0.0.0',9002),TCPCommandHandler)
wstserver = websocket_server.WebsocketServer(9001,'0.0.0.0')
wstserver.set_fn_message_received(lambda client,server,message: handle_wscmdline(client,message))
rpcserver_globalvars=globals()
rpcserver_localvars['views2control']=[]
rpcserver_localvars['scenes2control']=[]
rpcserver_thread=ServerThread(rpcserver)
rpcserver_thread.daemon = False
rpcserver_thread.start()
tcpserver_thread=ServerThread(tcpserver)
tcpserver_thread.daemon = False
tcpserver_thread.start()
wstserver_thread=ServerThread(wstserver)
wstserver_thread.daemon = False
wstserver_thread.start()
def unregister():
print("stopping rpcserver")
global rpcserver
global tcpserver
rpcserver.socket.close()
rpcserver.shutdown()
tcpserver.socket.close()
tcpserver.server_close()
bpy.utils.unregister_module(__name__)