This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
forked from jayanam/jmesh-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fc_primitive_mode_op.py
575 lines (400 loc) · 18.6 KB
/
fc_primitive_mode_op.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import bpy
from bpy.types import Operator
from bpy.props import *
import bgl
import blf
import bmesh
import gpu
from gpu_extras.batch import batch_for_shader
from bpy_extras import view3d_utils
import mathutils
from .utils.fc_bool_util import select_active, execute_boolean_op, execute_slice_op, is_apply_immediate
from .utils.fc_view_3d_utils import *
from .types.shape import *
from .types.rectangle_shape import *
from .types.polyline_shape import *
from .types.circle_shape import *
from .types.curve_shape import *
from .types.enums import *
# Primitive mode operator
class FC_Primitive_Mode_Operator(bpy.types.Operator):
bl_idname = "object.fc_primitve_mode_op"
bl_label = "Primitive Mode Operator"
bl_description = "Primitive Mode Operator"
bl_options = {"REGISTER", "UNDO", "BLOCKING"}
@classmethod
def poll(cls, context):
# if context.object is None:
# return False
if context.window_manager.in_primitive_mode:
return False
if context.object == None:
return True
return context.object.mode == "OBJECT" or context.object.mode == "SCULPT"
def __init__(self):
self.draw_handle_2d = None
self.draw_handle_3d = None
self.draw_event = None
self.shape = Polyline_Shape()
self.create_batch(None)
def invoke(self, context, event):
args = (self, context)
context.window_manager.in_primitive_mode = True
self.current_mode = None
if context.object is not None:
self.current_mode = context.object.mode
self.create_shape(context)
self.register_handlers(args, context)
context.window_manager.modal_handler_add(self)
return {"RUNNING_MODAL"}
def register_handlers(self, args, context):
self.draw_handle_3d = bpy.types.SpaceView3D.draw_handler_add(
self.draw_callback_3d, args, "WINDOW", "POST_VIEW")
self.draw_handle_2d = bpy.types.SpaceView3D.draw_handler_add(
self.draw_callback_2d, args, "WINDOW", "POST_PIXEL")
self.draw_event = context.window_manager.event_timer_add(0.1, window=context.window)
def unregister_handlers(self, context):
context.window_manager.in_primitive_mode = False
context.window_manager.event_timer_remove(self.draw_event)
bpy.types.SpaceView3D.draw_handler_remove(self.draw_handle_2d, "WINDOW")
bpy.types.SpaceView3D.draw_handler_remove(self.draw_handle_3d, "WINDOW")
self.draw_handle_2d = None
self.draw_handle_3d = None
self.draw_event = None
def get_snapped_mouse_pos(self, mouse_pos_2d, context):
mouse_pos_3d = self.get_3d_for_mouse(mouse_pos_2d, context)
if context.scene.use_snapping and mouse_pos_3d is not None:
mouse_pos_3d = get_snap_3d_vertex(context, mouse_pos_3d)
mouse_pos_2d = get_2d_vertex(context, mouse_pos_3d)
return mouse_pos_2d, mouse_pos_3d
def get_3d_for_mouse(self, mouse_pos_2d, context):
# Check if to snap to the surface of the object
if context.scene.snap_to_target:
mouse_pos_3d = self.shape.get_3d_for_2d(mouse_pos_2d, context)
if mouse_pos_3d is None:
mouse_pos_3d = get_3d_vertex(context, mouse_pos_2d)
else:
mouse_pos_3d = get_3d_vertex(context, mouse_pos_2d)
return mouse_pos_3d
def modal(self, context, event):
if context.area:
context.area.tag_redraw()
result = "PASS_THROUGH"
if event.type == "ESC" and event.value == "PRESS":
was_none = self.shape.is_none()
self.shape.reset()
self.create_batch(None)
if was_none:
self.unregister_handlers(context)
return {'FINISHED'}
# The mouse wheel is moved
if not self.shape.is_none():
up = event.type == "WHEELUPMOUSE"
down = event.type == "WHEELDOWNMOUSE"
if up or down:
inc = 1 if up else -1
if self.shape.handle_mouse_wheel(inc, context):
mouse_pos_2d = (event.mouse_region_x, event.mouse_region_y)
mouse_pos_3d = self.get_3d_for_mouse(mouse_pos_2d, context)
self.create_batch(mouse_pos_3d)
result = "RUNNING_MODAL"
# The mouse is moved
if event.type == "MOUSEMOVE" and not self.shape.is_none():
mouse_pos_2d = self.shape.get_mouse_pos_2d(event.mouse_region_x, event.mouse_region_y)
mouse_pos_2d, mouse_pos_3d = self.get_snapped_mouse_pos(mouse_pos_2d, context)
if self.shape.handle_mouse_move(mouse_pos_2d, mouse_pos_3d, event, context):
self.create_batch(mouse_pos_3d)
# Left mouse button is released
if event.value == "RELEASE" and event.type == "LEFTMOUSE":
mouse_pos_2d = (event.mouse_region_x, event.mouse_region_y)
self.shape.handle_mouse_release(mouse_pos_2d, event, context)
# Left mouse button is pressed
if event.value == "PRESS" and event.type == "LEFTMOUSE":
self.create_shape(context)
mouse_pos_2d = (event.mouse_region_x, event.mouse_region_y)
# If an object is hit, set it as target
if event.ctrl:
hit, hit_obj = self.shape.is_object_hit(mouse_pos_2d, context)
if hit:
context.scene.carver_target = hit_obj
mouse_pos_2d, mouse_pos_3d = self.get_snapped_mouse_pos(mouse_pos_2d, context)
if self.shape.is_moving():
self.shape.stop_move(context)
if self.shape.is_extruding():
self.shape.stop_extrude(context)
if self.shape.is_rotating():
self.shape.stop_rotate(context)
if self.shape.is_processing():
result = "RUNNING_MODAL"
if self.shape.is_created():
if self.shape.set_vertex_moving(mouse_pos_3d):
result = "RUNNING_MODAL"
if self.shape.handle_mouse_press(mouse_pos_2d, mouse_pos_3d, event, context):
self.create_object(context)
else:
# So that the direction is defined during shape
# creation, not when it is extruded
if self.shape.is_processing():
view_context = ViewContext(context)
self.shape.set_view_context(view_context)
self.create_batch(mouse_pos_3d)
# Keyboard
if event.value == "PRESS":
# try to move the shape
if event.type == "G":
mouse_pos_2d = (event.mouse_region_x, event.mouse_region_y)
mouse_pos_2d, mouse_pos_3d = self.get_snapped_mouse_pos(mouse_pos_2d, context)
if self.shape.start_move(mouse_pos_3d):
result = "RUNNING_MODAL"
if self.shape.is_moving():
if event.type in ["X", "Y", "N"]:
self.shape.set_move_axis(event.type)
result = "RUNNING_MODAL"
# try to rotate the shape
if event.type == "R":
mouse_pos_2d = (event.mouse_region_x, event.mouse_region_y)
mouse_pos_3d = self.get_3d_for_mouse(mouse_pos_2d, context)
if self.shape.start_rotate(mouse_pos_3d, context):
self.create_batch()
result = "RUNNING_MODAL"
# try to extrude the shape
if self.shape.is_extruding():
if (event.type == "DOWN_ARROW" or event.type == "UP_ARROW"):
self.shape.handle_extrude(event.type == "UP_ARROW", context)
self.create_batch()
result = "RUNNING_MODAL"
elif (event.type in ["X", "Y", "Z", "N"]):
self.shape.set_extrude_axis(event.type)
self.create_batch()
result = "RUNNING_MODAL"
if event.type == "E":
mouse_pos_2d = (event.mouse_region_x, event.mouse_region_y)
mouse_pos_3d = self.get_3d_for_mouse(mouse_pos_2d, context)
if self.shape.start_extrude(mouse_pos_2d, mouse_pos_3d, context):
self.create_batch()
result = "RUNNING_MODAL"
# toggle input method
if event.type == "I":
self.shape.set_next_input_method(context)
self.shape.build_actions()
result = "RUNNING_MODAL"
# toggle bool mode
if event.type == "O":
context.scene.bool_mode = next_enum(context.scene.bool_mode,
context.scene, "bool_mode")
self.shape.build_actions()
result = "RUNNING_MODAL"
if event.type == "C":
if self.shape.can_set_center_type():
context.scene.center_type = next_enum(context.scene.center_type, context.scene, "center_type")
self.shape.build_actions()
result = "RUNNING_MODAL"
if event.type == "F":
if self.shape.can_start_from_center():
context.scene.start_center = not context.scene.start_center
self.shape.build_actions()
result = "RUNNING_MODAL"
# toggle primitve
if event.type == "P":
if self.shape.is_none():
context.scene.primitive_type = next_enum(context.scene.primitive_type,
context.scene, "primitive_type")
self.create_shape(context)
result = "RUNNING_MODAL"
return { result }
def create_shape(self, context):
if self.shape.is_none():
if context.scene.primitive_type == "Circle":
self.shape = Circle_Shape()
elif context.scene.primitive_type == "Polyline":
self.shape = Polyline_Shape()
elif context.scene.primitive_type == "Curve":
self.shape = Curve_Shape()
else:
self.shape = Rectangle_Shape()
self.shape.initialize(context)
def create_object(self, context):
# TODO: Refactor -> Creation factory with shape as parameter
if self.shape.connected_shape():
self.create_mesh(context)
else:
self.create_curve(context)
def create_curve(self, context):
curve_shape = self.shape
if curve_shape.is_2_points_input():
self.create_bezier(context)
else:
self.create_path(context)
def set_bevel(self, curve):
obj_data = curve.data
obj_data.bevel_depth = 0.05
obj_data.resolution_u = 24
obj_data.bevel_resolution = 12
obj_data.fill_mode = 'FULL'
def create_path(self, context):
if context.object is not None:
bpy.ops.object.mode_set(mode='OBJECT')
curve_shape = self.shape
bpy.ops.curve.primitive_nurbs_path_add(enter_editmode=True)
self.set_bevel(context.active_object)
bpy.ops.curve.select_all(action='SELECT')
bpy.ops.curve.delete()
for point in curve_shape.get_points():
bpy.ops.curve.vertex_add(location=point)
self.shape.reset()
def create_bezier(self, context):
if context.object is not None:
bpy.ops.object.mode_set(mode='OBJECT')
curve_shape = self.shape
bpy.ops.curve.primitive_bezier_curve_add(enter_editmode=True, location=(0, 0, 0))
curve = context.active_object
self.set_bevel(curve)
bez_points = curve.data.splines[0].bezier_points
point_count = len(bez_points) - 1
norm_start = curve_shape.get_normal_start()
norm_end = curve_shape.get_normal_end()
bez_points[0].co = curve_shape.get_start_point()
if norm_start is not None:
bez_points[0].handle_right = bez_points[0].co + norm_start
bez_points[0].handle_left = bez_points[0].co - norm_start
bez_points[point_count].co = curve_shape.get_end_point()
if norm_end is not None:
bez_points[point_count].handle_right = bez_points[point_count].co - norm_end
bez_points[point_count].handle_left = bez_points[point_count].co + norm_end
self.shape.reset()
def create_mesh(self, context):
try:
if context.object is not None:
bpy.ops.object.mode_set(mode='OBJECT')
is_bool_create = (context.scene.bool_mode == "Create")
# Create a mesh and an object and
# add the object to the scene collection
mesh = bpy.data.meshes.new("MyMesh")
obj = bpy.data.objects.new("MyObject", mesh)
bpy.context.scene.collection.objects.link(obj)
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = obj
obj.select_set(state=True)
# Create a bmesh and add the vertices
# added by mouse clicks
bm = bmesh.new()
bm.from_mesh(mesh)
for v in self.shape.vertices:
bm.verts.new(v)
bm.verts.index_update()
bm.faces.new(bm.verts)
# Extrude mesh if extrude mesh option is enabled
self.extrude_mesh(context, bm, is_bool_create)
bm.to_mesh(mesh)
bm.free()
bpy.context.view_layer.objects.active = obj
obj.select_set(state=True)
self.remove_doubles()
# set origin to geometry
bpy.ops.object.editmode_toggle()
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
# Fast bool modes
if not is_bool_create:
target_obj = bpy.context.scene.carver_target
if target_obj is not None:
bool_mode_id = self.get_bool_mode_id(context.scene.bool_mode)
if bool_mode_id != 3:
execute_boolean_op(context, target_obj, bool_mode_id)
else:
execute_slice_op(context, target_obj)
# delete the bool object if apply immediate is checked
if is_apply_immediate():
bpy.ops.object.delete()
select_active(target_obj)
except RuntimeError:
pass
finally:
if self.current_mode is not None:
bpy.ops.object.mode_set(mode=self.current_mode)
def get_bool_mode_id(self, bool_name):
if bool_name == "Difference":
return 0
elif bool_name == "Union":
return 1
elif bool_name == "Intersect":
return 2
elif bool_name == "Slice":
return 3
return -1
def remove_doubles(self):
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.remove_doubles()
def extrude_mesh(self, context, bm, is_bool_create):
if context.scene.extrude_mesh:
length = 1
if not is_bool_create:
length = 100
dir = self.shape.get_dir() * length
if self.shape.is_extruded():
dir = self.shape.get_dir() * self.shape.extrusion
# extr_geom = bm.edges[:]
extr_geom = bm.faces[:]
r = bmesh.ops.extrude_face_region(bm, geom=extr_geom)
verts = [e for e in r['geom'] if isinstance(e, bmesh.types.BMVert)]
bmesh.ops.translate(bm, vec=dir, verts=verts)
def finish(self):
self.unregister_handlers(bpy.context)
return {"FINISHED"}
def create_batch(self, mouse_pos = None):
points = self.shape.get_vertices_copy(mouse_pos)
extrude_points = self.shape.get_vertices_extruded_copy(mouse_pos)
extrude_lines = []
for index, vertex in enumerate(extrude_points):
extrude_lines.append(points[index])
extrude_lines.append(vertex)
self.shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
self.batch = batch_for_shader(self.shader, 'LINE_LOOP',
{"pos": points})
self.batch_extruded = batch_for_shader(self.shader, 'LINE_LOOP',
{"pos": extrude_points})
self.batch_lines_extruded = batch_for_shader(self.shader, 'LINES',
{"pos": extrude_lines})
self.batch_points = batch_for_shader(self.shader, 'POINTS', {"pos": points})
def draw_action_line(self, action, pos_y):
blf.color(1, 1, 1, 1, 1)
blf.position(1, 10, pos_y , 1)
blf.draw(1, action.title)
if(action.content != ""):
blf.position(1, 115, pos_y , 1)
blf.draw(1, ": " + action.content)
blf.color(1, 0, 0.5, 1, 1)
blf.position(1, 250, pos_y, 1)
blf.draw(1, action.id)
# Draw handler to paint in pixels
def draw_callback_2d(self, op, context):
self.shape.draw_text()
# Draw text for primitive mode
blf.size(1, 16, 72)
size = 20
pos_y = 160 # self.get_actions_height(size)
self.draw_action_line(self.shape.actions[0], pos_y)
for index in range(len(self.shape.actions)-1):
action = self.shape.actions[index+1]
self.draw_action_line(action, (pos_y - 10) - (index + 1) * size)
blf.color(1, 1, 1, 1, 1)
def get_actions_height(self, size):
return len(self.shape.actions) * size
# Draw handler to paint onto the screen
def draw_callback_3d(self, op, context):
self.shader.bind()
if self.shape.connected_shape():
# Draw lines
bgl.glEnable(bgl.GL_LINE_SMOOTH)
self.shader.uniform_float("color", (0.2, 0.5, 0.8, 1.0))
bgl.glLineWidth(2)
self.batch_extruded.draw(self.shader)
bgl.glLineWidth(1)
self.batch_lines_extruded.draw(self.shader)
bgl.glLineWidth(3)
self.shader.uniform_float("color", (0.1, 0.3, 0.7, 1.0))
self.batch.draw(self.shader)
else:
self.shader.uniform_float("color", (0.1, 0.3, 0.7, 1.0))
bgl.glPointSize(self.shape.get_point_size(context))
self.batch_points.draw(self.shader)