Fix T71100: Node Wrangler creates nodes on linked node trees
[blender-addons.git] / precision_drawing_tools / pdt_tangent.py
blobf9ccda51bc7b716e114fc67334911552299b5a95
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # -----------------------------------------------------------------------
4 # Author: Alan Odom (Clockmender), Rune Morling (ermo) Copyright (c) 2019
5 # -----------------------------------------------------------------------
7 import bpy
8 import bmesh
9 from math import sqrt, floor, asin, sin, cos, pi
10 from mathutils import Vector
11 from bpy.types import Operator
13 from .pdt_functions import (
14 oops,
15 arc_centre,
16 set_mode,
17 view_coords,
18 view_coords_i,
21 from .pdt_msg_strings import (
22 PDT_OBJ_MODE_ERROR,
23 PDT_ERR_NO_ACT_OBJ,
24 PDT_ERR_SEL_3_VERTS,
25 PDT_ERR_SEL_1_VERT,
26 PDT_ERR_BADDISTANCE,
27 PDT_ERR_MATHSERROR,
28 PDT_ERR_SAMERADII,
29 PDT_ERR_VERT_MODE,
32 from . import pdt_exception
34 PDT_ObjectModeError = pdt_exception.ObjectModeError
35 PDT_SelectionError = pdt_exception.SelectionError
38 def get_tangent_intersect_outer(hloc_0, vloc_0, hloc_1, vloc_1, radius_0, radius_1):
39 """Return Location in 2 Dimensions of the Intersect Point for Outer Tangents.
41 Args:
42 hloc_0: Horizontal Coordinate of Centre of First Arc
43 vloc_0: Vertical Coordinate of Centre of First Arc
44 hloc_1: Horizontal Coordinate of Centre of Second Arc
45 vloc_1: Vertical Coordinate of Centre of Second Arc
46 radius_0: Radius of First Arc
47 radius_1: Radius of Second Arc
49 Returns:
50 hloc_p: Horizontal Coordinate of Centre of Intersection
51 vloc_p: Vertical Coordinate of Centre of Intersection.
52 """
54 hloc_p = ((hloc_1 * radius_0) - (hloc_0 * radius_1)) / (radius_0 - radius_1)
55 vloc_p = ((vloc_1 * radius_0) - (vloc_0 * radius_1)) / (radius_0 - radius_1)
57 return hloc_p, vloc_p
60 def get_tangent_intersect_inner(hloc_0, vloc_0, hloc_1, vloc_1, radius_0, radius_1):
61 """Return Location in 2 Dimensions of the Intersect Point for Inner Tangents.
63 Args:
64 hloc_0: Horizontal Coordinate of Centre of First Arc
65 vloc_0: Vertical Coordinate of Centre of First Arc
66 hloc_1: Horizontal Coordinate of Centre of Second Arc
67 vloc_1: Vertical Coordinate of Centre of Second Arc
68 radius_0: Radius of First Arc
69 radius_1: Radius of Second Arc
71 Returns:
72 hloc_p: Horizontal Coordinate of Centre of Intersection
73 vloc_p: Vertical Coordinate of Centre of Intersection.
74 """
76 hloc_p = ((hloc_1 * radius_0) + (hloc_0 * radius_1)) / (radius_0 + radius_1)
77 vloc_p = ((vloc_1 * radius_0) + (vloc_0 * radius_1)) / (radius_0 + radius_1)
79 return hloc_p, vloc_p
82 def get_tangent_points(context, hloc_0, vloc_0, radius_0, hloc_p, vloc_p):
83 """Return Location in 2 Dimensions of the Tangent Points.
85 Args:
86 context: Blender bpy.context instance
87 hloc_0: Horizontal Coordinate of Centre of First Arc
88 vloc_0: Vertical Coordinate of Centre of First Arc
89 radius_0: Radius of First Arc
90 hloc_p: Horizontal Coordinate of Intersection
91 vloc_p: Vertical Coordinate of Intersection
93 Returns:
94 hloc_t1: Horizontal Location of First Tangent Point
95 hloc_t2: Horizontal Location of Second Tangent Point
96 vloc_t1: Vertical Location of First Tangent Point
97 vloc_t2: Vertical Location of Second Tangent Point
98 """
100 # Uses basic Pythagorus' theorem to compute locations
102 numerator = (radius_0 ** 2 * (hloc_p - hloc_0)) + (
103 radius_0
104 * (vloc_p - vloc_0)
105 * sqrt((hloc_p - hloc_0) ** 2 + (vloc_p - vloc_0) ** 2 - radius_0 ** 2)
107 denominator = (hloc_p - hloc_0) ** 2 + (vloc_p - vloc_0) ** 2
108 hloc_t1 = round((numerator / denominator) + hloc_0, 5)
110 numerator = (radius_0 ** 2 * (hloc_p - hloc_0)) - (
111 radius_0
112 * (vloc_p - vloc_0)
113 * sqrt((hloc_p - hloc_0) ** 2 + (vloc_p - vloc_0) ** 2 - radius_0 ** 2)
115 denominator = (hloc_p - hloc_0) ** 2 + (vloc_p - vloc_0) ** 2
116 hloc_t2 = round((numerator / denominator) + hloc_0, 5)
118 # Get Y values
119 numerator = (radius_0 ** 2 * (vloc_p - vloc_0)) - (
120 radius_0
121 * (hloc_p - hloc_0)
122 * sqrt((hloc_p - hloc_0) ** 2 + (vloc_p - vloc_0) ** 2 - radius_0 ** 2)
124 denominator = (hloc_p - hloc_0) ** 2 + (vloc_p - vloc_0) ** 2
125 vloc_t1 = round((numerator / denominator) + vloc_0, 5)
127 numerator = (radius_0 ** 2 * (vloc_p - vloc_0)) + (
128 radius_0
129 * (hloc_p - hloc_0)
130 * sqrt((hloc_p - hloc_0) ** 2 + (vloc_p - vloc_0) ** 2 - radius_0 ** 2)
132 denominator = (hloc_p - hloc_0) ** 2 + (vloc_p - vloc_0) ** 2
133 vloc_t2 = round((numerator / denominator) + vloc_0, 5)
135 return hloc_t1, hloc_t2, vloc_t1, vloc_t2
138 def make_vectors(coords, a1, a2, a3, pg):
139 """Return Vectors of the Tangent Points.
141 Args:
142 coords: A List of Coordinates in 2D space of the tangent points
143 & a third dimension for the vectors
144 a1: Index of horizontal axis
145 a2: Index of vertical axis
146 a3: Index of depth axis
147 pg: PDT Parameters Group - our variables
149 Returns:
150 tangent_vector_o1: Location of First Tangent Point
151 tangent_vector_o2: Location of Second Tangent Point
152 tangent_vector_o3: Location of First Tangent Point
153 tangent_vector_o4: Location of Second Tangent Point
156 tangent_vector_o1 = Vector((0, 0, 0))
157 tangent_vector_o1[a1] = coords[0]
158 tangent_vector_o1[a2] = coords[1]
159 tangent_vector_o1[a3] = coords[8]
160 tangent_vector_o2 = Vector((0, 0, 0))
161 tangent_vector_o2[a1] = coords[2]
162 tangent_vector_o2[a2] = coords[3]
163 tangent_vector_o2[a3] = coords[8]
164 tangent_vector_o3 = Vector((0, 0, 0))
165 tangent_vector_o3[a1] = coords[4]
166 tangent_vector_o3[a2] = coords[5]
167 tangent_vector_o3[a3] = coords[8]
168 tangent_vector_o4 = Vector((0, 0, 0))
169 tangent_vector_o4[a1] = coords[6]
170 tangent_vector_o4[a2] = coords[7]
171 tangent_vector_o4[a3] = coords[8]
173 if pg.plane == "LO":
174 # Reset coordinates from view local (Horiz, Vert, depth) to World XYZ.
176 tangent_vector_o1 = view_coords(
177 tangent_vector_o1[a1], tangent_vector_o1[a2], tangent_vector_o1[a3]
179 tangent_vector_o2 = view_coords(
180 tangent_vector_o2[a1], tangent_vector_o2[a2], tangent_vector_o2[a3]
182 tangent_vector_o3 = view_coords(
183 tangent_vector_o3[a1], tangent_vector_o3[a2], tangent_vector_o3[a3]
185 tangent_vector_o4 = view_coords(
186 tangent_vector_o4[a1], tangent_vector_o4[a2], tangent_vector_o4[a3]
189 return (tangent_vector_o1, tangent_vector_o2, tangent_vector_o3, tangent_vector_o4)
192 def tangent_setup(context, pg, plane, obj_data, centre_0, centre_1, centre_2, radius_0, radius_1):
193 """This section sets up all the variables required for the tangent functions.
195 Args:
196 context: Blender bpy.context instance
197 pg: PDT Parameter Group of variables
198 plane: Working plane
199 obj_data: All the data of the chosen object
200 centre_0: Centre coordinates of the first arc
201 centre_1: Centre coordinates of the second arc
202 centre_2: Coordinates of the point
203 radius_0: Radius of the first Arc
204 radius_1: Radius of the second Arc
206 Returns:
207 Status Set.
210 a1, a2, a3 = set_mode(plane)
211 mode = pg.tangent_mode
212 if plane == "LO":
213 # Translate world coordinates into view local (horiz, vert, depth)
215 centre_0 = view_coords_i(centre_0[a1], centre_0[a2], centre_0[a3])
216 centre_1 = view_coords_i(centre_1[a1], centre_1[a2], centre_1[a3])
217 centre_2 = view_coords_i(centre_2[a1], centre_2[a2], centre_2[a3])
218 if pg.tangent_mode == "point":
219 vector_difference = centre_2 - centre_0
220 distance = sqrt(vector_difference[a1] ** 2 + vector_difference[a2] ** 2)
221 else:
222 vector_difference = centre_1 - centre_0
223 distance = sqrt(vector_difference[a1] ** 2 + vector_difference[a2] ** 2)
225 if (
226 (distance <= radius_0 and mode in {"point"}) or
227 (distance <= (radius_0 + radius_1) and mode in {"inner", "both"}) or
228 (distance <= radius_0 or distance <= radius_1 and mode in {"outer", "both"})
230 # Cannot execute, centres are too close.
232 pg.error = f"{PDT_ERR_BADDISTANCE}"
233 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
234 return {"FINISHED"}
236 """This next section will draw Point based Tangents.
238 These are drawn from a point to an Arc
241 if mode == "point":
242 if (
243 (centre_2[a1] - centre_0[a1]) ** 2 + (centre_2[a2] - centre_0[a2]) ** 2 - radius_0 ** 2
244 ) > 0:
245 hloc_to1, hloc_to2, vloc_to1, vloc_to2 = get_tangent_points(
246 context, centre_0[a1], centre_0[a2], radius_0, centre_2[a1], centre_2[a2]
248 else:
249 pg.error = PDT_ERR_MATHSERROR
250 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
251 return {"FINISHED"}
252 # Point Tangents
254 tangent_vector_o1 = Vector((0, 0, 0))
255 tangent_vector_o1[a1] = hloc_to1
256 tangent_vector_o1[a2] = vloc_to1
257 tangent_vector_o1[a3] = centre_2[a3]
258 tangent_vector_o2 = Vector((0, 0, 0))
259 tangent_vector_o2[a1] = hloc_to2
260 tangent_vector_o2[a2] = vloc_to2
261 tangent_vector_o2[a3] = centre_2[a3]
262 if pg.plane == "LO":
263 # Translate view local coordinates (horiz, vert, depth) into World XYZ
265 centre_2 = view_coords(centre_2[a1], centre_2[a2], centre_2[a3])
266 tangent_vector_o1 = view_coords(
267 tangent_vector_o1[a1], tangent_vector_o1[a2], tangent_vector_o1[a3]
269 tangent_vector_o2 = view_coords(
270 tangent_vector_o2[a1], tangent_vector_o2[a2], tangent_vector_o2[a3]
272 tangent_vectors = (centre_2, tangent_vector_o1, tangent_vector_o2)
273 draw_tangents(tangent_vectors, obj_data)
275 return {"FINISHED"}
277 """This next section will draw Arc based Outer Tangents.
279 These are drawn from an Arc to another Arc
282 if mode in {"outer", "both"}:
283 # Uses basic trigonometry and Pythagorus' theorem to compute locations
285 if radius_0 == radius_1:
286 # No intersection point for outer tangents
288 sin_angle = (centre_1[a2] - centre_0[a2]) / distance
289 cos_angle = (centre_1[a1] - centre_0[a1]) / distance
290 hloc_to1 = centre_0[a1] + (radius_0 * sin_angle)
291 hloc_to2 = centre_0[a1] - (radius_0 * sin_angle)
292 hloc_to3 = centre_1[a1] + (radius_0 * sin_angle)
293 hloc_to4 = centre_1[a1] - (radius_0 * sin_angle)
294 vloc_to1 = centre_0[a2] - (radius_0 * cos_angle)
295 vloc_to2 = centre_0[a2] + (radius_0 * cos_angle)
296 vloc_to3 = centre_1[a2] - (radius_0 * cos_angle)
297 vloc_to4 = centre_1[a2] + (radius_0 * cos_angle)
298 else:
299 hloc_po, vloc_po = get_tangent_intersect_outer(
300 centre_0[a1], centre_0[a2], centre_1[a1], centre_1[a2], radius_0, radius_1
303 if ((hloc_po - centre_0[a1]) ** 2 + (vloc_po - centre_0[a2]) ** 2 - radius_0 ** 2) > 0:
304 hloc_to1, hloc_to2, vloc_to1, vloc_to2 = get_tangent_points(
305 context, centre_0[a1], centre_0[a2], radius_0, hloc_po, vloc_po
307 else:
308 pg.error = PDT_ERR_MATHSERROR
309 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
310 return {"FINISHED"}
311 if ((hloc_po - centre_0[a1]) ** 2 + (vloc_po - centre_0[a2]) ** 2 - radius_1 ** 2) > 0:
312 hloc_to3, hloc_to4, vloc_to3, vloc_to4 = get_tangent_points(
313 context, centre_1[a1], centre_1[a2], radius_1, hloc_po, vloc_po
315 else:
316 pg.error = PDT_ERR_MATHSERROR
317 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
318 return {"FINISHED"}
320 dloc_p = centre_0[a3]
321 coords_in = (
322 hloc_to1,
323 vloc_to1,
324 hloc_to2,
325 vloc_to2,
326 hloc_to3,
327 vloc_to3,
328 hloc_to4,
329 vloc_to4,
330 dloc_p,
332 tangent_vectors = make_vectors(coords_in, a1, a2, a3, pg)
333 draw_tangents(tangent_vectors, obj_data)
335 """This next section will draw Arc based Inner Tangents.
337 These are drawn from an Arc to another Arc
340 if mode in {"inner", "both"}:
341 # Uses basic trigonometry and Pythagorus' theorem to compute locations
343 hloc_pi, vloc_pi = get_tangent_intersect_inner(
344 centre_0[a1], centre_0[a2], centre_1[a1], centre_1[a2], radius_0, radius_1
346 if ((hloc_pi - centre_0[a1]) ** 2 + (vloc_pi - centre_0[a2]) ** 2 - radius_0 ** 2) > 0:
347 hloc_to1, hloc_to2, vloc_to1, vloc_to2 = get_tangent_points(
348 context, centre_0[a1], centre_0[a2], radius_0, hloc_pi, vloc_pi
350 else:
351 pg.error = PDT_ERR_MATHSERROR
352 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
353 return {"FINISHED"}
354 if ((hloc_pi - centre_0[a1]) ** 2 + (vloc_pi - centre_0[a2]) ** 2 - radius_0 ** 2) > 0:
355 hloc_to3, hloc_to4, vloc_to3, vloc_to4 = get_tangent_points(
356 context, centre_1[a1], centre_1[a2], radius_1, hloc_pi, vloc_pi
358 else:
359 pg.error = PDT_ERR_MATHSERROR
360 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
361 return {"FINISHED"}
363 dloc_p = centre_0[a3]
364 coords_in = (
365 hloc_to1,
366 vloc_to1,
367 hloc_to2,
368 vloc_to2,
369 hloc_to3,
370 vloc_to3,
371 hloc_to4,
372 vloc_to4,
373 dloc_p,
375 tangent_vectors = make_vectors(coords_in, a1, a2, a3, pg)
376 draw_tangents(tangent_vectors, obj_data)
378 return {"FINISHED"}
381 def draw_tangents(tangent_vectors, obj_data):
382 """Add Edges Representing the Tangents.
384 Note:
385 The length of the tanget_vectors determines which tangents will be
386 drawn, 3 gives Point Tangents, 4 gives Inner/Outer tangents
388 Args:
389 tangent_vectors: A list of vectors representing the tangents
390 obj_data: A list giving Object, Object Location and Object Bmesh
392 Returns:
393 Nothing.
395 obj = obj_data[0]
396 obj_loc = obj_data[1]
397 bm = obj_data[2]
398 if len(tangent_vectors) == 3:
399 point_vertex_outer = bm.verts.new(tangent_vectors[0] - obj_loc)
400 tangent_vertex_o1 = bm.verts.new(tangent_vectors[1] - obj_loc)
401 tangent_vertex_o2 = bm.verts.new(tangent_vectors[2] - obj_loc)
402 bm.edges.new([tangent_vertex_o1, point_vertex_outer])
403 bm.edges.new([tangent_vertex_o2, point_vertex_outer])
404 else:
405 tangent_vertex_o1 = bm.verts.new(tangent_vectors[0] - obj_loc)
406 tangent_vertex_o2 = bm.verts.new(tangent_vectors[2] - obj_loc)
407 tangent_vertex_o3 = bm.verts.new(tangent_vectors[1] - obj_loc)
408 tangent_vertex_o4 = bm.verts.new(tangent_vectors[3] - obj_loc)
409 bm.edges.new([tangent_vertex_o1, tangent_vertex_o2])
410 bm.edges.new([tangent_vertex_o3, tangent_vertex_o4])
411 bmesh.update_edit_mesh(obj.data)
414 def analyse_arc(context, pg):
415 """Analyses an Arc inferred from Selected Vertices.
417 Note:
418 Will work if more than 3 vertices are selected, taking the
419 first, the nearest to the middle and the last.
421 Args:
422 context: Blender bpy.context instance
423 pg: PDT Parameters Group - our variables
425 Returns:
426 vector_delta: Location of Arc Centre
427 radius: Radius of Arc.
429 obj = context.view_layer.objects.active
430 if obj is None:
431 pg.error = PDT_ERR_NO_ACT_OBJ
432 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
433 raise PDT_ObjectModeError
434 if obj.mode == "EDIT":
435 obj_loc = obj.matrix_world.decompose()[0]
436 bm = bmesh.from_edit_mesh(obj.data)
437 verts = [v for v in bm.verts if v.select]
438 if len(verts) < 3:
439 pg.error = f"{PDT_ERR_SEL_3_VERTS} {len(verts)})"
440 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
441 raise PDT_SelectionError
442 vector_a = verts[0].co
443 # Get the nearest to middle vertex of the arc
445 vector_b = verts[int(floor(len(verts) / 2))].co
446 vector_c = verts[-1].co
447 vector_delta, radius = arc_centre(vector_a, vector_b, vector_c)
449 return vector_delta, radius
452 class PDT_OT_TangentOperate(Operator):
453 """Calculate Tangents from Inputs."""
455 bl_idname = "pdt.tangentoperate"
456 bl_label = "Calculate Tangents"
457 bl_options = {"REGISTER", "UNDO"}
458 bl_description = "Calculate Tangents to Arcs from Points or Other Arcs"
460 @classmethod
461 def poll(cls, context):
462 ob = context.object
463 if ob is None:
464 return False
465 return all([bool(ob), ob.type == "MESH", ob.mode == "EDIT"])
467 def execute(self, context):
468 """Calculate Tangents from Inputs.
470 Note:
471 Uses pg.plane, pg.tangent_point0, pg.tangent_radius0, pg.tangent_point1
472 pg.tangent_radius1, pg.tangent_point2 to place tangents.
474 Analyses distance between arc centres, or arc centre and tangent point
475 to determine which mode is possible (Inner, Outer, or Point). If centres are
476 both contained within 1 inferred circle, Inner tangents are not possible.
478 Arcs of same radius will have no intersection for outer tangents so these
479 are calculated differently.
481 Args:
482 context: Blender bpy.context instance.
484 Returns:
485 Nothing.
488 scene = context.scene
489 pg = scene.pdt_pg
490 plane = pg.plane
491 # Get Object
492 obj = context.view_layer.objects.active
493 if obj is not None:
494 if obj.mode not in {"EDIT"} or obj.type != "MESH":
495 pg.error = PDT_OBJ_MODE_ERROR
496 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
497 return {"FINISHED"}
498 else:
499 pg.error = PDT_ERR_NO_ACT_OBJ
500 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
501 return {"FINISHED"}
502 bm = bmesh.from_edit_mesh(obj.data)
503 obj_loc = obj.matrix_world.decompose()[0]
504 obj_data = (obj, obj_loc, bm)
506 radius_0 = pg.tangent_radius0
507 radius_1 = pg.tangent_radius1
508 centre_0 = pg.tangent_point0
509 centre_1 = pg.tangent_point1
510 centre_2 = pg.tangent_point2
512 tangent_setup(
513 context, pg, plane, obj_data, centre_0, centre_1, centre_2, radius_0, radius_1
516 return {"FINISHED"}
519 class PDT_OT_TangentOperateSel(Operator):
520 """Calculate Tangents from Selection."""
522 bl_idname = "pdt.tangentoperatesel"
523 bl_label = "Calculate Tangents"
524 bl_options = {"REGISTER", "UNDO"}
525 bl_description = "Calculate Tangents to Arcs from 2 Selected Vertices, or 1 & Point in Menu"
527 @classmethod
528 def poll(cls, context):
529 ob = context.object
530 if ob is None:
531 return False
532 return all([bool(ob), ob.type == "MESH", ob.mode == "EDIT"])
534 def execute(self, context):
535 """Calculate Tangents from Selection.
537 Note:
538 Uses pg.plane & 2 or more selected Vertices to place tangents.
539 One vertex must be on each arc.
541 Analyses distance between arc centres, or arc centre and tangent point
542 to determine which mode is possible (Inner, Outer, or Point). If centres are
543 both contained within 1 inferred circle, Inner tangents are not possible.
545 Arcs of same radius will have no intersection for outer tangents so these
546 are calculated differently.
548 Args:
549 context: Blender bpy.context instance.
551 Returns:
552 Nothing.
555 scene = context.scene
556 pg = scene.pdt_pg
557 plane = pg.plane
558 # Get Object
559 obj = context.view_layer.objects.active
560 if obj is not None:
561 if obj.mode not in {"EDIT"} or obj.type != "MESH":
562 pg.error = PDT_OBJ_MODE_ERROR
563 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
564 return {"FINISHED"}
565 else:
566 pg.error = PDT_ERR_NO_ACT_OBJ
567 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
568 return {"FINISHED"}
569 bm = bmesh.from_edit_mesh(obj.data)
570 obj_loc = obj.matrix_world.decompose()[0]
571 obj_data = (obj, obj_loc, bm)
573 # Get All Values from Selected Vertices
574 verts = [v for v in bm.verts if v.select]
575 if len(verts) <= 0:
576 pg.error = f"{PDT_ERR_SEL_1_VERT} 0"
577 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
578 return {"FINISHED"}
579 v1 = verts[0]
580 vn = verts[-1]
581 for v in bm.verts:
582 v.select_set(False)
583 for e in bm.edges:
584 e.select_set(False)
585 v1.select_set(True)
586 bpy.ops.mesh.select_linked()
587 verts1 = [v for v in bm.verts if v.select].copy()
588 if len(verts1) < 3:
589 pg.error = f"{PDT_ERR_VERT_MODE} or Less than 3 vertices in your Arc(s)"
590 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
591 return {"FINISHED"}
592 for v in bm.verts:
593 v.select_set(False)
594 for e in bm.edges:
595 e.select_set(False)
596 vn.select_set(True)
597 bpy.ops.mesh.select_linked()
598 vertsn = [v for v in bm.verts if v.select].copy()
599 for v in bm.verts:
600 v.select_set(False)
601 for e in bm.edges:
602 e.select_set(False)
603 bmesh.update_edit_mesh(obj.data)
604 bm.select_history.clear()
605 # Select the nearest to middle vertex in the arc
607 verts1 = [verts1[0].co, verts1[int(floor(len(verts1) / 2))].co, verts1[-1].co]
608 vertsn = [vertsn[0].co, vertsn[int(floor(len(vertsn) / 2))].co, vertsn[-1].co]
609 centre_0, radius_0 = arc_centre(verts1[0], verts1[1], verts1[2])
610 centre_1, radius_1 = arc_centre(vertsn[0], vertsn[1], vertsn[2])
611 centre_2 = pg.tangent_point2
613 tangent_setup(
614 context, pg, plane, obj_data, centre_0, centre_1, centre_2, radius_0, radius_1
617 return {"FINISHED"}
620 class PDT_OT_TangentSet1(Operator):
621 """Calculates Centres & Radii from 3 Vectors."""
623 bl_idname = "pdt.tangentset1"
624 bl_label = "Calculate Centres & Radii"
625 bl_options = {"REGISTER", "UNDO"}
626 bl_description = "Calculate Centres & Radii from Selected Vertices"
628 @classmethod
629 def poll(cls, context):
630 ob = context.object
631 if ob is None:
632 return False
633 return all([bool(ob), ob.type == "MESH", ob.mode == "EDIT"])
635 def execute(self, context):
636 """Sets Input Tangent Point 1 to analysis of Arc.
638 Args:
639 context: Blender bpy.context instance.
641 Returns:
642 Nothing.
644 scene = context.scene
645 pg = scene.pdt_pg
646 vector_delta, radius = analyse_arc(context, pg)
647 pg.tangent_point0 = vector_delta
648 pg.tangent_radius0 = radius
649 return {"FINISHED"}
652 class PDT_OT_TangentSet2(Operator):
653 """Calculates Centres & Radii from 3 Vectors."""
655 bl_idname = "pdt.tangentset2"
656 bl_label = "Calculate Centres & Radii"
657 bl_options = {"REGISTER", "UNDO"}
658 bl_description = "Calculate Centres & Radii from Selected Vertices"
660 @classmethod
661 def poll(cls, context):
662 obj = context.object
663 if obj is None:
664 return False
665 return all([bool(obj), obj.type == "MESH", obj.mode == "EDIT"])
667 def execute(self, context):
668 """Sets Input Tangent Point 2 to analysis of Arc.
670 Args:
671 context: Blender bpy.context instance.
673 Returns:
674 Nothing.
676 scene = context.scene
677 pg = scene.pdt_pg
678 vector_delta, radius = analyse_arc(context, pg)
679 pg.tangent_point1 = vector_delta
680 pg.tangent_radius1 = radius
681 return {"FINISHED"}
684 class PDT_OT_TangentSet3(Operator):
685 """Set Tangent Origin Point from Cursor."""
687 bl_idname = "pdt.tangentset3"
688 bl_label = "Set Tangent Origin Point from Cursor"
689 bl_options = {"REGISTER", "UNDO"}
690 bl_description = "Set Tangent Origin Point from Cursor"
692 @classmethod
693 def poll(cls, context):
694 obj = context.object
695 if obj is None:
696 return False
697 return all([bool(obj), obj.type == "MESH", obj.mode == "EDIT"])
699 def execute(self, context):
700 """Sets Input Tangent Point 3 to analysis of Arc.
702 Args:
703 context: Blender bpy.context instance.
705 Returns:
706 Nothing.
708 scene = context.scene
709 pg = scene.pdt_pg
710 pg.tangent_point2 = scene.cursor.location
711 return {"FINISHED"}
714 class PDT_OT_TangentSet4(Operator):
715 """Set Tangent Origin Point from Cursor."""
717 bl_idname = "pdt.tangentset4"
718 bl_label = "Set Tangent Origin Point from Vertex"
719 bl_options = {"REGISTER", "UNDO"}
720 bl_description = "Set Tangent Origin Point from Vertex"
722 @classmethod
723 def poll(cls, context):
724 obj = context.object
725 if obj is None:
726 return False
727 return all([bool(obj), obj.type == "MESH", obj.mode == "EDIT"])
729 def execute(self, context):
730 """Sets Input Tangent Point 2 to selected Vertex.
732 Args:
733 context: Blender bpy.context instance.
735 Returns:
736 Nothing.
738 scene = context.scene
739 pg = scene.pdt_pg
740 obj = context.object
741 bm = bmesh.from_edit_mesh(obj.data)
742 verts = [v for v in bm.verts if v.select]
743 if len(verts) != 1:
744 pg.error = f"{PDT_ERR_SEL_1_VERT} {len(verts)})"
745 context.window_manager.popup_menu(oops, title="Error", icon="ERROR")
746 raise PDT_SelectionError
747 pg.tangent_point2 = verts[0].co
748 return {"FINISHED"}
751 class PDT_OT_TangentExpandMenu(Operator):
752 """Expand/Collapse Tangent Menu."""
754 bl_idname = "pdt.tangentexpandmenu"
755 bl_label = "Expand/Collapse Tangent Menu"
756 bl_options = {"REGISTER", "UNDO"}
757 bl_description = "Expand/Collapse Tangent Menu to Show/Hide Input Options"
759 def execute(self, context):
760 """Expand/Collapse Tangent Menu.
762 Note:
763 This is used to add further options to the menu.
765 Args:
766 context: Blender bpy.context instance.
768 Returns:
769 Nothing.
771 scene = context.scene
772 pg = scene.pdt_pg
773 if pg.menu_expand:
774 pg.menu_expand = False
775 else:
776 pg.menu_expand = True
777 return {"FINISHED"}