forked from arinik9/BlenderBQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblender.py
366 lines (302 loc) · 10.9 KB
/
blender.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import sys
sys.path.append('.')
import bpy
import bgl
import socket
import json
import math
import logging
import mathutils
def blendPos(dim):
return dim / 50.0
def read_command(transport):
try:
line = transport.readline()
if not line:
return
data = json.loads(line)
except ValueError as e:
logging.exception(e)
raise IOError()
try:
cmd = data['__cmd__']
del data['__cmd__']
except KeyError as e:
logging.exception(e)
raise IOError()
return cmd, data
class BBQOperator(bpy.types.Operator):
bl_idname = "object.bbq"
bl_label = "BBQ Operator"
# TODO use Blender's Properties
# sock_addr = bpy.props.StringProperty(name="Server address")
def __init__(self):
print("Starting")
self.transport = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# self.transport.setblocking(False)
self.sockfile = None
self.move_origin = mathutils.Vector((0, 0, 0))
self.rotate_origin = mathutils.Euler((0, 0, 0))
self.moving = False
self.move_lock = None
self.scale_origin = mathutils.Vector((1, 1, 1))
self.noob = False
# rotation (pottery mode)
self.continuous_speed = 0
self.rotation_level = 0
self.max_rotation_level = 5.
self.rotation_inc = 0.02
_commands = [
self.mode_sculpt,
self.mode_object,
self.mode_texture_paint,
self.mode_edit,
self.view_top,
self.view_bottom,
self.view_left,
self.view_right,
self.view_front,
self.view_back,
self.view_camera,
self.render,
self.object_move_origin,
self.object_move,
self.object_move_end,
self.object_rotate_origin,
self.object_rotate,
self.object_rotate_end,
self.object_scale_origin,
self.object_scale,
self.object_center,
self.set_continuous_rotation,
self.do_rotation_left,
self.do_rotation_right,
self.stop_rotation,
self.my_little_swinging_vase,
self.paint_color,
self.finger_touch,
self.sculpt_add,
self.sculpt_subtract,
self.object_reset_everything,
self.toggle_noob,
]
self.commands = {f.__name__: f for f in _commands}
self._timer = None
self.current_mode = 'OBJECT'
def __del__(self):
print("Ending")
self.transport.close()
if self.sockfile:
self.sockfile.close()
print("End")
@classmethod
def poll(cls, context):
return context.area.type == 'VIEW_3D' # Only enable in 3d view
def execute(self, context):
return {'RUNNING_MODAL'}
def modal(self, context, event):
if event.type == 'ESC':
context.window_manager.event_timer_remove(self._timer)
return {'FINISHED'}
if event.type == 'A':
self.sculpt_add()
if event.type == 'S':
self.sculpt_subtract()
if self.moving:
if event.type == 'X':
self.move_lock = 'X'
if event.type == 'Y':
self.move_lock = 'Y'
if event.type == 'Z':
self.move_lock = 'Z'
if event.type == 'TIMER':
self.my_little_swinging_vase()
try:
cmd = read_command(self.sockfile)
except IOError as e:
logging.exception(e)
else:
if cmd:
func, kwargs = cmd
print(func, kwargs)
if func in self.commands:
self.commands[func](**kwargs)
return {'RUNNING_MODAL'}
def invoke(self, context, event):
try:
self.x, self.y, self.z = 0, 0, 0
self.transport.connect(('', 1337))
self.transport.setblocking(False)
self.sockfile = self.transport.makefile()
except IOError as e:
logging.exception(e)
return {'CANCELLED'}
print("Started")
self._timer = context.window_manager.event_timer_add(0.01, context.window)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
# return context.window_manager.invoke_props_dialog(self)
def set_cursor(self, x, y, z):
bpy.data.objects['cursor'].location = x, y, z
def my_little_swinging_vase(self, **kwargs):
for o in bpy.context.selected_objects:
angle = o.rotation_euler.copy()
angle.z += self.continuous_speed
o.rotation_euler = angle
def set_continuous_rotation(self, direction):
self.rotation_level = max(-self.max_rotation_level, min(self.rotation_level + direction, self.max_rotation_level))
print('Rotation level', self.rotation_level)
self.continuous_speed = float(self.rotation_level) * self.rotation_inc
def do_rotation_left(self):
self.set_continuous_rotation(1)
def do_rotation_right(self):
self.set_continuous_rotation(-1)
def stop_rotation(self):
self.continuous_speed = 0
self.rotation_level = 0
def sculpt_add(self):
bpy.data.brushes['SculptDraw'].direction = 'ADD'
def sculpt_subtract(self):
bpy.data.brushes['SculptDraw'].direction = 'SUBTRACT'
def finger_touch(self, **kwargs):
if self.current_mode == 'VERTEX_PAINT':
return
x, y, z = kwargs['x'], kwargs['y'], kwargs['z']
vx, vy, vz = kwargs['vx'], kwargs['vy'], kwargs['vz']
dist = 0.42 # magic number
x, y, z = self.foo(x, y, z)
x2, y2, z2 = x + vx * dist, y + vy * dist, z + vz * dist
# x2, y2, z2 = self.foo(x2, y2, z2)
self.x, self.y, self.z = x, y, z
p1 = { 'name': 'dummy_foo',
'is_start': True,
'location': (x, y, z),
'mouse': (0, 0),
'pressure': 1.0,
'pen_flip': False,
'time': 1.0 }
p2 = { 'name': 'dummy_bar',
'is_start': False,
'location': (x2, y2, z2),
'mouse': (0, 0),
'pressure': 1.0,
'pen_flip': False,
'time': 1.0 }
print(self.current_mode)
if self.current_mode == 'SCULPT':
bpy.ops.sculpt.brush_stroke(stroke=[p1, p2])
elif self.current_mode == 'VERTEX_PAINT':
bpy.ops.paint.vertex_paint(stroke=[p1, p2])
print('Painting vertex')
self.set_cursor(x, y, z)
def paint_color(self, **kwargs):
r, g, b = kwargs['r'], kwargs['g'], kwargs['b']
# bpy.data.brushes['TexDraw'].color = r, g, b
bpy.data.materials['Cylindre'].diffuse_color = r, g, b
bpy.data.materials['Cursor'].diffuse_color = 1 - r, 1 - g, 1 - b
def foo(self, x, y, z):
bbox = bpy.context.selected_objects[0].bound_box
xmin = min(pos[0] for pos in bbox)
ymin = min(pos[1] for pos in bbox)
zmin = min(pos[2] for pos in bbox)
xmax = max(pos[0] for pos in bbox)
ymax = max(pos[1] for pos in bbox)
zmax = max(pos[2] for pos in bbox)
dx = xmax - xmin
dy = ymax - ymin
dz = zmax - zmin
def bar(p, d, t, m):
return (p + 1) / 2.0 * d * (1 + t * 2) + m - d * t
# TODO x est toujours constant après transfo. Jeune homme allez i
t = 0.8
x_ = bar(x, dx, t, xmin)
y_ = bar(y, dy, t, ymin)
z_ = bar(z, dz, t, zmin)
z_ -= dz * 0.42
return x_, y_, z_
def mode_set(self, mode):
if bpy.context.area.type == 'VIEW_3D':
bpy.ops.object.mode_set(mode=mode)
self.current_mode = mode
def mode_sculpt(self):
self.mode_set('SCULPT')
def mode_object(self):
self.mode_set('OBJECT')
def mode_texture_paint(self):
self.mode_set('OBJECT')
def mode_edit(self):
self.mode_set('EDIT')
def view_numpad(self, view):
if bpy.context.area.type == 'VIEW_3D':
bpy.ops.view3d.viewnumpad(type=view)
def view_top(self):
self.view_numpad('TOP')
def view_bottom(self):
self.view_numpad('BOTTOM')
def view_left(self):
self.view_numpad('LEFT')
def view_right(self):
self.view_numpad('RIGHT')
def view_front(self):
self.view_numpad('FRONT')
def view_back(self):
self.view_numpad('BACK')
def view_camera(self):
self.view_numpad('CAMERA')
def object_move_origin(self):
self.move_origin = bpy.context.selected_objects[0].location.copy()
self.moving = True
self.move_matrix_origin = \
bpy.context.area.spaces[0].region_3d.view_matrix
def object_move_end(self):
self.moving = False
self.move_lock = None
def object_move(self, **kwargs):
dx, dy, dz = kwargs['loc_x'], -kwargs['loc_z'], kwargs['loc_y']
if self.move_lock == 'X':
dy, dz = 0, 0
if self.move_lock == 'Y':
dx, dz = 0, 0
if self.move_lock == 'Z':
dx, dy = 0, 0
dx, dy, dz = map(blendPos, [dx, dy, dz])
for o in bpy.context.selected_objects:
o.location = self.move_origin + mathutils.Vector((dx, dy, dz))
pass
def object_rotate_origin(self):
self.rotate_origin = \
bpy.context.selected_objects[0].rotation_euler.copy()
def object_rotate_end(self):
pass
def object_rotate(self, **kwargs):
dx, dy, dz = kwargs['rot_x'], -kwargs['rot_y'], kwargs['rot_z']
for o in bpy.context.selected_objects:
pass
# o.rotation_euler.x = self.rotate_origin.x + dx
# o.rotation_euler.y = self.rotate_origin.y - dy
# o.rotation_euler.z = self.rotate_origin.z + dy
def object_scale_origin(self):
s = bpy.context.selected_objects[0].scale
self.scale_origin = s.x, s.y, s.z
def object_scale(self, **kwargs):
sx, sy, sz = kwargs['sx'], kwargs['sy'], kwargs['sz']
x, y, z = self.scale_origin
dx, dy, dz = sx * x, sy * y, sz * z
for o in bpy.context.selected_objects:
o.scale = (dx, dy, dz)
def object_center(self):
for o in bpy.context.selected_objects:
o.location = 0, 0, 0
def object_reset_everything(self):
for o in bpy.context.selected_objects:
o.location = 0, 0, 0
o.rotation_euler = 0, 0, 0
def render(self):
bpy.ops.render.render()
def toggle_noob(self):
self.noob = not self.noob
if not self.noob:
bpy.data.scenes['Scene'].tool_settings.sculpt.radial_symmetry = (1, 1, 1)
else:
bpy.data.scenes['Scene'].tool_settings.sculpt.radial_symmetry = (1, 1, 64)
bpy.utils.register_class(BBQOperator)