forked from blender/blender-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carver_operator.py
1350 lines (1120 loc) · 55.6 KB
/
carver_operator.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: 2019-2023 Blender Foundation
#
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
import bpy_extras
import sys
from bpy.props import (
BoolProperty,
IntProperty,
PointerProperty,
StringProperty,
EnumProperty,
)
from mathutils import (
Vector,
)
from bpy_extras.view3d_utils import (
region_2d_to_vector_3d,
region_2d_to_origin_3d,
region_2d_to_location_3d,
location_3d_to_region_2d,
)
from .carver_profils import (
Profils
)
from .carver_utils import (
duplicateObject,
UndoListUpdate,
createMeshFromData,
SelectObject,
Selection_Save_Restore,
Selection_Save,
Selection_Restore,
update_grid,
objDiagonal,
Undo,
UndoAdd,
Pick,
rot_axis_quat,
MoveCursor,
Picking,
CreateCutSquare,
CreateCutCircle,
CreateCutLine,
boolean_operation,
update_bevel,
CreateBevel,
Rebool,
Snap_Cursor,
)
from .carver_draw import draw_callback_px
# Modal Operator
class CARVER_OT_operator(bpy.types.Operator):
bl_idname = "carver.operator"
bl_label = "Carver"
bl_description = "Cut or create Meshes in Object mode"
bl_options = {'REGISTER', 'UNDO'}
def __init__(self):
context = bpy.context
# Carve mode: Cut, Object, Profile
self.CutMode = False
self.CreateMode = False
self.ObjectMode = False
self.ProfileMode = False
# Create mode
self.ExclusiveCreateMode = False
if len(context.selected_objects) == 0:
self.ExclusiveCreateMode = True
self.CreateMode = True
# Cut type (Rectangle, Circle, Line)
self.rectangle = 0
self.line = 1
self.circle = 2
# Cut Rectangle coordinates
self.rectangle_coord = []
# Selected type of cut
self.CutType = 0
# Boolean operation
self.difference = 0
self.union = 1
self.BoolOps = self.difference
self.CurrentSelection = context.selected_objects.copy()
self.CurrentActive = context.active_object
self.all_sel_obj_list = context.selected_objects.copy()
self.save_active_obj = None
args = (self, context)
self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, args, 'WINDOW', 'POST_PIXEL')
self.mouse_path = [(0, 0), (0, 0)]
# Keyboard event
self.shift = False
self.ctrl = False
self.alt = False
self.dont_apply_boolean = context.scene.mesh_carver.DontApply
self.Auto_BevelUpdate = True
# Circle variables
self.stepAngle = [2, 4, 5, 6, 9, 10, 15, 20, 30, 40, 45, 60, 72, 90]
self.step = 4
# Primitives Position
self.xpos = 0
self.ypos = 0
self.InitPosition = False
# Close polygonal shape
self.Closed = False
# Depth Cursor
self.snapCursor = context.scene.mesh_carver.DepthCursor
# Help
self.AskHelp = False
# Working object
self.OpsObj = context.active_object
# Rebool forced (cut line)
self.ForceRebool = False
self.ViewVector = Vector()
self.CurrentObj = None
# Brush
self.BrushSolidify = False
self.WidthSolidify = False
self.CarveDepth = False
self.BrushDepth = False
self.BrushDepthOffset = 0.0
self.snap = False
self.ObjectScale = False
#Init create circle primitive
self.CLR_C = []
# Cursor location
self.CurLoc = Vector((0.0, 0.0, 0.0))
self.SavCurLoc = Vector((0.0, 0.0, 0.0))
# Mouse region
self.mouse_region = -1, -1
self.SavMousePos = None
self.xSavMouse = 0
# Scale, rotate object
self.ascale = 0
self.aRotZ = 0
self.nRotZ = 0
self.quat_rot_axis = None
self.quat_rot = None
self.RandomRotation = context.scene.mesh_carver.ORandom
self.ShowCursor = True
self.Instantiate = context.scene.mesh_carver.OInstanciate
self.ProfileBrush = None
self.ObjectBrush = None
self.InitBrush = {
'location' : None,
'scale' : None,
'rotation_quaternion' : None,
'rotation_euler' : None,
'display_type' : 'WIRE',
'show_in_front' : False
}
# Array variables
self.nbcol = 1
self.nbrow = 1
self.gapx = 0
self.gapy = 0
self.scale_x = 1
self.scale_y = 1
self.GridScaleX = False
self.GridScaleY = False
@classmethod
def poll(cls, context):
ob = None
if len(context.selected_objects) > 0:
ob = context.selected_objects[0]
# Test if selected object or none (for create mode)
return (
(ob and ob.type == 'MESH' and context.mode == 'OBJECT') or
(context.mode == 'OBJECT' and ob is None) or
(context.mode == 'EDIT_MESH'))
def modal(self, context, event):
PI = 3.14156
region_types = {'WINDOW', 'UI'}
win = context.window
# Find the limit of the view3d region
self.check_region(context,event)
for area in win.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if not region_types or region.type in region_types:
region.tag_redraw()
# Change the snap increment value using the wheel mouse
if self.CutMode:
if self.alt is False:
if self.ctrl and (self.CutType in (self.line, self.rectangle)):
# Get the VIEW3D area
for i, a in enumerate(context.screen.areas):
if a.type == 'VIEW_3D':
space = context.screen.areas[i].spaces.active
grid_scale = space.overlay.grid_scale
grid_subdivisions = space.overlay.grid_subdivisions
if event.type == 'WHEELUPMOUSE':
space.overlay.grid_subdivisions += 1
elif event.type == 'WHEELDOWNMOUSE':
space.overlay.grid_subdivisions -= 1
if event.type in {
'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE',
'NUMPAD_1', 'NUMPAD_2', 'NUMPAD_3', 'NUMPAD_4', 'NUMPAD_6',
'NUMPAD_7', 'NUMPAD_8', 'NUMPAD_9', 'NUMPAD_5'}:
return {'PASS_THROUGH'}
try:
# [Shift]
self.shift = True if event.shift else False
# [Ctrl]
self.ctrl = True if event.ctrl else False
# [Alt]
self.alt = False
# [Alt] press : Init position variable before moving the cut brush with LMB
if event.alt:
if self.InitPosition is False:
self.xpos = 0
self.ypos = 0
self.last_mouse_region_x = event.mouse_region_x
self.last_mouse_region_y = event.mouse_region_y
self.InitPosition = True
self.alt = True
# [Alt] release : update the coordinates
if self.InitPosition and self.alt is False:
for i in range(0, len(self.mouse_path)):
l = list(self.mouse_path[i])
l[0] += self.xpos
l[1] += self.ypos
self.mouse_path[i] = tuple(l)
self.xpos = self.ypos = 0
self.InitPosition = False
if event.type == 'SPACE' and event.value == 'PRESS':
# If object or profile mode is TRUE : Confirm the cut
if self.ObjectMode or self.ProfileMode:
# If array, remove double with intersect meshes
if ((self.nbcol + self.nbrow) > 3):
# Go in edit mode mode
bpy.ops.object.mode_set(mode='EDIT')
# Remove duplicate vertices
bpy.ops.mesh.remove_doubles()
# Return in object mode
bpy.ops.object.mode_set(mode='OBJECT')
if self.alt:
# Save selected objects
self.all_sel_obj_list = context.selected_objects.copy()
if len(context.selected_objects) > 0:
bpy.ops.object.select_all(action='TOGGLE')
if self.ObjectMode:
SelectObject(self, self.ObjectBrush)
else:
SelectObject(self, self.ProfileBrush)
duplicateObject(self)
else:
# Brush Cut
self.Cut()
# Save selected objects
if self.ObjectMode:
if len(self.ObjectBrush.children) > 0:
self.all_sel_obj_list = context.selected_objects.copy()
if len(context.selected_objects) > 0:
bpy.ops.object.select_all(action='TOGGLE')
if self.ObjectMode:
SelectObject(self, self.ObjectBrush)
else:
SelectObject(self, self.ProfileBrush)
duplicateObject(self)
UndoListUpdate(self)
# Save cursor position
self.SavMousePos = self.CurLoc
else:
if self.CutMode is False:
# Cut Mode
self.CutType += 1
if self.CutType > 2:
self.CutType = 0
else:
if self.CutType == self.line:
# Cuts creation
CreateCutLine(self, context)
if self.CreateMode:
# Object creation
self.CreateGeometry()
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
# Cursor Snap
context.scene.mesh_carver.DepthCursor = self.snapCursor
# Object Instantiate
context.scene.mesh_carver.OInstanciate = self.Instantiate
# Random rotation
context.scene.mesh_carver.ORandom = self.RandomRotation
return {'FINISHED'}
else:
self.Cut()
UndoListUpdate(self)
#-----------------------------------------------------
# Object creation
#-----------------------------------------------------
# Object creation
if event.type == self.carver_prefs.Key_Create and event.value == 'PRESS':
if self.ExclusiveCreateMode is False:
self.CreateMode = not self.CreateMode
# Auto Bevel Update
if event.type == self.carver_prefs.Key_Update and event.value == 'PRESS':
self.Auto_BevelUpdate = not self.Auto_BevelUpdate
# Boolean operation type
if event.type == self.carver_prefs.Key_Bool and event.value == 'PRESS':
if (self.ProfileMode is True) or (self.ObjectMode is True):
if self.BoolOps == self.difference:
self.BoolOps = self.union
else:
self.BoolOps = self.difference
# Brush Mode
if event.type == self.carver_prefs.Key_Brush and event.value == 'PRESS':
self.dont_apply_boolean = False
if (self.ProfileMode is False) and (self.ObjectMode is False):
self.ProfileMode = True
else:
self.ProfileMode = False
if self.ObjectBrush is not None:
if self.ObjectMode is False:
self.ObjectMode = True
self.BrushSolidify = False
self.CList = self.OB_List
Selection_Save_Restore(self)
context.scene.mesh_carver.nProfile = self.nProfil
else:
self.ObjectMode = False
else:
self.BrushSolidify = False
Selection_Save_Restore(self)
if self.ProfileMode:
createMeshFromData(self)
self.ProfileBrush = bpy.data.objects["CT_Profil"]
Selection_Save(self)
self.BrushSolidify = True
bpy.ops.object.select_all(action='TOGGLE')
self.ProfileBrush.select_set(True)
context.view_layer.objects.active = self.ProfileBrush
# Set xRay
self.ProfileBrush.show_in_front = True
solidify_modifier = context.object.modifiers.new("CT_SOLIDIFY",
'SOLIDIFY')
solidify_modifier.thickness = 0.1
Selection_Restore(self)
self.CList = self.CurrentSelection
else:
if self.ObjectBrush is not None:
if self.ObjectMode is False:
if self.ObjectBrush is not None:
self.ObjectBrush.location = self.InitBrush['location']
self.ObjectBrush.scale = self.InitBrush['scale']
self.ObjectBrush.rotation_quaternion = self.InitBrush['rotation_quaternion']
self.ObjectBrush.rotation_euler = self.InitBrush['rotation_euler']
self.ObjectBrush.display_type = self.InitBrush['display_type']
self.ObjectBrush.show_in_front = self.InitBrush['show_in_front']
#Store active and selected objects
Selection_Save(self)
#Remove Carver modifier
self.BrushSolidify = False
bpy.ops.object.select_all(action='TOGGLE')
self.ObjectBrush.select_set(True)
context.view_layer.objects.active = self.ObjectBrush
bpy.ops.object.modifier_remove(modifier="CT_SOLIDIFY")
#Restore selected and active object
Selection_Restore(self)
else:
if self.SolidifyPossible:
#Store active and selected objects
Selection_Save(self)
self.BrushSolidify = True
bpy.ops.object.select_all(action='TOGGLE')
self.ObjectBrush.select_set(True)
context.view_layer.objects.active = self.ObjectBrush
# Set xRay
self.ObjectBrush.show_in_front = True
solidify_modifier = context.object.modifiers.new("CT_SOLIDIFY",
'SOLIDIFY')
solidify_modifier.thickness = 0.1
#Restore selected and active object
Selection_Restore(self)
# Help display
if event.type == self.carver_prefs.Key_Help and event.value == 'PRESS':
self.AskHelp = not self.AskHelp
# Instantiate object
if event.type == self.carver_prefs.Key_Instant and event.value == 'PRESS':
self.Instantiate = not self.Instantiate
# Close polygonal shape
if event.type == self.carver_prefs.Key_Close and event.value == 'PRESS':
if self.CreateMode:
self.Closed = not self.Closed
if event.type == self.carver_prefs.Key_Apply and event.value == 'PRESS':
self.dont_apply_boolean = not self.dont_apply_boolean
# Scale object
if event.type == self.carver_prefs.Key_Scale and event.value == 'PRESS':
if self.ObjectScale is False:
self.mouse_region = event.mouse_region_x, event.mouse_region_y
self.ObjectScale = True
# Grid : Snap on grid
if event.type == self.carver_prefs.Key_Snap and event.value == 'PRESS':
self.snap = not self.snap
# Array : Add column
if event.type == 'UP_ARROW' and event.value == 'PRESS':
self.nbrow += 1
update_grid(self, context)
# Array : Delete column
elif event.type == 'DOWN_ARROW' and event.value == 'PRESS':
self.nbrow -= 1
update_grid(self, context)
# Array : Add row
elif event.type == 'RIGHT_ARROW' and event.value == 'PRESS':
self.nbcol += 1
update_grid(self, context)
# Array : Delete row
elif event.type == 'LEFT_ARROW' and event.value == 'PRESS':
self.nbcol -= 1
update_grid(self, context)
# Array : Scale gap between columns
if event.type == self.carver_prefs.Key_Gapy and event.value == 'PRESS':
if self.GridScaleX is False:
self.mouse_region = event.mouse_region_x, event.mouse_region_y
self.GridScaleX = True
# Array : Scale gap between rows
if event.type == self.carver_prefs.Key_Gapx and event.value == 'PRESS':
if self.GridScaleY is False:
self.mouse_region = event.mouse_region_x, event.mouse_region_y
self.GridScaleY = True
# Cursor depth or solidify pattern
if event.type == self.carver_prefs.Key_Depth and event.value == 'PRESS':
if (self.ObjectMode is False) and (self.ProfileMode is False):
self.snapCursor = not self.snapCursor
else:
# Solidify
if (self.ObjectMode or self.ProfileMode) and (self.SolidifyPossible):
solidify = True
if self.ObjectMode:
z = self.ObjectBrush.data.vertices[0].co.z
ErrorMarge = 0.01
for v in self.ObjectBrush.data.vertices:
if abs(v.co.z - z) > ErrorMarge:
solidify = False
self.CarveDepth = True
self.mouse_region = event.mouse_region_x, event.mouse_region_y
break
if solidify:
if self.ObjectMode:
for mb in self.ObjectBrush.modifiers:
if mb.type == 'SOLIDIFY':
AlreadySoldify = True
else:
for mb in self.ProfileBrush.modifiers:
if mb.type == 'SOLIDIFY':
AlreadySoldify = True
if AlreadySoldify is False:
Selection_Save(self)
self.BrushSolidify = True
bpy.ops.object.select_all(action='TOGGLE')
if self.ObjectMode:
self.ObjectBrush.select_set(True)
context.view_layer.objects.active = self.ObjectBrush
# Active le xray
self.ObjectBrush.show_in_front = True
else:
self.ProfileBrush.select_set(True)
context.view_layer.objects.active = self.ProfileBrush
# Active le xray
self.ProfileBrush.show_in_front = True
solidify_modifier = context.object.modifiers.new("CT_SOLIDIFY",
'SOLIDIFY')
solidify_modifier.thickness = 0.1
Selection_Restore(self)
self.WidthSolidify = not self.WidthSolidify
self.mouse_region = event.mouse_region_x, event.mouse_region_y
if event.type == self.carver_prefs.Key_BrushDepth and event.value == 'PRESS':
if self.ObjectMode:
self.CarveDepth = False
self.BrushDepth = True
self.mouse_region = event.mouse_region_x, event.mouse_region_y
# Random rotation
if event.type == 'R' and event.value == 'PRESS':
self.RandomRotation = not self.RandomRotation
# Undo
if event.type == 'Z' and event.value == 'PRESS':
if self.ctrl:
if (self.CutType == self.line) and (self.CutMode):
if len(self.mouse_path) > 1:
self.mouse_path[len(self.mouse_path) - 1:] = []
else:
Undo(self)
# Mouse move
if event.type == 'MOUSEMOVE' :
if self.ObjectMode or self.ProfileMode:
fac = 50.0
if self.shift:
fac = 500.0
if self.WidthSolidify:
if self.ObjectMode:
bpy.data.objects[self.ObjectBrush.name].modifiers[
"CT_SOLIDIFY"].thickness += (event.mouse_region_x - self.mouse_region[0]) / fac
elif self.ProfileMode:
bpy.data.objects[self.ProfileBrush.name].modifiers[
"CT_SOLIDIFY"].thickness += (event.mouse_region_x - self.mouse_region[0]) / fac
self.mouse_region = event.mouse_region_x, event.mouse_region_y
elif self.CarveDepth:
for v in self.ObjectBrush.data.vertices:
v.co.z += (event.mouse_region_x - self.mouse_region[0]) / fac
self.mouse_region = event.mouse_region_x, event.mouse_region_y
elif self.BrushDepth:
self.BrushDepthOffset += (event.mouse_region_x - self.mouse_region[0]) / fac
self.mouse_region = event.mouse_region_x, event.mouse_region_y
else:
if (self.GridScaleX):
self.gapx += (event.mouse_region_x - self.mouse_region[0]) / 50
self.mouse_region = event.mouse_region_x, event.mouse_region_y
update_grid(self, context)
return {'RUNNING_MODAL'}
elif (self.GridScaleY):
self.gapy += (event.mouse_region_x - self.mouse_region[0]) / 50
self.mouse_region = event.mouse_region_x, event.mouse_region_y
update_grid(self, context)
return {'RUNNING_MODAL'}
elif self.ObjectScale:
self.ascale = -(event.mouse_region_x - self.mouse_region[0])
self.mouse_region = event.mouse_region_x, event.mouse_region_y
if self.ObjectMode:
self.ObjectBrush.scale.x -= float(self.ascale) / 150.0
if self.ObjectBrush.scale.x <= 0.0:
self.ObjectBrush.scale.x = 0.0
self.ObjectBrush.scale.y -= float(self.ascale) / 150.0
if self.ObjectBrush.scale.y <= 0.0:
self.ObjectBrush.scale.y = 0.0
self.ObjectBrush.scale.z -= float(self.ascale) / 150.0
if self.ObjectBrush.scale.z <= 0.0:
self.ObjectBrush.scale.z = 0.0
elif self.ProfileMode:
if self.ProfileBrush is not None:
self.ProfileBrush.scale.x -= float(self.ascale) / 150.0
self.ProfileBrush.scale.y -= float(self.ascale) / 150.0
self.ProfileBrush.scale.z -= float(self.ascale) / 150.0
else:
if self.LMB:
if self.ctrl:
self.aRotZ = - \
((int((event.mouse_region_x - self.xSavMouse) / 10.0) * PI / 4.0) * 25.0)
else:
self.aRotZ -= event.mouse_region_x - self.mouse_region[0]
self.ascale = 0.0
self.mouse_region = event.mouse_region_x, event.mouse_region_y
else:
target_hit, target_normal, target_eul_rotation = Pick(context, event, self)
if target_hit is not None:
self.ShowCursor = True
up_vector = Vector((0.0, 0.0, 1.0))
quat_rot_axis = rot_axis_quat(up_vector, target_normal)
self.quat_rot = target_eul_rotation @ quat_rot_axis
MoveCursor(quat_rot_axis, target_hit, self)
self.SavCurLoc = target_hit
if self.ctrl:
if self.SavMousePos is not None:
xEcart = abs(self.SavMousePos.x - self.SavCurLoc.x)
yEcart = abs(self.SavMousePos.y - self.SavCurLoc.y)
zEcart = abs(self.SavMousePos.z - self.SavCurLoc.z)
if (xEcart > yEcart) and (xEcart > zEcart):
self.CurLoc = Vector(
(target_hit.x, self.SavMousePos.y, self.SavMousePos.z))
if (yEcart > xEcart) and (yEcart > zEcart):
self.CurLoc = Vector(
(self.SavMousePos.x, target_hit.y, self.SavMousePos.z))
if (zEcart > xEcart) and (zEcart > yEcart):
self.CurLoc = Vector(
(self.SavMousePos.x, self.SavMousePos.y, target_hit.z))
else:
self.CurLoc = target_hit
else:
self.CurLoc = target_hit
else:
if self.CutMode:
if self.alt is False:
if self.ctrl :
# Find the closest position on the overlay grid and snap the mouse on it
# Draw a mini grid around the cursor
mouse_pos = [[event.mouse_region_x, event.mouse_region_y]]
Snap_Cursor(self, context, event, mouse_pos)
else:
if len(self.mouse_path) > 0:
self.mouse_path[len(self.mouse_path) -
1] = (event.mouse_region_x, event.mouse_region_y)
else:
# [ALT] press, update position
self.xpos += (event.mouse_region_x - self.last_mouse_region_x)
self.ypos += (event.mouse_region_y - self.last_mouse_region_y)
self.last_mouse_region_x = event.mouse_region_x
self.last_mouse_region_y = event.mouse_region_y
elif event.type == 'LEFTMOUSE' and event.value == 'PRESS':
if self.ObjectMode or self.ProfileMode:
if self.LMB is False:
target_hit, target_normal, target_eul_rotation = Pick(context, event, self)
if target_hit is not None:
up_vector = Vector((0.0, 0.0, 1.0))
self.quat_rot_axis = rot_axis_quat(up_vector, target_normal)
self.quat_rot = target_eul_rotation @ self.quat_rot_axis
self.mouse_region = event.mouse_region_x, event.mouse_region_y
self.xSavMouse = event.mouse_region_x
if self.ctrl:
self.nRotZ = int((self.aRotZ / 25.0) / (PI / 4.0))
self.aRotZ = self.nRotZ * (PI / 4.0) * 25.0
self.LMB = True
# LEFTMOUSE
elif event.type == 'LEFTMOUSE' and event.value == 'RELEASE' and self.in_view_3d:
if self.ObjectMode or self.ProfileMode:
# Rotation and scale
self.LMB = False
if self.ObjectScale is True:
self.ObjectScale = False
if self.GridScaleX is True:
self.GridScaleX = False
if self.GridScaleY is True:
self.GridScaleY = False
if self.WidthSolidify:
self.WidthSolidify = False
if self.CarveDepth is True:
self.CarveDepth = False
if self.BrushDepth is True:
self.BrushDepth = False
else:
if self.CutMode is False:
if self.ctrl:
Picking(context, event)
else:
if self.CutType == self.line:
if self.CutMode is False:
self.mouse_path.clear()
self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))
self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))
else:
self.mouse_path[0] = (event.mouse_region_x, event.mouse_region_y)
self.mouse_path[1] = (event.mouse_region_x, event.mouse_region_y)
self.CutMode = True
else:
if self.CutType != self.line:
# Cut creation
if self.CutType == self.rectangle:
CreateCutSquare(self, context)
if self.CutType == self.circle:
CreateCutCircle(self, context)
if self.CutType == self.line:
CreateCutLine(self, context)
if self.CreateMode:
# Object creation
self.CreateGeometry()
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
# Depth Cursor
context.scene.mesh_carver.DepthCursor = self.snapCursor
# Instantiate object
context.scene.mesh_carver.OInstanciate = self.Instantiate
# Random rotation
context.scene.mesh_carver.ORandom = self.RandomRotation
# Apply operation
context.scene.mesh_carver.DontApply = self.dont_apply_boolean
# if Object mode, set initiale state
if self.ObjectBrush is not None:
self.ObjectBrush.location = self.InitBrush['location']
self.ObjectBrush.scale = self.InitBrush['scale']
self.ObjectBrush.rotation_quaternion = self.InitBrush['rotation_quaternion']
self.ObjectBrush.rotation_euler = self.InitBrush['rotation_euler']
self.ObjectBrush.display_type = self.InitBrush['display_type']
self.ObjectBrush.show_in_front = self.InitBrush['show_in_front']
# remove solidify
Selection_Save(self)
self.BrushSolidify = False
bpy.ops.object.select_all(action='TOGGLE')
self.ObjectBrush.select_set(True)
context.view_layer.objects.active = self.ObjectBrush
bpy.ops.object.modifier_remove(modifier="CT_SOLIDIFY")
Selection_Restore(self)
context.scene.mesh_carver.nProfile = self.nProfil
return {'FINISHED'}
else:
self.Cut()
UndoListUpdate(self)
else:
# Line
self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))
# Change brush profil or circle subdivisions
elif (event.type == 'COMMA' and event.value == 'PRESS') or \
(event.type == self.carver_prefs.Key_Subrem and event.value == 'PRESS'):
# Brush profil
if self.ProfileMode:
self.nProfil += 1
if self.nProfil >= self.MaxProfil:
self.nProfil = 0
createMeshFromData(self)
# Circle subdivisions
if self.CutType == self.circle:
self.step += 1
if self.step >= len(self.stepAngle):
self.step = len(self.stepAngle) - 1
# Change brush profil or circle subdivisions
elif (event.type == 'PERIOD' and event.value == 'PRESS') or \
(event.type == self.carver_prefs.Key_Subadd and event.value == 'PRESS'):
# Brush profil
if self.ProfileMode:
self.nProfil -= 1
if self.nProfil < 0:
self.nProfil = self.MaxProfil - 1
createMeshFromData(self)
# Circle subdivisions
if self.CutType == self.circle:
if self.step > 0:
self.step -= 1
# Quit
elif event.type in {'RIGHTMOUSE', 'ESC'}:
# Depth Cursor
context.scene.mesh_carver.DepthCursor = self.snapCursor
# Instantiate object
context.scene.mesh_carver.OInstanciate = self.Instantiate
# Random Rotation
context.scene.mesh_carver.ORandom = self.RandomRotation
# Apply boolean operation
context.scene.mesh_carver.DontApply = self.dont_apply_boolean
# Reset Object
if self.ObjectBrush is not None:
self.ObjectBrush.location = self.InitBrush['location']
self.ObjectBrush.scale = self.InitBrush['scale']
self.ObjectBrush.rotation_quaternion = self.InitBrush['rotation_quaternion']
self.ObjectBrush.rotation_euler = self.InitBrush['rotation_euler']
self.ObjectBrush.display_type = self.InitBrush['display_type']
self.ObjectBrush.show_in_front = self.InitBrush['show_in_front']
# Remove solidify modifier
Selection_Save(self)
self.BrushSolidify = False
bpy.ops.object.select_all(action='TOGGLE')
self.ObjectBrush.select_set(True)
context.view_layer.objects.active = self.ObjectBrush
bpy.ops.object.modifier_remove(modifier="CT_SOLIDIFY")
bpy.ops.object.select_all(action='TOGGLE')
Selection_Restore(self)
Selection_Save_Restore(self)
context.view_layer.objects.active = self.CurrentActive
context.scene.mesh_carver.nProfile = self.nProfil
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
# Remove Copy Object Brush
if bpy.data.objects.get("CarverBrushCopy") is not None:
brush = bpy.data.objects["CarverBrushCopy"]
self.ObjectBrush.data = bpy.data.meshes[brush.data.name]
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects["CarverBrushCopy"].select_set(True)
bpy.ops.object.delete()
return {'FINISHED'}
return {'RUNNING_MODAL'}
except:
print("\n[Carver MT ERROR]\n")
import traceback
traceback.print_exc()
context.window.cursor_modal_set("DEFAULT")
context.area.header_text_set(None)
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
self.report({'WARNING'},
"Operation finished. Failure during Carving (Check the console for more info)")
return {'FINISHED'}
def cancel(self, context):
# Note: used to prevent memory leaks on quitting Blender while the modal operator
# is still running, gets called on return {"CANCELLED"}
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
def invoke(self, context, event):
if context.area.type != 'VIEW_3D':
self.report({'WARNING'},
"View3D not found or not currently active. Operation Cancelled")
self.cancel(context)
return {'CANCELLED'}
# test if some other object types are selected that are not meshes
for obj in context.selected_objects:
if obj.type != "MESH":
self.report({'WARNING'},
"Some selected objects are not of the Mesh type. Operation Cancelled")
self.cancel(context)
return {'CANCELLED'}
if context.mode == 'EDIT_MESH':
bpy.ops.object.mode_set(mode='OBJECT')
#Load the Carver preferences
self.carver_prefs = bpy.context.preferences.addons[__package__].preferences
# Get default patterns
self.Profils = []
for p in Profils:
self.Profils.append((p[0], p[1], p[2], p[3]))
for o in context.scene.objects:
if not o.name.startswith(self.carver_prefs.ProfilePrefix):
continue
# In-scene profiles may have changed, remove them to refresh
for m in bpy.data.meshes:
if m.name.startswith(self.carver_prefs.ProfilePrefix):
bpy.data.meshes.remove(m)
vertices = []
for v in o.data.vertices:
vertices.append((v.co.x, v.co.y, v.co.z))
faces = []
for f in o.data.polygons:
face = []
for v in f.vertices:
face.append(v)
faces.append(face)
self.Profils.append(
(o.name,
Vector((o.location.x, o.location.y, o.location.z)),
vertices, faces)
)
self.nProfil = context.scene.mesh_carver.nProfile
self.MaxProfil = len(self.Profils)
# reset selected profile if last profile exceeds length of array
if self.nProfil >= self.MaxProfil:
self.nProfil = context.scene.mesh_carver.nProfile = 0
if len(context.selected_objects) > 1:
self.ObjectBrush = context.active_object
# Copy the brush object
ob = bpy.data.objects.new("CarverBrushCopy", context.object.data.copy())
ob.location = self.ObjectBrush.location
context.collection.objects.link(ob)
context.view_layer.update()
# Save default variables
self.InitBrush['location'] = self.ObjectBrush.location.copy()
self.InitBrush['scale'] = self.ObjectBrush.scale.copy()
self.InitBrush['rotation_quaternion'] = self.ObjectBrush.rotation_quaternion.copy()
self.InitBrush['rotation_euler'] = self.ObjectBrush.rotation_euler.copy()
self.InitBrush['display_type'] = self.ObjectBrush.display_type
self.InitBrush['show_in_front'] = self.ObjectBrush.show_in_front
# Test if flat object
z = self.ObjectBrush.data.vertices[0].co.z
ErrorMarge = 0.01
self.SolidifyPossible = True
for v in self.ObjectBrush.data.vertices:
if abs(v.co.z - z) > ErrorMarge:
self.SolidifyPossible = False
break
self.CList = []
self.OPList = []
self.RList = []
self.OB_List = []
for obj in context.selected_objects:
if obj != self.ObjectBrush:
self.OB_List.append(obj)
# Left button
self.LMB = False
# Undo Variables
self.undo_index = 0
self.undo_limit = context.preferences.edit.undo_steps
self.undo_list = []