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
|
# ***** 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 numpy as np
from math import sqrt, tan, pi
from mathutils import Vector
from mathutils.geometry import intersect_point_line
from .pdt_functions import (
set_mode,
oops,
get_percent,
dis_ang,
check_selection,
arc_centre,
intersection,
view_coords_i,
view_coords,
view_dir,
set_axis,
)
from . import pdt_exception
PDT_SelectionError = pdt_exception.SelectionError
PDT_InvalidVector = pdt_exception.InvalidVector
PDT_ObjectModeError = pdt_exception.ObjectModeError
PDT_InfRadius = pdt_exception.InfRadius
PDT_NoObjectError = pdt_exception.NoObjectError
PDT_IntersectionError = pdt_exception.IntersectionError
PDT_InvalidOperation = pdt_exception.InvalidOperation
PDT_VerticesConnected = pdt_exception.VerticesConnected
PDT_InvalidAngle = pdt_exception.InvalidAngle
from .pdt_msg_strings import (
PDT_ERR_BAD3VALS,
PDT_ERR_BAD2VALS,
PDT_ERR_BAD1VALS,
PDT_ERR_CONNECTED,
PDT_ERR_SEL_2_VERTS,
PDT_ERR_EDOB_MODE,
PDT_ERR_NO_ACT_OBJ,
PDT_ERR_VERT_MODE,
PDT_ERR_SEL_3_VERTS,
PDT_ERR_SEL_3_OBJS,
PDT_ERR_EDIT_MODE,
PDT_ERR_NON_VALID,
PDT_LAB_NOR,
PDT_ERR_STRIGHT_LINE,
PDT_LAB_ARCCENTRE,
PDT_ERR_SEL_4_VERTS,
PDT_ERR_INT_NO_ALL,
PDT_LAB_INTERSECT,
PDT_ERR_SEL_4_OBJS,
PDT_INF_OBJ_MOVED,
PDT_ERR_SEL_2_VERTIO,
PDT_ERR_SEL_2_OBJS,
PDT_ERR_SEL_3_VERTIO,
PDT_ERR_TAPER_ANG,
PDT_ERR_TAPER_SEL,
PDT_ERR_INT_LINES,
PDT_LAB_PLANE,
)
def vector_build(context, pg, obj, operation, values, num_values):
"""Build Movement Vector from input Fields.
Args:
context: Blender bpy.context instance.
PDT parameter group as pg, object, operation,
command line values, required number of values.
Returns:
Vector to position, or offset items.
"""
scene = context.scene
plane = pg.plane
flip_a = pg.flip_angle
flip_p = pg.flip_percent
# Cartesian 3D coordinates
if num_values == 3 and len(values) == 3:
output_vector = Vector((float(values[0]), float(values[1]), float(values[2])))
# Polar 2D coordinates
elif num_values == 2 and len(values) == 2:
output_vector = dis_ang(values, flip_a, plane, scene)
# Percentage of imaginary line between two 3D coordinates
elif num_values == 1 and len(values) == 1:
output_vector = get_percent(obj, flip_p, float(values[0]), operation, scene)
else:
if num_values == 3:
pg.error = PDT_ERR_BAD3VALS
elif num_values == 2:
pg.error = PDT_ERR_BAD2VALS
else:
pg.error = PDT_ERR_BAD1VALS
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_InvalidVector
return output_vector
def placement_normal(context, operation):
"""Manipulates Geometry, or Objects by Normal Intersection between 3 points.
-- 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 "oops" 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
ext_a = pg.extend
obj = context.view_layer.objects.active
if obj.mode == "EDIT":
if obj is None:
pg.error = PDT_ERR_NO_ACT_OBJ
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_ObjectModeError
obj_loc = obj.matrix_world.decompose()[0]
bm = bmesh.from_edit_mesh(obj.data)
if len(bm.select_history) == 3:
vector_a, vector_b, vector_c = check_selection(3, bm, obj)
if vector_a is None:
pg.error = PDT_ERR_VERT_MODE
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_InvalidVector
else:
pg.error = f"{PDT_ERR_SEL_3_VERTIO} {len(bm.select_history)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
elif obj.mode == "OBJECT":
objs = context.view_layer.objects.selected
if len(objs) != 3:
pg.error = f"{PDT_ERR_SEL_3_OBJS} {len(objs)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
objs_s = [ob for ob in objs if ob.name != obj.name]
vector_a = obj.matrix_world.decompose()[0]
vector_b = objs_s[-1].matrix_world.decompose()[0]
vector_c = objs_s[-2].matrix_world.decompose()[0]
vector_delta = intersect_point_line(vector_a, vector_b, vector_c)[0]
if operation == "C":
if obj.mode == "EDIT":
scene.cursor.location = obj_loc + vector_delta
elif obj.mode == "OBJECT":
scene.cursor.location = vector_delta
elif operation == "P":
if obj.mode == "EDIT":
pg.pivot_loc = obj_loc + vector_delta
elif obj.mode == "OBJECT":
pg.pivot_loc = vector_delta
elif operation == "G":
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 operation == "N":
if obj.mode == "EDIT":
vertex_new = 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)
vertex_new.select_set(True)
else:
pg.error = f"{PDT_ERR_EDIT_MODE} {obj.mode})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
return
elif operation == "V" and obj.mode == "EDIT":
vector_new = vector_delta
vertex_new = bm.verts.new(vector_new)
if ext_a:
for v in [v for v in bm.verts if v.select]:
bm.edges.new([v, vertex_new])
else:
bm.edges.new([bm.select_history[-1], vertex_new])
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
vertex_new.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
else:
pg.error = f"{operation} {PDT_ERR_NON_VALID} {PDT_LAB_NOR}"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
def placement_arc_centre(context, operation):
"""Manipulates Geometry, or Objects to an Arc Centre defined by 3 points on an Imaginary Arc.
-- 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 "oops" 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
ext_a = pg.extend
obj = context.view_layer.objects.active
if obj.mode == "EDIT":
if obj is None:
pg.error = PDT_ERR_NO_ACT_OBJ
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_ObjectModeError
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:
pg.error = f"{PDT_ERR_SEL_3_VERTS} {len(verts)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
vector_a = verts[0].co
vector_b = verts[1].co
vector_c = verts[2].co
vector_delta, radius = arc_centre(vector_a, vector_b, vector_c)
if str(radius) == "inf":
pg.error = PDT_ERR_STRIGHT_LINE
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_InfRadius
pg.distance = radius
if operation == "C":
scene.cursor.location = obj_loc + vector_delta
elif operation == "P":
pg.pivot_loc = obj_loc + vector_delta
elif operation == "N":
vector_new = vector_delta
vertex_new = bm.verts.new(vector_new)
for v in [v for v in bm.verts if v.select]:
v.select_set(False)
vertex_new.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
vertex_new.select_set(True)
elif operation == "G":
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 operation == "V":
vertex_new = bm.verts.new(vector_delta)
if ext_a:
for v in [v for v in bm.verts if v.select]:
bm.edges.new([v, vertex_new])
v.select_set(False)
vertex_new.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], vertex_new])
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
else:
pg.error = f"{operation} {PDT_ERR_NON_VALID} {PDT_LAB_ARCCENTRE}"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
elif obj.mode == "OBJECT":
if len(context.view_layer.objects.selected) != 3:
pg.error = f"{PDT_ERR_SEL_3_OBJS} {len(context.view_layer.objects.selected)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
vector_a = context.view_layer.objects.selected[0].matrix_world.decompose()[0]
vector_b = context.view_layer.objects.selected[1].matrix_world.decompose()[0]
vector_c = context.view_layer.objects.selected[2].matrix_world.decompose()[0]
vector_delta, radius = arc_centre(vector_a, vector_b, vector_c)
pg.distance = radius
if operation == "C":
scene.cursor.location = vector_delta
elif operation == "P":
pg.pivot_loc = vector_delta
elif operation == "G":
context.view_layer.objects.active.location = vector_delta
else:
pg.error = f"{operation} {PDT_ERR_NON_VALID} {PDT_LAB_ARCCENTRE}"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
def placement_intersect(context, operation):
"""Manipulates Geometry, or Objects by Convergance Intersection between 4 points, or 2 Edges.
- 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 "oops" 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
plane = pg.plane
obj = context.view_layer.objects.active
if obj.mode == "EDIT":
if obj is None:
pg.error = PDT_ERR_NO_ACT_OBJ
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_NoObjectError
obj_loc = obj.matrix_world.decompose()[0]
bm = bmesh.from_edit_mesh(obj.data)
edges = [e for e in bm.edges if e.select]
ext_a = pg.extend
if len(edges) == 2:
vertex_a = edges[0].verts[0]
vertex_b = edges[0].verts[1]
vertex_c = edges[1].verts[0]
vertex_d = edges[1].verts[1]
else:
if len(bm.select_history) != 4:
pg.error = (
PDT_ERR_SEL_4_VERTS
+ str(len(bm.select_history))
+ " Vertices/"
+ str(len(edges))
+ " Edges)"
)
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
vertex_a = bm.select_history[-1]
vertex_b = bm.select_history[-2]
vertex_c = bm.select_history[-3]
vertex_d = bm.select_history[-4]
vector_delta, done = intersection(vertex_a.co, vertex_b.co, vertex_c.co, vertex_d.co, plane)
if not done:
pg.error = f"{PDT_ERR_INT_LINES} {plane} {PDT_LAB_PLANE}"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_IntersectionError
if operation == "C":
scene.cursor.location = obj_loc + vector_delta
elif operation == "P":
pg.pivot_loc = obj_loc + vector_delta
elif operation == "N":
vector_new = vector_delta
vertex_new = bm.verts.new(vector_new)
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)
vertex_new.select_set(True)
bmesh.update_edit_mesh(obj.data)
bm.select_history.clear()
elif operation in {"G", "V"}:
vertex_new = None
proc = False
if (vertex_a.co - vector_delta).length < (vertex_b.co - vector_delta).length:
if operation == "G":
vertex_a.co = vector_delta
proc = True
else:
vertex_new = bm.verts.new(vector_delta)
bm.edges.new([vertex_a, vertex_new])
proc = True
else:
if operation == "G" and ext_a:
vertex_b.co = vector_delta
elif operation == "V" and ext_a:
vertex_new = bm.verts.new(vector_delta)
bm.edges.new([vertex_b, vertex_new])
else:
return
if (vertex_c.co - vector_delta).length < (vertex_d.co - vector_delta).length:
if operation == "G" and ext_a:
vertex_c.co = vector_delta
elif operation == "V" and ext_a:
bm.edges.new([vertex_c, vertex_new])
else:
return
else:
if operation == "G" and ext_a:
vertex_d.co = vector_delta
elif operation == "V" and ext_a:
bm.edges.new([vertex_d, vertex_new])
else:
return
bm.select_history.clear()
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
if not proc and not ext_a:
pg.error = PDT_ERR_INT_NO_ALL
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
bmesh.update_edit_mesh(obj.data)
return
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 vertex_new is not None:
vertex_new.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:
pg.error = f"{operation} {PDT_ERR_NON_VALID} {PDT_LAB_INTERSECT}"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_InvalidOperation
elif obj.mode == "OBJECT":
if len(context.view_layer.objects.selected) != 4:
pg.error = f"{PDT_ERR_SEL_4_OBJS} {len(context.view_layer.objects.selected)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
order = pg.object_order.split(",")
objs = sorted(context.view_layer.objects.selected, key=lambda x: x.name)
pg.error = (
"Original Object Order (1,2,3,4) was: "
+ objs[0].name
+ ", "
+ objs[1].name
+ ", "
+ objs[2].name
+ ", "
+ objs[3].name
)
context.window_manager.popup_menu(oops, title="Info", icon="INFO")
vector_a = objs[int(order[0]) - 1].matrix_world.decompose()[0]
vector_b = objs[int(order[1]) - 1].matrix_world.decompose()[0]
vector_c = objs[int(order[2]) - 1].matrix_world.decompose()[0]
vector_d = objs[int(order[3]) - 1].matrix_world.decompose()[0]
vector_delta, done = intersection(vector_a, vector_b, vector_c, vector_d, plane)
if not done:
pg.error = f"{PDT_ERR_INT_LINES} {plane} {PDT_LAB_PLANE}"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_IntersectionError
if operation == "C":
scene.cursor.location = vector_delta
elif operation == "P":
pg.pivot_loc = vector_delta
elif operation == "G":
context.view_layer.objects.active.location = vector_delta
pg.error = f"{PDT_INF_OBJ_MOVED} {context.view_layer.objects.active.name}"
context.window_manager.popup_menu(oops, title="Info", icon="INFO")
else:
pg.error = f"{operation} {PDT_ERR_NON_VALID} {PDT_LAB_INTERSECT}"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
return
else:
return
def join_two_vertices(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.
"""
scene = context.scene
pg = scene.pdt_pg
obj = context.view_layer.objects.active
if all([bool(obj), obj.type == "MESH", obj.mode == "EDIT"]):
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
except ValueError:
pg.error = PDT_ERR_CONNECTED
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_VerticesConnected
else:
pg.error = f"{PDT_ERR_SEL_2_VERTS} {len(verts)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
else:
pg.error = f"{PDT_ERR_EDOB_MODE},{obj.mode})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_ObjectModeError
def set_angle_distance_two(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:
pg.error = PDT_ERR_NO_ACT_OBJ
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
return
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:
vector_a, vector_b = check_selection(2, bm, obj)
if vector_a is None:
pg.error = PDT_ERR_VERT_MODE
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_InvalidVector
else:
pg.error = f"{PDT_ERR_SEL_2_VERTIO} {len(bm.select_history)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
else:
pg.error = f"{PDT_ERR_SEL_2_VERTIO} {len(verts)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
elif obj.mode == "OBJECT":
objs = context.view_layer.objects.selected
if len(objs) < 2:
pg.error = f"{PDT_ERR_SEL_2_OBJS} {len(objs)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
objs_s = [ob for ob in objs if ob.name != obj.name]
vector_a = obj.matrix_world.decompose()[0]
vector_b = objs_s[-1].matrix_world.decompose()[0]
if plane == "LO":
vector_difference = vector_b - vector_a
vector_b = view_coords_i(vector_difference.x, vector_difference.y, vector_difference.z)
vector_a = Vector((0, 0, 0))
v0 = np.array([vector_a.x + 1, vector_a.y]) - np.array([vector_a.x, vector_a.y])
v1 = np.array([vector_b.x, vector_b.y]) - np.array([vector_a.x, vector_a.y])
else:
a1, a2, _ = set_mode(plane)
v0 = np.array([vector_a[a1] + 1, vector_a[a2]]) - np.array([vector_a[a1], vector_a[a2]])
v1 = np.array([vector_b[a1], vector_b[a2]]) - np.array([vector_a[a1], vector_a[a2]])
ang = np.rad2deg(np.arctan2(np.linalg.det([v0, v1]), np.dot(v0, v1)))
val_round = context.preferences.addons[__package__].preferences.pdt_input_round
if flip_a:
if ang > 0:
pg.angle = round(ang - 180, val_round)
else:
pg.angle = round(ang - 180, val_round)
else:
pg.angle = round(ang, val_round)
if plane == "LO":
pg.distance = round(sqrt(
(vector_a.x - vector_b.x) ** 2 +
(vector_a.y - vector_b.y) ** 2), val_round)
else:
pg.distance = round(sqrt(
(vector_a[a1] - vector_b[a1]) ** 2 +
(vector_a[a2] - vector_b[a2]) ** 2), val_round)
pg.cartesian_coords = Vector(([round(i, val_round) for i in vector_b - vector_a]))
def set_angle_distance_three(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:
pg.error = PDT_ERR_NO_ACT_OBJ
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_NoObjectError
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:
vector_a, vector_b, vector_c = check_selection(3, bm, obj)
if vector_a is None:
pg.error = PDT_ERR_VERT_MODE
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_InvalidVector
else:
pg.error = f"{PDT_ERR_SEL_3_VERTIO} {len(bm.select_history)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
else:
pg.error = f"{PDT_ERR_SEL_3_VERTIO} {len(verts)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
elif obj.mode == "OBJECT":
objs = context.view_layer.objects.selected
if len(objs) < 3:
pg.error = PDT_ERR_SEL_3_OBJS + str(len(objs))
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
objs_s = [ob for ob in objs if ob.name != obj.name]
vector_a = obj.matrix_world.decompose()[0]
vector_b = objs_s[-1].matrix_world.decompose()[0]
vector_c = objs_s[-2].matrix_world.decompose()[0]
ba = np.array([vector_b.x, vector_b.y, vector_b.z]) - np.array(
[vector_a.x, vector_a.y, vector_a.z]
)
bc = np.array([vector_c.x, vector_c.y, vector_c.z]) - np.array(
[vector_a.x, vector_a.y, vector_a.z]
)
angle_cosine = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
ang = np.degrees(np.arccos(angle_cosine))
val_round = context.preferences.addons[__package__].preferences.pdt_input_round
if flip_a:
if ang > 0:
pg.angle = round(ang - 180, val_round)
else:
pg.angle = round(ang - 180, val_round)
else:
pg.angle = round(ang, val_round)
pg.distance = round((vector_a - vector_b).length, val_round)
pg.cartesian_coords = Vector(([round(i, val_round) for i in vector_b - vector_a]))
def origin_to_cursor(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
pg = context.scene.pdt_pg
obj = context.view_layer.objects.active
if obj is None:
pg.error = PDT_ERR_NO_ACT_OBJ
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
return
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:
pg.error = f"{PDT_ERR_EDOB_MODE} {obj.mode})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_ObjectModeError
def taper(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 all([bool(obj), obj.type == "MESH", obj.mode == "EDIT"]):
if ang_v > 80 or ang_v < -80:
pg.error = f"{PDT_ERR_TAPER_ANG} {ang_v})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_InvalidAngle
if obj is None:
pg.error = PDT_ERR_NO_ACT_OBJ
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_NoObjectError
_, a2, a3 = set_axis(tap_ax)
bm = bmesh.from_edit_mesh(obj.data)
if len(bm.select_history) >= 1:
rotate_vertex = bm.select_history[-1]
view_vector = view_coords(rotate_vertex.co.x, rotate_vertex.co.y, rotate_vertex.co.z)
else:
pg.error = f"{PDT_ERR_TAPER_SEL} {len(bm.select_history)})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_SelectionError
for v in [v for v in bm.verts if v.select]:
if pg.plane == "LO":
v_loc = view_coords(v.co.x, v.co.y, v.co.z)
dis_v = sqrt((view_vector.x - v_loc.x) ** 2 + (view_vector.y - v_loc.y) ** 2)
x_loc = dis_v * tan(ang_v * pi / 180)
view_matrix = view_dir(x_loc, 0)
v.co = v.co - view_matrix
else:
dis_v = sqrt(
(rotate_vertex.co[a3] - v.co[a3]) ** 2 + (rotate_vertex.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()
else:
pg.error = f"{PDT_ERR_EDOB_MODE},{obj.mode})"
context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
raise PDT_ObjectModeError
|