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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
|
# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****
#
# -----------------------------------------------------------------------
# Author: Alan Odom (Clockmender), Rune Morling (ermo) Copyright (c) 2019
# -----------------------------------------------------------------------
#
import bmesh
import bpy
import numpy as np
from bpy.types import Operator
from mathutils import Vector
from mathutils.geometry import intersect_point_line
from math import sin, cos, tan, pi, sqrt
from .pdt_functions import (
setMode,
checkSelection,
setAxis,
updateSel,
viewCoords,
viewCoordsI,
viewDir,
arcCentre,
intersection,
getPercent,
)
from .pdt_msg_strings import (
PDT_ERR_CONNECTED,
PDT_ERR_EDIT_MODE,
PDT_ERR_EDOB_MODE,
PDT_ERR_FACE_SEL,
PDT_ERR_INT_LINES,
PDT_ERR_INT_NO_ALL,
PDT_ERR_NON_VALID,
PDT_ERR_NO_ACT_OBJ,
PDT_ERR_NO_ACT_VERTS,
PDT_ERR_SEL_1_EDGE,
PDT_ERR_SEL_1_VERT,
PDT_ERR_SEL_1_VERTI,
PDT_ERR_SEL_2_OBJS,
PDT_ERR_SEL_2_VERTIO,
PDT_ERR_SEL_2_VERTS,
PDT_ERR_SEL_2_EDGES,
PDT_ERR_SEL_3_OBJS,
PDT_ERR_SEL_3_VERTIO,
PDT_ERR_SEL_3_VERTS,
PDT_ERR_SEL_4_OBJS,
PDT_ERR_SEL_4_VERTS,
PDT_ERR_STRIGHT_LINE,
PDT_ERR_TAPER_ANG,
PDT_ERR_TAPER_SEL,
PDT_ERR_VERT_MODE,
PDT_INF_OBJ_MOVED,
PDT_LAB_ABS,
PDT_LAB_ARCCENTRE,
PDT_LAB_DEL,
PDT_LAB_DIR,
PDT_LAB_INTERSECT,
PDT_LAB_NOR,
PDT_LAB_PERCENT,
PDT_LAB_PLANE
)
class PDT_OT_PlacementAbs(Operator):
"""Use Absolute, or Global Placement."""
bl_idname = "pdt.absolute"
bl_label = "Absolute Mode"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Manipulates Geometry, or Objects by Absolute (World) Coordinates.
- Reads pg.operate from Operation Mode Selector as 'data'
- Reads pg.cartesian_coords scene variables to:
-- set position of CUrsor (CU)
-- set postion of Pivot Point (PP)
-- MoVe geometry/objects (MV)
-- Extrude Vertices (EV)
-- Split Edges (SE)
-- add a New Vertex (NV)
Invalid Options result in self.report Error.
Local vector variable 'vector_delta' is used to reposition features.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
oper = pg.operation
vector_delta = pg.cartesian_coords
if oper not in {"CU", "PP", "NV"}:
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
obj_loc = obj.matrix_world.decompose()[0]
if obj.mode == "EDIT":
bm = bmesh.from_edit_mesh(obj.data)
verts = [v for v in bm.verts if v.select]
if len(verts) == 0:
errmsg = PDT_ERR_NO_ACT_VERTS
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if oper == "CU":
scene.cursor.location = vector_delta
scene.cursor.rotation_euler = (0, 0, 0)
elif oper == "PP":
pg.pivot_loc = vector_delta
elif oper == "MV":
if obj.mode == "EDIT":
for v in verts:
v.co = vector_delta - obj_loc
bm.select_history.clear()
bmesh.ops.remove_doubles(bm, verts=[v for v in bm.verts if v.select], dist=0.0001)
bmesh.update_edit_mesh(obj.data)
elif obj.mode == "OBJECT":
for ob in context.view_layer.objects.selected:
ob.location = vector_delta
elif oper == "SE" and obj.mode == "EDIT":
edges = [e for e in bm.edges if e.select]
if len(edges) != 1:
errmsg = f"{PDT_ERR_SEL_1_EDGE} {len(edges)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
geom = bmesh.ops.subdivide_edges(bm, edges=edges, cuts=1)
new_verts = [v for v in geom["geom_split"] if isinstance(v, bmesh.types.BMVert)]
nVert = new_verts[0]
nVert.co = vector_delta - obj_loc
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
nVert.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif oper == "NV":
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if obj.mode == "EDIT":
bm = bmesh.from_edit_mesh(obj.data)
vNew = vector_delta - obj.location
nVert = bm.verts.new(vNew)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
nVert.select_set(True)
else:
errmsg = f"{PDT_ERR_EDIT_MODE} {obj.mode})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif oper == "EV" and obj.mode == "EDIT":
vNew = vector_delta - obj_loc
nVert = bm.verts.new(vNew)
for v in [v for v in bm.verts if v.select]:
bm.edges.new([v, nVert])
v.select_set(False)
nVert.select_set(True)
bm.select_history.clear()
bmesh.ops.remove_doubles(bm, verts=[v for v in bm.verts if v.select], dist=0.0001)
bmesh.update_edit_mesh(obj.data)
else:
errmsg = f"{oper} {PDT_ERR_NON_VALID} {PDT_LAB_ABS}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
class PDT_OT_PlacementDelta(Operator):
"""Use Delta, or Incremental Placement."""
bl_idname = "pdt.delta"
bl_label = "Delta Mode"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Manipulates Geometry, or Objects by Delta Offset (Increment).
- Reads pg.operation from Operation Mode Selector as 'oper'
- Reads pg.select, pg.plane, pg.cartesian_coords scene variables to:
-- set position of CUrsor (CU)
-- set position of Pivot Point (PP)
-- MoVe geometry/objects (MV)
-- Extrude Vertices (EV)
-- Split Edges (SE)
-- add a New Vertex (NV)
-- Duplicate Geometry (DG)
-- Extrude Geometry (EG)
Invalid Options result in self.report Error.
Local vector variable 'vector_delta' used to reposition features.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
x_loc = pg.cartesian_coords.x
y_loc = pg.cartesian_coords.y
z_loc = pg.cartesian_coords.z
mode_s = pg.select
oper = pg.operation
if pg.plane == "LO":
vector_delta = viewCoords(x_loc, y_loc, z_loc)
else:
vector_delta = Vector((x_loc, y_loc, z_loc))
if mode_s == "REL" and oper == "CU":
scene.cursor.location = scene.cursor.location + vector_delta
elif mode_s == "REL" and oper == "PP":
pg.pivot_loc = pg.pivot_loc + vector_delta
else:
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
obj_loc = obj.matrix_world.decompose()[0]
if obj.mode == "EDIT":
bm = bmesh.from_edit_mesh(obj.data)
if oper not in {"MV", "SE", "EV", "DG", "EG"}:
if len(bm.select_history) >= 1:
actV = checkSelection(1, bm, obj)
if actV is None:
errmsg = PDT_ERR_VERT_MODE
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
errmsg = f"{PDT_ERR_SEL_1_VERTI} {len(bm.select_history)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if oper not in {"CU", "PP", "NV"} and obj.mode == "EDIT":
verts = [v for v in bm.verts if v.select]
if len(verts) == 0:
errmsg = PDT_ERR_NO_ACT_VERTS
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if oper == "CU":
if obj.mode == "EDIT":
scene.cursor.location = obj_loc + actV + vector_delta
elif obj.mode == "OBJECT":
scene.cursor.location = obj_loc + vector_delta
elif oper == "PP":
if obj.mode == "EDIT":
pg.pivot_loc = obj_loc + actV + vector_delta
elif obj.mode == "OBJECT":
pg.pivot_loc = obj_loc + vector_delta
elif oper == "MV":
if obj.mode == "EDIT":
bmesh.ops.translate(bm, verts=verts, vec=vector_delta)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif obj.mode == "OBJECT":
for ob in context.view_layer.objects.selected:
ob.location = ob.location + vector_delta
elif oper == "SE" and obj.mode == "EDIT":
edges = [e for e in bm.edges if e.select]
faces = [f for f in bm.faces if f.select]
if len(faces) != 0:
errmsg = PDT_ERR_FACE_SEL
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if len(edges) < 1:
errmsg = f"{PDT_ERR_SEL_1_EDGE} {len(edges)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
geom = bmesh.ops.subdivide_edges(bm, edges=edges, cuts=1)
new_verts = [v for v in geom["geom_split"] if isinstance(v, bmesh.types.BMVert)]
bmesh.ops.translate(bm, verts=new_verts, vec=vector_delta)
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif oper == "NV":
if obj.mode == "EDIT":
vNew = actV + vector_delta
nVert = bm.verts.new(vNew)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
nVert.select_set(True)
else:
errmsg = f"{PDT_ERR_EDIT_MODE} {obj.mode})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif oper == "EV" and obj.mode == "EDIT":
for v in [v for v in bm.verts if v.select]:
nVert = bm.verts.new(v.co)
nVert.co = nVert.co + vector_delta
bm.edges.new([v, nVert])
v.select_set(False)
nVert.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif oper == "DG" and obj.mode == "EDIT":
ret = bmesh.ops.duplicate(
bm,
geom=(
[f for f in bm.faces if f.select]
+ [e for e in bm.edges if e.select]
+ [v for v in bm.verts if v.select]
),
use_select_history=True,
)
geom_dupe = ret["geom"]
verts_dupe = [v for v in geom_dupe if isinstance(v, bmesh.types.BMVert)]
edges_dupe = [e for e in geom_dupe if isinstance(e, bmesh.types.BMEdge)]
faces_dupe = [f for f in geom_dupe if isinstance(f, bmesh.types.BMFace)]
del ret
bmesh.ops.translate(bm, verts=verts_dupe, vec=vector_delta)
updateSel(bm, verts_dupe, edges_dupe, faces_dupe)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif oper == "EG" and obj.mode == "EDIT":
ret = bmesh.ops.extrude_face_region(
bm,
geom=(
[f for f in bm.faces if f.select]
+ [e for e in bm.edges if e.select]
+ [v for v in bm.verts if v.select]
),
use_select_history=True,
)
geom_extr = ret["geom"]
verts_extr = [v for v in geom_extr if isinstance(v, bmesh.types.BMVert)]
edges_extr = [e for e in geom_extr if isinstance(e, bmesh.types.BMEdge)]
faces_extr = [f for f in geom_extr if isinstance(f, bmesh.types.BMFace)]
del ret
bmesh.ops.translate(bm, verts=verts_extr, vec=vector_delta)
updateSel(bm, verts_extr, edges_extr, faces_extr)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
else:
errmsg = f"{oper} {PDT_ERR_NON_VALID} {PDT_LAB_DEL}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
class PDT_OT_PlacementDis(Operator):
"""Use Directional, or Distance @ Angle Placement."""
bl_idname = "pdt.distance"
bl_label = "Distance@Angle Mode"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Manipulates Geometry, or Objects by Distance at Angle (Direction).
- Reads pg.operation from Operation Mode Selector as 'oper'
- Reads pg.select, pg.distance, pg.angle, pg.plane & pg.flip_angle scene variables to:
-- set position of CUrsor (CU)
-- set position of Pivot Point (PP)
-- MoVe geometry/objects (MV)
-- Extrude Vertices (EV)
-- Split Edges (SE)
-- add a New Vertex (NV)
-- Duplicate Geometry (DG)
-- Extrude Geometry (EG)
Invalid Options result in self.report Error.
Local vector variable 'vector_delta' used to reposition features.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
dis_v = pg.distance
ang_v = pg.angle
plane = pg.plane
mode_s = pg.select
oper = pg.operation
flip_a = pg.flip_angle
if flip_a:
if ang_v > 0:
ang_v = ang_v - 180
else:
ang_v = ang_v + 180
pg.angle = ang_v
if plane == "LO":
vector_delta = viewDir(dis_v, ang_v)
else:
a1, a2, _ = setMode(plane)
vector_delta = Vector((0, 0, 0))
vector_delta[a1] = vector_delta[a1] + (dis_v * cos(ang_v * pi / 180))
vector_delta[a2] = vector_delta[a2] + (dis_v * sin(ang_v * pi / 180))
if mode_s == "REL" and oper == "CU":
scene.cursor.location = scene.cursor.location + vector_delta
elif mode_s == "REL" and oper == "PP":
pg.pivot_loc = pg.pivot_loc + vector_delta
else:
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
obj_loc = obj.matrix_world.decompose()[0]
if obj.mode == "EDIT":
bm = bmesh.from_edit_mesh(obj.data)
if oper not in {"MV", "SE", "EV", "DG", "EG"}:
if len(bm.select_history) >= 1:
actV = checkSelection(1, bm, obj)
if actV is None:
errmsg = PDT_ERR_VERT_MODE
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
errmsg = f"{PDT_ERR_SEL_1_VERTI} {len(bm.select_history)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if oper not in {"CU", "PP", "NV"} and obj.mode == "EDIT":
verts = [v for v in bm.verts if v.select]
if len(verts) == 0:
errmsg = PDT_ERR_NO_ACT_VERTS
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if oper == "CU":
if obj.mode == "EDIT":
scene.cursor.location = obj_loc + actV + vector_delta
elif obj.mode == "OBJECT":
scene.cursor.location = obj_loc + vector_delta
elif oper == "PP":
if obj.mode == "EDIT":
pg.pivot_loc = obj_loc + actV + vector_delta
elif obj.mode == "OBJECT":
pg.pivot_loc = obj_loc + vector_delta
elif oper == "MV":
if obj.mode == "EDIT":
bmesh.ops.translate(bm, verts=verts, vec=vector_delta)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif obj.mode == "OBJECT":
for ob in context.view_layer.objects.selected:
ob.location = ob.location + vector_delta
elif oper == "SE" and obj.mode == "EDIT":
edges = [e for e in bm.edges if e.select]
faces = [f for f in bm.faces if f.select]
if len(faces) != 0:
errmsg = PDT_ERR_FACE_SEL
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if len(edges) < 1:
errmsg = f"{PDT_ERR_SEL_1_EDGE} {len(edges)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
geom = bmesh.ops.subdivide_edges(bm, edges=edges, cuts=1)
new_verts = [v for v in geom["geom_split"] if isinstance(v, bmesh.types.BMVert)]
bmesh.ops.translate(bm, verts=new_verts, vec=vector_delta)
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif oper == "NV":
if obj.mode == "EDIT":
vNew = actV + vector_delta
nVert = bm.verts.new(vNew)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
nVert.select_set(True)
else:
errmsg = f"{PDT_ERR_EDIT_MODE} {obj.mode})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif oper == "EV" and obj.mode == "EDIT":
for v in [v for v in bm.verts if v.select]:
nVert = bm.verts.new(v.co)
nVert.co = nVert.co + vector_delta
bm.edges.new([v, nVert])
v.select_set(False)
nVert.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif oper == "DG" and obj.mode == "EDIT":
ret = bmesh.ops.duplicate(
bm,
geom=(
[f for f in bm.faces if f.select]
+ [e for e in bm.edges if e.select]
+ [v for v in bm.verts if v.select]
),
use_select_history=True,
)
geom_dupe = ret["geom"]
verts_dupe = [v for v in geom_dupe if isinstance(v, bmesh.types.BMVert)]
edges_dupe = [e for e in geom_dupe if isinstance(e, bmesh.types.BMEdge)]
faces_dupe = [f for f in geom_dupe if isinstance(f, bmesh.types.BMFace)]
del ret
bmesh.ops.translate(bm, verts=verts_dupe, vec=vector_delta)
updateSel(bm, verts_dupe, edges_dupe, faces_dupe)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif oper == "EG" and obj.mode == "EDIT":
ret = bmesh.ops.extrude_face_region(
bm,
geom=(
[f for f in bm.faces if f.select]
+ [e for e in bm.edges if e.select]
+ [v for v in bm.verts if v.select]
),
use_select_history=True,
)
geom_extr = ret["geom"]
verts_extr = [v for v in geom_extr if isinstance(v, bmesh.types.BMVert)]
edges_extr = [e for e in geom_extr if isinstance(e, bmesh.types.BMEdge)]
faces_extr = [f for f in geom_extr if isinstance(f, bmesh.types.BMFace)]
del ret
bmesh.ops.translate(bm, verts=verts_extr, vec=vector_delta)
updateSel(bm, verts_extr, edges_extr, faces_extr)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
else:
errmsg = f"{oper} {PDT_ERR_NON_VALID} {PDT_LAB_DIR}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
class PDT_OT_PlacementPer(Operator):
"""Use Percentage Placement."""
bl_idname = "pdt.percent"
bl_label = "Percentage Mode"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Manipulates Geometry, or Objects by Percentage between 2 points.
- Reads pg.operation from Operation Mode Selector as 'oper'
- Reads pg.percent, pg.extend & pg.flip_percent scene variables to:
-- set position of CUrsor (CU)
-- set position of Pivot Point (PP)
-- MoVe geometry/objects (MV)
-- Extrude Vertices (EV)
-- Split edges (SE)
-- add a New vertex (NV)
Invalid Options result in self.report Error.
Local vector variable 'vector_delta' used to reposition features.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
per_v = pg.percent
oper = pg.operation
ext_a = pg.extend
flip_p = pg.flip_percent
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if obj.mode == "EDIT":
bm = bmesh.from_edit_mesh(obj.data)
obj_loc = obj.matrix_world.decompose()[0]
vector_delta = getPercent(obj, flip_p, per_v, oper, scene)
if vector_delta is None:
return {"FINISHED"}
if oper == "CU":
if obj.mode == "EDIT":
scene.cursor.location = obj_loc + vector_delta
elif obj.mode == "OBJECT":
scene.cursor.location = vector_delta
elif oper == "PP":
if obj.mode == "EDIT":
pg.pivot_loc = obj_loc + vector_delta
elif obj.mode == "OBJECT":
pg.pivot_loc = vector_delta
elif oper == "MV":
if obj.mode == "EDIT":
bm.select_history[-1].co = vector_delta
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif obj.mode == "OBJECT":
obj.location = vector_delta
elif oper == "SE" and obj.mode == "EDIT":
edges = [e for e in bm.edges if e.select]
if len(edges) != 1:
errmsg = f"{PDT_ERR_SEL_1_EDGE} {len(edges)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
geom = bmesh.ops.subdivide_edges(bm, edges=edges, cuts=1)
new_verts = [v for v in geom["geom_split"] if isinstance(v, bmesh.types.BMVert)]
nVert = new_verts[0]
nVert.co = vector_delta
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
nVert.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif oper == "NV":
if obj.mode == "EDIT":
nVert = bm.verts.new(vector_delta)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
nVert.select_set(True)
else:
errmsg = f"{PDT_ERR_EDIT_MODE} {obj.mode})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif oper == "EV" and obj.mode == "EDIT":
nVert = bm.verts.new(vector_delta)
if ext_a:
for v in [v for v in bm.verts if v.select]:
bm.edges.new([v, nVert])
v.select_set(False)
else:
bm.edges.new([bm.select_history[-1], nVert])
nVert.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
else:
errmsg = f"{oper} {PDT_ERR_NON_VALID} {PDT_LAB_PERCENT}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
class PDT_OT_PlacementNormal(Operator):
"""Use Normal, or Perpendicular Placement."""
bl_idname = "pdt.normal"
bl_label = "Normal Mode"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Manipulates Geometry, or Objects by Normal Intersection between 3 points.
- Reads pg.operation from Operation Mode Selector as 'oper'
- Reads pg.extend scene variable to:
-- set position of CUrsor (CU)
-- set position of Pivot Point (PP)
-- MoVe geometry/objects (MV)
-- Extrude Vertices (EV)
-- Split Edges (SE)
-- add a New Vertex (NV)
Invalid Options result in self.report Error.
Local vector variable 'vector_delta' used to reposition features.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
oper = pg.operation
ext_a = pg.extend
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
obj_loc = obj.matrix_world.decompose()[0]
if obj.mode == "EDIT":
bm = bmesh.from_edit_mesh(obj.data)
if len(bm.select_history) == 3:
actV, othV, lstV = checkSelection(3, bm, obj)
if actV is None:
errmsg = PDT_ERR_VERT_MODE
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
errmsg = f"{PDT_ERR_SEL_3_VERTS} {len(bm.select_history)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif obj.mode == "OBJECT":
objs = context.view_layer.objects.selected
if len(objs) != 3:
errmsg = f"{PDT_ERR_SEL_3_OBJS} {len(objs)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
objs_s = [ob for ob in objs if ob.name != obj.name]
actV = obj.matrix_world.decompose()[0]
othV = objs_s[-1].matrix_world.decompose()[0]
lstV = objs_s[-2].matrix_world.decompose()[0]
vector_delta = intersect_point_line(actV, othV, lstV)[0]
if oper == "CU":
if obj.mode == "EDIT":
scene.cursor.location = obj_loc + vector_delta
elif obj.mode == "OBJECT":
scene.cursor.location = vector_delta
elif oper == "PP":
if obj.mode == "EDIT":
pg.pivot_loc = obj_loc + vector_delta
elif obj.mode == "OBJECT":
pg.pivot_loc = vector_delta
elif oper == "MV":
if obj.mode == "EDIT":
if ext_a:
for v in [v for v in bm.verts if v.select]:
v.co = vector_delta
bm.select_history.clear()
bmesh.ops.remove_doubles(
bm, verts=[v for v in bm.verts if v.select], dist=0.0001
)
else:
bm.select_history[-1].co = vector_delta
bm.select_history.clear()
bmesh.update_edit_mesh(obj.data)
elif obj.mode == "OBJECT":
context.view_layer.objects.active.location = vector_delta
elif oper == "NV":
if obj.mode == "EDIT":
nVert = bm.verts.new(vector_delta)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
nVert.select_set(True)
else:
errmsg = f"{PDT_ERR_EDIT_MODE} {obj.mode})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif oper == "EV" and obj.mode == "EDIT":
vNew = vector_delta
nVert = bm.verts.new(vNew)
if ext_a:
for v in [v for v in bm.verts if v.select]:
bm.edges.new([v, nVert])
else:
bm.edges.new([bm.select_history[-1], nVert])
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
nVert.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
else:
errmsg = f"{oper} {PDT_ERR_NON_VALID} {PDT_LAB_NOR}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
class PDT_OT_PlacementInt(Operator):
"""Use Intersection, or Convergence Placement."""
bl_idname = "pdt.intersect"
bl_label = "Intersect Mode"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Manipulates Geometry, or Objects by Convergance Intersection between 4 points, or 2 Edges.
- Reads pg.operation from Operation Mode Selector as 'oper'
- Reads pg.plane scene variable and operates in Working Plane to:
-- set position of CUrsor (CU)
-- set position of Pivot Point (PP)
-- MoVe geometry/objects (MV)
-- Extrude Vertices (EV)
-- add a New vertex (NV)
Invalid Options result in self.report Error.
Local vector variable 'vector_delta' used to reposition features.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
oper = pg.operation
plane = pg.plane
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if obj.mode == "EDIT":
obj_loc = obj.matrix_world.decompose()[0]
bm = bmesh.from_edit_mesh(obj.data)
edges = [e for e in bm.edges if e.select]
if len(bm.select_history) == 4:
ext_a = pg.extend
va = bm.select_history[-1]
vo = bm.select_history[-2]
vl = bm.select_history[-3]
vf = bm.select_history[-4]
actV, othV, lstV, fstV = checkSelection(4, bm, obj)
if actV is None:
errmsg = PDT_ERR_VERT_MODE
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif len(edges) == 2:
ext_a = pg.extend
va = edges[0].verts[0]
vo = edges[0].verts[1]
vl = edges[1].verts[0]
vf = edges[1].verts[1]
else:
errmsg = (
PDT_ERR_SEL_4_VERTS
+ str(len(bm.select_history))
+ " Vertices/"
+ str(len(edges))
+ " Edges)"
)
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
vector_delta, done = intersection(va.co, vo.co, vl.co, vf.co, plane)
if not done:
errmsg = f"{PDT_ERR_INT_LINES} {plane} {PDT_LAB_PLANE}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if oper == "CU":
scene.cursor.location = obj_loc + vector_delta
elif oper == "PP":
pg.pivot_loc = obj_loc + vector_delta
elif oper == "NV":
vNew = vector_delta
nVert = bm.verts.new(vNew)
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
for f in bm.faces:
f.select_set(False)
for e in bm.edges:
e.select_set(False)
nVert.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif oper in {"MV", "EV"}:
nVert = None
proc = False
if (va.co - vector_delta).length < (vo.co - vector_delta).length:
if oper == "MV":
va.co = vector_delta
proc = True
elif oper == "EV":
nVert = bm.verts.new(vector_delta)
bm.edges.new([va, nVert])
proc = True
else:
if oper == "MV" and ext_a:
vo.co = vector_delta
elif oper == "EV" and ext_a:
nVert = bm.verts.new(vector_delta)
bm.edges.new([vo, nVert])
if (vl.co - vector_delta).length < (vf.co - vector_delta).length:
if oper == "MV" and ext_a:
vl.co = vector_delta
elif oper == "EV" and ext_a:
bm.edges.new([vl, nVert])
else:
if oper == "MV" and ext_a:
vf.co = vector_delta
elif oper == "EV" and ext_a:
bm.edges.new([vf, nVert])
bm.select_history.clear()
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
if not proc and not ext_a:
errmsg = PDT_ERR_INT_NO_ALL
self.report({"ERROR"}, errmsg)
bmesh.update_edit_mesh(obj.data)
return {"FINISHED"}
else:
for v in bm.verts:
v.select_set(False)
for f in bm.faces:
f.select_set(False)
for e in bm.edges:
e.select_set(False)
if nVert is not None:
nVert.select_set(True)
for v in bm.select_history:
if v is not None:
v.select_set(True)
bmesh.update_edit_mesh(obj.data)
else:
errmsg = f"{oper} {PDT_ERR_NON_VALID} {PDT_LAB_INTERSECT}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif obj.mode == "OBJECT":
if len(context.view_layer.objects.selected) != 4:
errmsg = f"{PDT_ERR_SEL_4_OBJS} {len(context.view_layer.objects.selected)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
order = pg.object_order.split(",")
objs = sorted(
[ob for ob in context.view_layer.objects.selected], key=lambda x: x.name
)
message = (
"Original Object Order was: "
+ objs[0].name
+ ", "
+ objs[1].name
+ ", "
+ objs[2].name
+ ", "
+ objs[3].name
)
self.report({"INFO"}, message)
actV = objs[int(order[0]) - 1].matrix_world.decompose()[0]
othV = objs[int(order[1]) - 1].matrix_world.decompose()[0]
lstV = objs[int(order[2]) - 1].matrix_world.decompose()[0]
fstV = objs[int(order[3]) - 1].matrix_world.decompose()[0]
vector_delta, done = intersection(actV, othV, lstV, fstV, plane)
if not done:
errmsg = f"{PDT_ERR_INT_LINES} {plane} {PDT_LAB_PLANE}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if oper == "CU":
scene.cursor.location = vector_delta
elif oper == "PP":
pg.pivot_loc = vector_delta
elif oper == "MV":
context.view_layer.objects.active.location = vector_delta
infmsg = PDT_INF_OBJ_MOVED + message
self.report({"INFO"}, infmsg)
else:
errmsg = f"{oper} {PDT_ERR_NON_VALID} {PDT_LAB_INTERSECT}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
class PDT_OT_PlacementCen(Operator):
"""Use Placement at Arc Centre."""
bl_idname = "pdt.centre"
bl_label = "Centre Mode"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Manipulates Geometry, or Objects to an Arc Centre defined by 3 points on an Imaginary Arc.
Valid Options for pg.operation; CU PP MV NV EV
- Reads pg.operation from Operation Mode Selector as 'oper'
- Reads pg.extend scene variable to:
-- set position of CUrsor (CU)
-- set position of Pivot Point (PP)
-- MoVe geometry/objects (MV)
-- Extrude Vertices (EV)
-- add a New vertex (NV)
Invalid Options result in self.report Error.
Local vector variable 'vector_delta' used to reposition features.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
oper = pg.operation
ext_a = pg.extend
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if obj.mode == "EDIT":
obj = context.view_layer.objects.active
obj_loc = obj.matrix_world.decompose()[0]
bm = bmesh.from_edit_mesh(obj.data)
verts = [v for v in bm.verts if v.select]
if len(verts) == 3:
actV = verts[0].co
othV = verts[1].co
lstV = verts[2].co
else:
errmsg = f"{PDT_ERR_SEL_3_VERTS} {len(verts)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
vector_delta, radius = arcCentre(actV, othV, lstV)
if str(radius) == "inf":
errmsg = PDT_ERR_STRIGHT_LINE
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
pg.distance = radius
if oper == "CU":
scene.cursor.location = obj_loc + vector_delta
elif oper == "PP":
pg.pivot_loc = obj_loc + vector_delta
elif oper == "NV":
vNew = vector_delta
nVert = bm.verts.new(vNew)
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
nVert.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
nVert.select_set(True)
elif oper == "MV":
if obj.mode == "EDIT":
if ext_a:
for v in [v for v in bm.verts if v.select]:
v.co = vector_delta
bm.select_history.clear()
bmesh.ops.remove_doubles(
bm, verts=[v for v in bm.verts if v.select], dist=0.0001
)
else:
bm.select_history[-1].co = vector_delta
bm.select_history.clear()
bmesh.update_edit_mesh(obj.data)
elif obj.mode == "OBJECT":
context.view_layer.objects.active.location = vector_delta
elif oper == "EV":
nVert = bm.verts.new(vector_delta)
if ext_a:
for v in [v for v in bm.verts if v.select]:
bm.edges.new([v, nVert])
v.select_set(False)
nVert.select_set(True)
bm.select_history.clear()
bmesh.ops.remove_doubles(
bm, verts=[v for v in bm.verts if v.select], dist=0.0001
)
bmesh.update_edit_mesh(obj.data)
else:
bm.edges.new([bm.select_history[-1], nVert])
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
else:
errmsg = f"{oper} {PDT_ERR_NON_VALID} {PDT_LAB_ARCCENTRE}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif obj.mode == "OBJECT":
if len(context.view_layer.objects.selected) != 3:
errmsg = f"{PDT_ERR_SEL_3_OBJS} {len(context.view_layer.objects.selected)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
actV = context.view_layer.objects.selected[0].matrix_world.decompose()[0]
othV = context.view_layer.objects.selected[1].matrix_world.decompose()[0]
lstV = context.view_layer.objects.selected[2].matrix_world.decompose()[0]
vector_delta, radius = arcCentre(actV, othV, lstV)
pg.distance = radius
if oper == "CU":
scene.cursor.location = vector_delta
elif oper == "PP":
pg.pivot_loc = vector_delta
elif oper == "MV":
context.view_layer.objects.active.location = vector_delta
else:
errmsg = f"{oper} {PDT_ERR_NON_VALID} {PDT_LAB_ARCCENTRE}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
class PDT_OT_JoinVerts(Operator):
"""Join 2 Free Vertices into an Edge."""
bl_idname = "pdt.join"
bl_label = "Join 2 Vertices"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
ob = context.object
if ob is None:
return False
return all([bool(ob), ob.type == "MESH", ob.mode == "EDIT"])
def execute(self, context):
"""Joins 2 Free Vertices that do not form part of a Face.
Joins two vertices that do not form part of a single face
It is designed to close open Edge Loops, where a face is not required
or to join two disconnected Edges.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
obj = context.view_layer.objects.active
bm = bmesh.from_edit_mesh(obj.data)
verts = [v for v in bm.verts if v.select]
if len(verts) == 2:
try:
bm.edges.new([verts[-1], verts[-2]])
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
return {"FINISHED"}
except ValueError:
errmsg = PDT_ERR_CONNECTED
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
errmsg = f"{PDT_ERR_SEL_2_VERTS} {len(verts)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
class PDT_OT_Fillet(Operator):
"""Fillet Edges by Vertex, Set Use Verts to False for Extruded Structure."""
bl_idname = "pdt.fillet"
bl_label = "Fillet"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
ob = context.object
if ob is None:
return False
return all([bool(ob), ob.type == "MESH", ob.mode == "EDIT"])
def execute(self, context):
"""Create Fillets by Vertex or by Geometry.
Fillets connected edges, or connected faces
Uses:
- pg.fillet_radius ; Radius of fillet
- pg.fillet_segments ; Number of segments
- pg.fillet_profile ; Profile, values 0 to 1
- pg.fillet_vertices_only ; Vertices (True), or Face/Edges
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
plane = pg.plane
obj = context.view_layer.objects.active
bm = bmesh.from_edit_mesh(obj.data)
verts = [v for v in bm.verts if v.select]
if pg.fillet_int:
# Fillet & Intersect Two Edges
edges = [e for e in bm.edges if e.select]
if len(edges) == 2 and len(verts) == 4:
va = edges[0].verts[0]
vo = edges[0].verts[1]
vl = edges[1].verts[0]
vf = edges[1].verts[1]
vector_delta, done = intersection(va.co, vo.co, vl.co, vf.co, plane)
if not done:
errmsg = f"{PDT_ERR_INT_LINES} {plane} {PDT_LAB_PLANE}"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if (va.co - vector_delta).length < (vo.co - vector_delta).length:
va.co = vector_delta
vo.select_set(False)
else:
vo.co = vector_delta
va.select_set(False)
if (vl.co - vector_delta).length < (vf.co - vector_delta).length:
vl.co = vector_delta
vf.select_set(False)
else:
vf.co = vector_delta
vl.select_set(False)
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
bpy.ops.mesh.bevel(
offset_type="OFFSET",
offset=pg.fillet_radius,
segments=pg.fillet_segments,
profile=pg.fillet_profile,
vertex_only=True,
)
return {"FINISHED"}
else:
errmsg = f"{PDT_ERR_SEL_2_EDGES} {len(edges)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
if len(verts) == 0:
errmsg = PDT_ERR_SEL_1_VERT
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
# Intersct Edges
bpy.ops.mesh.bevel(
offset_type="OFFSET",
offset=pg.fillet_radius,
segments=pg.fillet_segments,
profile=pg.fillet_profile,
vertex_only=pg.fillet_vertices_only,
)
return {"FINISHED"}
class PDT_OT_Angle2(Operator):
"""Measure Distance and Angle in Working Plane, Also sets Deltas."""
bl_idname = "pdt.angle2"
bl_label = "Measure 2D"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Measures Angle and Offsets between 2 Points in View Plane.
Uses 2 Selected Vertices to set pg.angle and pg.distance scene variables
also sets delta offset from these 2 points using standard Numpy Routines
Works in Edit and Oject Modes.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
plane = pg.plane
flip_a = pg.flip_angle
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if obj.mode == "EDIT":
bm = bmesh.from_edit_mesh(obj.data)
verts = [v for v in bm.verts if v.select]
if len(verts) == 2:
if len(bm.select_history) == 2:
actV, othV = checkSelection(2, bm, obj)
if actV is None:
errmsg = PDT_ERR_VERT_MODE
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
errmsg = f"{PDT_ERR_SEL_2_VERTIO} {len(bm.select_history)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
errmsg = f"{PDT_ERR_SEL_2_VERTIO} {len(verts)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif obj.mode == "OBJECT":
objs = context.view_layer.objects.selected
if len(objs) < 2:
errmsg = f"{PDT_ERR_SEL_2_OBJS} {len(objs)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
objs_s = [ob for ob in objs if ob.name != obj.name]
actV = obj.matrix_world.decompose()[0]
othV = objs_s[-1].matrix_world.decompose()[0]
if plane == "LO":
disV = othV - actV
othV = viewCoordsI(disV.x, disV.y, disV.z)
actV = Vector((0, 0, 0))
v0 = np.array([actV.x + 1, actV.y]) - np.array([actV.x, actV.y])
v1 = np.array([othV.x, othV.y]) - np.array([actV.x, actV.y])
else:
a1, a2, _ = setMode(plane)
v0 = np.array([actV[a1] + 1, actV[a2]]) - np.array([actV[a1], actV[a2]])
v1 = np.array([othV[a1], othV[a2]]) - np.array([actV[a1], actV[a2]])
ang = np.rad2deg(np.arctan2(np.linalg.det([v0, v1]), np.dot(v0, v1)))
if flip_a:
if ang > 0:
pg.angle = ang - 180
else:
pg.angle = ang + 180
else:
pg.angle = ang
if plane == "LO":
pg.distance = sqrt((actV.x - othV.x) ** 2 + (actV.y - othV.y) ** 2)
else:
pg.distance = sqrt((actV[a1] - othV[a1]) ** 2 + (actV[a2] - othV[a2]) ** 2)
pg.cartesian_coords = othV - actV
return {"FINISHED"}
class PDT_OT_Angle3(Operator):
"""Measure Distance and Angle in 3D Space."""
bl_idname = "pdt.angle3"
bl_label = "Measure 3D"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Measures Angle and Offsets between 3 Points in World Space, Also sets Deltas.
Uses 3 Selected Vertices to set pg.angle and pg.distance scene variables
also sets delta offset from these 3 points using standard Numpy Routines
Works in Edit and Oject Modes.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
pg = context.scene.pdt_pg
flip_a = pg.flip_angle
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if obj.mode == "EDIT":
bm = bmesh.from_edit_mesh(obj.data)
verts = [v for v in bm.verts if v.select]
if len(verts) == 3:
if len(bm.select_history) == 3:
actV, othV, lstV = checkSelection(3, bm, obj)
if actV is None:
errmsg = PDT_ERR_VERT_MODE
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
errmsg = f"{PDT_ERR_SEL_3_VERTIO} {len(bm.select_history)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
else:
errmsg = f"{PDT_ERR_SEL_3_VERTIO} {len(verts)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
elif obj.mode == "OBJECT":
objs = context.view_layer.objects.selected
if len(objs) < 3:
errmsg = PDT_ERR_SEL_3_OBJS + str(len(objs))
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
objs_s = [ob for ob in objs if ob.name != obj.name]
actV = obj.matrix_world.decompose()[0]
othV = objs_s[-1].matrix_world.decompose()[0]
lstV = objs_s[-2].matrix_world.decompose()[0]
ba = np.array([othV.x, othV.y, othV.z]) - np.array([actV.x, actV.y, actV.z])
bc = np.array([lstV.x, lstV.y, lstV.z]) - np.array([actV.x, actV.y, actV.z])
cosA = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
ang = np.degrees(np.arccos(cosA))
if flip_a:
if ang > 0:
pg.angle = ang - 180
else:
pg.angle = ang + 180
else:
pg.angle = ang
pg.distance = (actV - othV).length
pg.cartesian_coords = othV - actV
return {"FINISHED"}
class PDT_OT_Origin(Operator):
"""Move Object Origin to Cursor Location."""
bl_idname = "pdt.origin"
bl_label = "Move Origin"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
"""Sets Object Origin in Edit Mode to Cursor Location.
Keeps geometry static in World Space whilst moving Object Origin
Requires cursor location
Works in Edit and Object Modes.
Args:
context: Blender bpy.context instance.
Returns:
Status Set.
"""
scene = context.scene
obj = context.view_layer.objects.active
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
obj_loc = obj.matrix_world.decompose()[0]
cur_loc = scene.cursor.location
diff_v = obj_loc - cur_loc
if obj.mode == "EDIT":
bm = bmesh.from_edit_mesh(obj.data)
for v in bm.verts:
v.co = v.co + diff_v
obj.location = cur_loc
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif obj.mode == "OBJECT":
for v in obj.data.vertices:
v.co = v.co + diff_v
obj.location = cur_loc
else:
errmsg = f"{PDT_ERR_EDOB_MODE} {obj.mode})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
return {"FINISHED"}
class PDT_OT_Taper(Operator):
"""Taper Vertices at Angle in Chosen Axis Mode."""
bl_idname = "pdt.taper"
bl_label = "Taper"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
ob = context.object
if ob is None:
return False
return all([bool(ob), ob.type == "MESH", ob.mode == "EDIT"])
def execute(self, context):
"""Taper Geometry along World Axes.
Similar to Shear command except that it shears by angle rather than displacement.
Rotates about World Axes and displaces along World Axes, angle must not exceed +-80 degrees.
Rotation axis is centred on Active Vertex.
Works only in Edit mode.
Args:
context: Blender bpy.context instance.
Note:
Uses pg.taper & pg.angle scene variables
Returns:
Status Set.
"""
scene = context.scene
pg = scene.pdt_pg
tap_ax = pg.taper
ang_v = pg.angle
obj = context.view_layer.objects.active
if ang_v > 80 or ang_v < -80:
errmsg = f"{PDT_ERR_TAPER_ANG} {ang_v})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
if obj is None:
errmsg = PDT_ERR_NO_ACT_OBJ
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
_, a2, a3 = setAxis(tap_ax)
bm = bmesh.from_edit_mesh(obj.data)
if len(bm.select_history) >= 1:
rotV = bm.select_history[-1]
viewV = viewCoords(rotV.co.x, rotV.co.y, rotV.co.z)
else:
errmsg = f"{PDT_ERR_TAPER_SEL} {len(bm.select_history)})"
self.report({"ERROR"}, errmsg)
return {"FINISHED"}
for v in [v for v in bm.verts if v.select]:
if pg.plane == "LO":
v_loc = viewCoords(v.co.x, v.co.y, v.co.z)
dis_v = sqrt((viewV.x - v_loc.x) ** 2 + (viewV.y - v_loc.y) ** 2)
x_loc = dis_v * tan(ang_v * pi / 180)
vm = viewDir(x_loc, 0)
v.co = v.co - vm
else:
dis_v = sqrt((rotV.co[a3] - v.co[a3]) ** 2 + (rotV.co[a2] - v.co[a2]) ** 2)
v.co[a2] = v.co[a2] - (dis_v * tan(ang_v * pi / 180))
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
return {"FINISHED"}
|