3 # This is a toolkit for edge manipulation based on several of mesh manipulation
4 # abilities of several CAD/CAE packages, notably CATIA's Geometric Workbench
5 # from which most of these tools have a functional basis based on the paradims
6 # that platform enables. These tools are a collection of scripts that I needed
7 # at some point, and so I will probably add and improve these as I continue to
8 # use and model with them.
10 # It might be good to eventually merge the tinyCAD VTX tools for unification
11 # purposes, and as these are edge-based tools, it would make sense. Or maybe
12 # merge this with tinyCAD instead?
14 # The GUI and Blender add-on structure shamelessly coded in imitation of the
18 # - "Ortho" inspired from CATIA's line creation tool which creates a line of a
19 # user specified length at a user specified angle to a curve at a chosen
20 # point. The user then selects the plane the line is to be created in.
21 # - "Shaft" is inspired from CATIA's tool of the same name. However, instead
22 # of a curve around an axis, this will instead shaft a line, a point, or
23 # a fixed radius about the selected axis.
24 # - "Slice" is from CATIA's ability to split a curve on a plane. When
25 # completed this be a Python equivalent with all the same basic
26 # functionality, though it will sadly be a little clumsier to use due
27 # to Blender's selection limitations.
30 # - Figure out how to do a GUI for "Shaft", especially for controlling radius?
31 # - Buggy parts have been hidden behind bpy.app.debug. Run Blender in debug
32 # to expose those. Example: Shaft with more than two edges selected.
34 # Paul "BrikBot" Marshall
35 # Created: January 28, 2012
36 # Last Modified: August 25, 2012
37 # Homepage (blog): http://post.darkarsenic.com/
38 # //blog.darkarsenic.com/
40 # Coded in IDLE, tested in Blender 2.63.
41 # Search for "@todo" to quickly find sections that need work.
44 # Functional code comes before fast code. Once it works, then worry about
45 # making it faster/more efficient.
47 # ##### BEGIN GPL LICENSE BLOCK #####
49 # The Blender Edgetools is to bring CAD tools to Blender.
50 # Copyright (C) 2012 Paul Marshall
52 # This program is free software: you can redistribute it and/or modify
53 # it under the terms of the GNU General Public License as published by
54 # the Free Software Foundation, either version 3 of the License, or
55 # (at your option) any later version.
57 # This program is distributed in the hope that it will be useful,
58 # but WITHOUT ANY WARRANTY; without even the implied warranty of
59 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 # GNU General Public License for more details.
62 # You should have received a copy of the GNU General Public License
63 # along with this program. If not, see <http://www.gnu.org/licenses/>.
65 # ##### END GPL LICENSE BLOCK #####
72 'author': "Paul Marshall",
75 'location': "View3D > Toolbar and View3D > Specials (W-key)",
77 'description': "CAD style edge manipulation tools",
78 'wiki_url': "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Modeling/EdgeTools",
79 'tracker_url': "https://blenderpython.svn.sourceforge.net/svnroot/blenderpython/scripts_library/scripts/addons_extern/mesh_edgetools.py",
82 import bpy, bmesh, mathutils
83 from math import acos, pi, radians, sqrt, tan
84 from mathutils import Matrix, Vector
85 from mathutils.geometry import (distance_point_to_plane,
90 from bpy.props import (BoolProperty,
96 # Quick an dirty method for getting the sign of a number:
98 return (number > 0) - (number < 0)
103 # Checks to see if two lines are parallel
104 def is_parallel(v1, v2, v3, v4):
105 result = intersect_line_line(v1, v2, v3, v4)
106 return result == None
111 # This is for the special case where the edge is parallel to an axis. In this
112 # the projection onto the XY plane will fail so it will have to be handled
113 # differently. This tells us if and how:
114 def is_axial(v1, v2, error = 0.000002):
116 # Don't need to store, but is easier to read:
117 vec0 = vector[0] > -error and vector[0] < error
118 vec1 = vector[1] > -error and vector[1] < error
119 vec2 = vector[2] > -error and vector[2] < error
120 if (vec0 or vec1) and vec2:
129 # For some reason "Vector = Vector" does not seem to look at the actual
130 # coordinates. This provides a way to do so.
131 def is_same_co(v1, v2):
132 if len(v1) != len(v2):
135 for co1, co2 in zip(v1, v2):
143 # Tests a face to see if it is planar.
144 def is_face_planar(face, error = 0.0005):
146 d = distance_point_to_plane(v.co, face.verts[0].co, face.normal)
148 print("Distance: " + str(d))
149 if d < -error or d > error:
156 # Starts with an edge. Then scans for linked, selected edges and builds a
157 # list with them in "order", starting at one end and moving towards the other.
158 def order_joined_edges(edge, edges = [], direction = 1):
164 print(edge, end = ", ")
165 print(edges, end = ", ")
166 print(direction, end = "; ")
168 # Robustness check: direction cannot be zero
173 for e in edge.verts[0].link_edges:
174 if e.select and edges.count(e) == 0:
177 newList.extend(order_joined_edges(e, edges, direction + 1))
178 newList.extend(edges)
181 newList.extend(edges)
182 newList.extend(order_joined_edges(e, edges, direction - 1))
184 # This will only matter at the first level:
185 direction = direction * -1
187 for e in edge.verts[1].link_edges:
188 if e.select and edges.count(e) == 0:
191 newList.extend(order_joined_edges(e, edges, direction + 2))
192 newList.extend(edges)
195 newList.extend(edges)
196 newList.extend(order_joined_edges(e, edges, direction))
199 print(newList, end = ", ")
205 # --------------- GEOMETRY CALCULATION METHODS --------------
207 # distance_point_line
209 # I don't know why the mathutils.geometry API does not already have this, but
210 # it is trivial to code using the structures already in place. Instead of
211 # returning a float, I also want to know the direction vector defining the
212 # distance. Distance can be found with "Vector.length".
213 def distance_point_line(pt, line_p1, line_p2):
214 int_co = intersect_point_line(pt, line_p1, line_p2)
215 distance_vector = int_co[0] - pt
216 return distance_vector
219 # interpolate_line_line
221 # This is an experiment into a cubic Hermite spline (c-spline) for connecting
222 # two edges with edges that obey the general equation.
223 # This will return a set of point coordinates (Vectors).
225 # A good, easy to read background on the mathematics can be found at:
226 # http://cubic.org/docs/hermite.htm
228 # Right now this is . . . less than functional :P
230 # - C-Spline and Bezier curves do not end on p2_co as they are supposed to.
231 # - B-Spline just fails. Epically.
232 # - Add more methods as I come across them. Who said flexibility was bad?
233 def interpolate_line_line(p1_co, p1_dir, p2_co, p2_dir, segments, tension = 1,
234 typ = 'BEZIER', include_ends = False):
236 fraction = 1 / segments
237 # Form: p1, tangent 1, p2, tangent 2
239 poly = [[2, -3, 0, 1], [1, -2, 1, 0],
240 [-2, 3, 0, 0], [1, -1, 0, 0]]
241 elif typ == 'BEZIER':
242 poly = [[-1, 3, -3, 1], [3, -6, 3, 0],
243 [1, 0, 0, 0], [-3, 3, 0, 0]]
244 p1_dir = p1_dir + p1_co
245 p2_dir = -p2_dir + p2_co
246 elif typ == 'BSPLINE':
247 ## Supposed poly matrix for a cubic b-spline:
248 ## poly = [[-1, 3, -3, 1], [3, -6, 3, 0],
249 ## [-3, 0, 3, 0], [1, 4, 1, 0]]
250 # My own invention to try to get something that somewhat acts right.
251 # This is semi-quadratic rather than fully cubic:
252 poly = [[0, -1, 0, 1], [1, -2, 1, 0],
253 [0, -1, 2, 0], [1, -1, 0, 0]]
256 # Generate each point:
257 for i in range(segments - 1):
258 t = fraction * (i + 1)
261 s = [t ** 3, t ** 2, t, 1]
262 h00 = (poly[0][0] * s[0]) + (poly[0][1] * s[1]) + (poly[0][2] * s[2]) + (poly[0][3] * s[3])
263 h01 = (poly[1][0] * s[0]) + (poly[1][1] * s[1]) + (poly[1][2] * s[2]) + (poly[1][3] * s[3])
264 h10 = (poly[2][0] * s[0]) + (poly[2][1] * s[1]) + (poly[2][2] * s[2]) + (poly[2][3] * s[3])
265 h11 = (poly[3][0] * s[0]) + (poly[3][1] * s[1]) + (poly[3][2] * s[2]) + (poly[3][3] * s[3])
266 pieces.append((h00 * p1_co) + (h01 * p1_dir) + (h10 * p2_co) + (h11 * p2_dir))
278 # intersect_line_face
280 # Calculates the coordinate of intersection of a line with a face. It returns
281 # the coordinate if one exists, otherwise None. It can only deal with tris or
282 # quads for a face. A quad does NOT have to be planar. Thus the following.
284 # Quad math and theory:
285 # A quad may not be planar. Therefore the treated definition of the surface is
286 # that the surface is composed of all lines bridging two other lines defined by
287 # the given four points. The lines do not "cross".
289 # The two lines in 3-space can defined as:
290 # ┌ ┐ ┌ ┐ ┌ ┐ ┌ ┐ ┌ ┐ ┌ ┐
291 # │x1│ │a11│ │b11│ │x2│ │a21│ │b21│
292 # │y1│ = (1-t1)│a12│ + t1│b12│, │y2│ = (1-t2)│a22│ + t2│b22│
293 # │z1│ │a13│ │b13│ │z2│ │a23│ │b23│
294 # └ ┘ └ ┘ └ ┘ └ ┘ └ ┘ └ ┘
295 # Therefore, the surface is the lines defined by every point alone the two
296 # lines with a same "t" value (t1 = t2). This is basically R = V1 + tQ, where
297 # Q = V2 - V1 therefore R = V1 + t(V2 - V1) -> R = (1 - t)V1 + tV2:
299 # │x12│ │(1-t)a11 + t * b11│ │(1-t)a21 + t * b21│
300 # │y12│ = (1 - t12)│(1-t)a12 + t * b12│ + t12│(1-t)a22 + t * b22│
301 # │z12│ │(1-t)a13 + t * b13│ │(1-t)a23 + t * b23│
303 # Now, the equation of our line can be likewise defined:
306 # │y3│ = │a32│ + t3│b32│
309 # Now we just have to find a valid solution for the two equations. This should
310 # be our point of intersection. Therefore, x12 = x3 -> x, y12 = y3 -> y,
311 # z12 = z3 -> z. Thus, to find that point we set the equation defining the
312 # surface as equal to the equation for the line:
314 # │(1-t)a11 + t * b11│ │(1-t)a21 + t * b21│ │a31│ │b31│
315 # (1 - t12)│(1-t)a12 + t * b12│ + t12│(1-t)a22 + t * b22│ = │a32│ + t3│b32│
316 # │(1-t)a13 + t * b13│ │(1-t)a23 + t * b23│ │a33│ │b33│
318 # This leaves us with three equations, three unknowns. Solving the system by
319 # hand is practically impossible, but using Mathematica we are given an insane
320 # series of three equations (not reproduced here for the sake of space: see
321 # http://www.mediafire.com/file/cc6m6ba3sz2b96m/intersect_line_surface.nb and
322 # http://www.mediafire.com/file/0egbr5ahg14talm/intersect_line_surface2.nb for
323 # Mathematica computation).
325 # Additionally, the resulting series of equations may result in a div by zero
326 # exception if the line in question if parallel to one of the axis or if the
327 # quad is planar and parallel to either the XY, XZ, or YZ planes. However, the
328 # system is still solvable but must be dealt with a little differently to avaid
329 # these special cases. Because the resulting equations are a little different,
330 # we have to code them differently. Hence the special cases.
332 # Tri math and theory:
333 # A triangle must be planar (three points define a plane). Therefore we just
334 # have to make sure that the line intersects inside the triangle.
336 # If the point is within the triangle, then the angle between the lines that
337 # connect the point to the each individual point of the triangle will be
338 # equal to 2 * PI. Otherwise, if the point is outside the triangle, then the
339 # sum of the angles will be less.
342 # - Figure out how to deal with n-gons. How the heck is a face with 8 verts
343 # definied mathematically? How do I then find the intersection point of
344 # a line with said vert? How do I know if that point is "inside" all the
345 # verts? I have no clue, and haven't been able to find anything on it so
346 # far. Maybe if someone (actually reads this and) who knows could note?
347 def intersect_line_face(edge, face, is_infinite = False, error = 0.000002):
350 # If we are dealing with a non-planar quad:
351 if len(face.verts) == 4 and not is_face_planar(face):
352 edgeA = face.edges[0]
356 for i in range(len(face.edges)):
357 if face.edges[i].verts[0] not in edgeA.verts and face.edges[i].verts[1] not in edgeA.verts:
358 edgeB = face.edges[i]
361 # I haven't figured out a way to mix this in with the above. Doing so might remove a
362 # few extra instructions from having to be executed saving a few clock cycles:
363 for i in range(len(face.edges)):
364 if face.edges[i] == edgeA or face.edges[i] == edgeB:
366 if (edgeA.verts[0] in face.edges[i].verts and edgeB.verts[1] in face.edges[i].verts) or (edgeA.verts[1] in face.edges[i].verts and edgeB.verts[0] in face.edges[i].verts):
370 # Define calculation coefficient constants:
371 # "xx1" is the x coordinate, "xx2" is the y coordinate, and "xx3" is the z
373 a11, a12, a13 = edgeA.verts[0].co[0], edgeA.verts[0].co[1], edgeA.verts[0].co[2]
374 b11, b12, b13 = edgeA.verts[1].co[0], edgeA.verts[1].co[1], edgeA.verts[1].co[2]
376 a21, a22, a23 = edgeB.verts[1].co[0], edgeB.verts[1].co[1], edgeB.verts[1].co[2]
377 b21, b22, b23 = edgeB.verts[0].co[0], edgeB.verts[0].co[1], edgeB.verts[0].co[2]
379 a21, a22, a23 = edgeB.verts[0].co[0], edgeB.verts[0].co[1], edgeB.verts[0].co[2]
380 b21, b22, b23 = edgeB.verts[1].co[0], edgeB.verts[1].co[1], edgeB.verts[1].co[2]
381 a31, a32, a33 = edge.verts[0].co[0], edge.verts[0].co[1], edge.verts[0].co[2]
382 b31, b32, b33 = edge.verts[1].co[0], edge.verts[1].co[1], edge.verts[1].co[2]
384 # There are a bunch of duplicate "sub-calculations" inside the resulting
385 # equations for t, t12, and t3. Calculate them once and store them to
386 # reduce computational time:
387 m01 = a13 * a22 * a31
388 m02 = a12 * a23 * a31
389 m03 = a13 * a21 * a32
390 m04 = a11 * a23 * a32
391 m05 = a12 * a21 * a33
392 m06 = a11 * a22 * a33
393 m07 = a23 * a32 * b11
394 m08 = a22 * a33 * b11
395 m09 = a23 * a31 * b12
396 m10 = a21 * a33 * b12
397 m11 = a22 * a31 * b13
398 m12 = a21 * a32 * b13
399 m13 = a13 * a32 * b21
400 m14 = a12 * a33 * b21
401 m15 = a13 * a31 * b22
402 m16 = a11 * a33 * b22
403 m17 = a12 * a31 * b23
404 m18 = a11 * a32 * b23
405 m19 = a13 * a22 * b31
406 m20 = a12 * a23 * b31
407 m21 = a13 * a32 * b31
408 m22 = a23 * a32 * b31
409 m23 = a12 * a33 * b31
410 m24 = a22 * a33 * b31
411 m25 = a23 * b12 * b31
412 m26 = a33 * b12 * b31
413 m27 = a22 * b13 * b31
414 m28 = a32 * b13 * b31
415 m29 = a13 * b22 * b31
416 m30 = a33 * b22 * b31
417 m31 = a12 * b23 * b31
418 m32 = a32 * b23 * b31
419 m33 = a13 * a21 * b32
420 m34 = a11 * a23 * b32
421 m35 = a13 * a31 * b32
422 m36 = a23 * a31 * b32
423 m37 = a11 * a33 * b32
424 m38 = a21 * a33 * b32
425 m39 = a23 * b11 * b32
426 m40 = a33 * b11 * b32
427 m41 = a21 * b13 * b32
428 m42 = a31 * b13 * b32
429 m43 = a13 * b21 * b32
430 m44 = a33 * b21 * b32
431 m45 = a11 * b23 * b32
432 m46 = a31 * b23 * b32
433 m47 = a12 * a21 * b33
434 m48 = a11 * a22 * b33
435 m49 = a12 * a31 * b33
436 m50 = a22 * a31 * b33
437 m51 = a11 * a32 * b33
438 m52 = a21 * a32 * b33
439 m53 = a22 * b11 * b33
440 m54 = a32 * b11 * b33
441 m55 = a21 * b12 * b33
442 m56 = a31 * b12 * b33
443 m57 = a12 * b21 * b33
444 m58 = a32 * b21 * b33
445 m59 = a11 * b22 * b33
446 m60 = a31 * b22 * b33
447 m61 = a33 * b12 * b21
448 m62 = a32 * b13 * b21
449 m63 = a33 * b11 * b22
450 m64 = a31 * b13 * b22
451 m65 = a32 * b11 * b23
452 m66 = a31 * b12 * b23
453 m67 = b13 * b22 * b31
454 m68 = b12 * b23 * b31
455 m69 = b13 * b21 * b32
456 m70 = b11 * b23 * b32
457 m71 = b12 * b21 * b33
458 m72 = b11 * b22 * b33
459 n01 = m01 - m02 - m03 + m04 + m05 - m06
460 n02 = -m07 + m08 + m09 - m10 - m11 + m12 + m13 - m14 - m15 + m16 + m17 - m18 - m25 + m27 + m29 - m31 + m39 - m41 - m43 + m45 - m53 + m55 + m57 - m59
461 n03 = -m19 + m20 + m33 - m34 - m47 + m48
462 n04 = m21 - m22 - m23 + m24 - m35 + m36 + m37 - m38 + m49 - m50 - m51 + m52
463 n05 = m26 - m28 - m30 + m32 - m40 + m42 + m44 - m46 + m54 - m56 - m58 + m60
464 n06 = m61 - m62 - m63 + m64 + m65 - m66 - m67 + m68 + m69 - m70 - m71 + m72
465 n07 = 2 * n01 + n02 + 2 * n03 + n04 + n05
466 n08 = n01 + n02 + n03 + n06
468 # Calculate t, t12, and t3:
469 t = (n07 - sqrt(pow(-n07, 2) - 4 * (n01 + n03 + n04) * n08)) / (2 * n08)
471 # t12 can be greatly simplified by defining it with t in it:
472 # If block used to help prevent any div by zero error.
476 # The line is parallel to the z-axis:
478 t12 = ((a11 - a31) + (b11 - a11) * t) / ((a21 - a11) + (a11 - a21 - b11 + b21) * t)
479 # The line is parallel to the y-axis:
481 t12 = ((a11 - a31) + (b11 - a11) * t) / ((a21 - a11) + (a11 - a21 - b11 + b21) * t)
482 # The line is along the y/z-axis but is not parallel to either:
484 t12 = -(-(a33 - b33) * (-a32 + a12 * (1 - t) + b12 * t) + (a32 - b32) * (-a33 + a13 * (1 - t) + b13 * t)) / (-(a33 - b33) * ((a22 - a12) * (1 - t) + (b22 - b12) * t) + (a32 - b32) * ((a23 - a13) * (1 - t) + (b23 - b13) * t))
486 # The line is parallel to the x-axis:
488 t12 = ((a12 - a32) + (b12 - a12) * t) / ((a22 - a12) + (a12 - a22 - b12 + b22) * t)
489 # The line is along the x/z-axis but is not parallel to either:
491 t12 = -(-(a33 - b33) * (-a31 + a11 * (1 - t) + b11 * t) + (a31 - b31) * (-a33 + a13 * (1 - t) + b13 * t)) / (-(a33 - b33) * ((a21 - a11) * (1 - t) + (b21 - b11) * t) + (a31 - b31) * ((a23 - a13) * (1 - t) + (b23 - b13) * t))
492 # The line is along the x/y-axis but is not parallel to either:
494 t12 = -(-(a32 - b32) * (-a31 + a11 * (1 - t) + b11 * t) + (a31 - b31) * (-a32 + a12 * (1 - t) + b12 * t)) / (-(a32 - b32) * ((a21 - a11) * (1 - t) + (b21 - b11) * t) + (a31 - b31) * ((a22 - a21) * (1 - t) + (b22 - b12) * t))
496 # Likewise, t3 is greatly simplified by defining it in terms of t and t12:
497 # If block used to prevent a div by zero error.
500 t3 = (-a11 + a31 + (a11 - b11) * t + (a11 - a21) * t12 + (a21 - a11 + b11 - b21) * t * t12) / (a31 - b31)
502 t3 = (-a12 + a32 + (a12 - b12) * t + (a12 - a22) * t12 + (a22 - a12 + b12 - b22) * t * t12) / (a32 - b32)
504 t3 = (-a13 + a33 + (a13 - b13) * t + (a13 - a23) * t12 + (a23 - a13 + b13 - b23) * t * t12) / (a33 - b33)
506 print("The second edge is a zero-length edge")
509 # Calculate the point of intersection:
510 x = (1 - t3) * a31 + t3 * b31
511 y = (1 - t3) * a32 + t3 * b32
512 z = (1 - t3) * a33 + t3 * b33
513 int_co = Vector((x, y, z))
518 # If the line does not intersect the quad, we return "None":
519 if (t < -1 or t > 1 or t12 < -1 or t12 > 1) and not is_infinite:
522 elif len(face.verts) == 3:
523 p1, p2, p3 = face.verts[0].co, face.verts[1].co, face.verts[2].co
524 int_co = intersect_line_plane(edge.verts[0].co, edge.verts[1].co, p1, face.normal)
530 aAB = acos(pA.dot(pB))
531 aBC = acos(pB.dot(pC))
532 aCA = acos(pC.dot(pA))
533 sumA = aAB + aBC + aCA
535 # If the point is outside the triangle:
536 if (sumA > (pi + error) and sumA < (pi - error)) and not is_infinite:
539 # This is the default case where we either have a planar quad or an n-gon.
541 int_co = intersect_line_plane(edge.verts[0].co, edge.verts[1].co,
542 face.verts[0].co, face.normal)
547 # project_point_plane
549 # Projects a point onto a plane. Returns a tuple of the projection vector
550 # and the projected coordinate.
551 def project_point_plane(pt, plane_co, plane_no):
552 proj_co = intersect_line_plane(pt, pt + plane_no, plane_co, plane_no)
553 proj_ve = proj_co - pt
554 return (proj_ve, proj_co)
557 # ------------ FILLET/CHAMPHER HELPER METHODS -------------
561 # The following is used to return edges that might be possible edges for
562 # propagation. If an edge is connected to the end vert, but is also a part
563 # of the on of the faces that the current edge composes, then it is a
564 # "corner edge" and is not valid as a propagation edge. If the edge is
565 # part of two faces that a in the same plane, then we cannot fillet/chamfer
566 # it because there is no angle between them.
567 def get_next_edge(edge, vert):
568 invalidEdges = [e for f in edge.link_faces for e in f.edges if e != edge]
569 invalidEdges.append(edge)
572 newEdge = [e for e in vert.link_edges if e not in invalidEdges and not is_planar_edge(e)]
573 if len(newEdge) == 0:
575 elif len(newEdge) == 1:
581 def is_planar_edge(edge, error = 0.000002):
582 angle = edge.calc_face_angle()
583 return (angle < error and angle > -error) or (angle < (180 + error) and angle > (180 - error))
588 # Calculates the base geometry data for the fillet. This assumes that the faces
592 # - Redesign so that the faces do not have to be planar
594 # There seems to be issues some of the vector math right now. Will need to be
596 def fillet_axis(edge, radius):
597 vectors = [None, None, None, None]
599 origin = Vector((0, 0, 0))
600 axis = edge.verts[1].co - edge.verts[0].co
602 # Get the "adjacency" base vectors for face 0:
603 for e in edge.link_faces[0].edges:
606 if e.verts[0] == edge.verts[0]:
607 vectors[0] = e.verts[1].co - e.verts[0].co
608 elif e.verts[1] == edge.verts[0]:
609 vectors[0] = e.verts[0].co - e.verts[1].co
610 elif e.verts[0] == edge.verts[1]:
611 vectors[1] = e.verts[1].co - e.verts[0].co
612 elif e.verts[1] == edge.verts[1]:
613 vectors[1] = e.verts[0].co - e.verts[1].co
615 # Get the "adjacency" base vectors for face 1:
616 for e in edge.link_faces[1].edges:
619 if e.verts[0] == edge.verts[0]:
620 vectors[2] = e.verts[1].co - e.verts[0].co
621 elif e.verts[1] == edge.verts[0]:
622 vectors[2] = e.verts[0].co - e.verts[1].co
623 elif e.verts[0] == edge.verts[1]:
624 vectors[3] = e.verts[1].co - e.verts[0].co
625 elif e.verts[1] == edge.verts[1]:
626 vectors[3] = e.verts[0].co - e.verts[1].co
628 # Get the normal for face 0 and face 1:
629 norm1 = edge.link_faces[0].normal
630 norm2 = edge.link_faces[1].normal
632 # We need to find the angle between the two faces, then bisect it:
633 theda = (pi - edge.calc_face_angle()) / 2
635 # We are dealing with a triangle here, and we will need the length
636 # of its adjacent side. The opposite is the radius:
637 adj_len = radius / tan(theda)
639 # Vectors can be thought of as being at the origin, and we need to make sure
640 # that the base vectors are planar with the "normal" definied by the edge to
641 # be filleted. Then we set the length of the vector and shift it into a
643 for i in range(len(vectors)):
644 vectors[i] = project_point_plane(vectors[i], origin, axis)[1]
645 vectors[i].length = adj_len
646 vectors[i] = vectors[i] + edge.verts[i % 2].co
648 # Compute fillet axis end points:
649 v1 = intersect_line_line(vectors[0], vectors[0] + norm1, vectors[2], vectors[2] + norm2)[0]
650 v2 = intersect_line_line(vectors[1], vectors[1] + norm1, vectors[3], vectors[3] + norm2)[0]
654 def fillet_point(t, face1, face2):
658 # ------------------- EDGE TOOL METHODS -------------------
660 # Extends an "edge" in two directions:
661 # - Requires two vertices to be selected. They do not have to form an edge.
662 # - Extends "length" in both directions
663 class Extend(bpy.types.Operator):
664 bl_idname = "mesh.edgetools_extend"
666 bl_description = "Extend the selected edges of vertice pair."
667 bl_options = {'REGISTER', 'UNDO'}
669 di1 = BoolProperty(name = "Forwards",
670 description = "Extend the edge forwards",
672 di2 = BoolProperty(name = "Backwards",
673 description = "Extend the edge backwards",
675 length = FloatProperty(name = "Length",
676 description = "Length to extend the edge",
677 min = 0.0, max = 1024.0,
680 def draw(self, context):
682 layout.prop(self, "di1")
683 layout.prop(self, "di2")
684 layout.prop(self, "length")
688 def poll(cls, context):
689 ob = context.active_object
690 return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
693 def invoke(self, context, event):
694 return self.execute(context)
697 def execute(self, context):
698 bpy.ops.object.editmode_toggle()
700 bm.from_mesh(bpy.context.active_object.data)
706 edges = [e for e in bEdges if e.select]
707 verts = [v for v in bVerts if v.select]
711 vector = e.verts[0].co - e.verts[1].co
712 vector.length = self.length
716 if (vector[0] + vector[1] + vector[2]) < 0:
717 v.co = e.verts[1].co - vector
718 newE = bEdges.new((e.verts[1], v))
720 v.co = e.verts[0].co + vector
721 newE = bEdges.new((e.verts[0], v))
724 if (vector[0] + vector[1] + vector[2]) < 0:
725 v.co = e.verts[0].co + vector
726 newE = bEdges.new((e.verts[0], v))
728 v.co = e.verts[1].co - vector
729 newE = bEdges.new((e.verts[1], v))
731 vector = verts[0].co - verts[1].co
732 vector.length = self.length
736 if (vector[0] + vector[1] + vector[2]) < 0:
737 v.co = verts[1].co - vector
738 e = bEdges.new((verts[1], v))
740 v.co = verts[0].co + vector
741 e = bEdges.new((verts[0], v))
744 if (vector[0] + vector[1] + vector[2]) < 0:
745 v.co = verts[0].co + vector
746 e = bEdges.new((verts[0], v))
748 v.co = verts[1].co - vector
749 e = bEdges.new((verts[1], v))
751 bm.to_mesh(bpy.context.active_object.data)
752 bpy.ops.object.editmode_toggle()
756 # Creates a series of edges between two edges using spline interpolation.
757 # This basically just exposes existing functionality in addition to some
758 # other common methods: Hermite (c-spline), Bezier, and b-spline. These
759 # alternates I coded myself after some extensive research into spline
762 # @todo Figure out what's wrong with the Blender bezier interpolation.
763 class Spline(bpy.types.Operator):
764 bl_idname = "mesh.edgetools_spline"
766 bl_description = "Create a spline interplopation between two edges"
767 bl_options = {'REGISTER', 'UNDO'}
769 alg = EnumProperty(name = "Spline Algorithm",
770 items = [('Blender', 'Blender', 'Interpolation provided through \"mathutils.geometry\"'),
771 ('Hermite', 'C-Spline', 'C-spline interpolation'),
772 ('Bezier', 'Bézier', 'Bézier interpolation'),
773 ('B-Spline', 'B-Spline', 'B-Spline interpolation')],
775 segments = IntProperty(name = "Segments",
776 description = "Number of segments to use in the interpolation",
780 flip1 = BoolProperty(name = "Flip Edge",
781 description = "Flip the direction of the spline on edge 1",
783 flip2 = BoolProperty(name = "Flip Edge",
784 description = "Flip the direction of the spline on edge 2",
786 ten1 = FloatProperty(name = "Tension",
787 description = "Tension on edge 1",
788 min = -4096.0, max = 4096.0,
789 soft_min = -8.0, soft_max = 8.0,
791 ten2 = FloatProperty(name = "Tension",
792 description = "Tension on edge 2",
793 min = -4096.0, max = 4096.0,
794 soft_min = -8.0, soft_max = 8.0,
797 def draw(self, context):
800 layout.prop(self, "alg")
801 layout.prop(self, "segments")
802 layout.label("Edge 1:")
803 layout.prop(self, "ten1")
804 layout.prop(self, "flip1")
805 layout.label("Edge 2:")
806 layout.prop(self, "ten2")
807 layout.prop(self, "flip2")
811 def poll(cls, context):
812 ob = context.active_object
813 return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
816 def invoke(self, context, event):
817 return self.execute(context)
820 def execute(self, context):
821 bpy.ops.object.editmode_toggle()
823 bm.from_mesh(bpy.context.active_object.data)
830 edges = [e for e in bEdges if e.select]
831 verts = [edges[v // 2].verts[v % 2] for v in range(4)]
836 p1_dir = verts[1].co - verts[0].co
840 p1_dir = verts[0].co - verts[1].co
843 p1_dir.length = -self.ten1
845 p1_dir.length = self.ten1
850 p2_dir = verts[2].co - verts[3].co
854 p2_dir = verts[3].co - verts[2].co
857 p2_dir.length = -self.ten2
859 p2_dir.length = self.ten2
861 # Get the interploted coordinates:
862 if self.alg == 'Blender':
863 pieces = interpolate_bezier(p1_co, p1_dir, p2_dir, p2_co, self.segments)
864 elif self.alg == 'Hermite':
865 pieces = interpolate_line_line(p1_co, p1_dir, p2_co, p2_dir, self.segments, 1, 'HERMITE')
866 elif self.alg == 'Bezier':
867 pieces = interpolate_line_line(p1_co, p1_dir, p2_co, p2_dir, self.segments, 1, 'BEZIER')
868 elif self.alg == 'B-Spline':
869 pieces = interpolate_line_line(p1_co, p1_dir, p2_co, p2_dir, self.segments, 1, 'BSPLINE')
873 # Add vertices and set the points:
874 for i in range(seg - 1):
881 e = bEdges.new((verts[i], verts[i + 1]))
883 bm.to_mesh(bpy.context.active_object.data)
884 bpy.ops.object.editmode_toggle()
888 # Creates edges normal to planes defined between each of two edges and the
889 # normal or the plane defined by those two edges.
890 # - Select two edges. The must form a plane.
891 # - On running the script, eight edges will be created. Delete the
892 # extras that you don't need.
893 # - The length of those edges is defined by the variable "length"
895 # @todo Change method from a cross product to a rotation matrix to make the
897 # --- todo completed Feb 4th, but still needs work ---
898 # @todo Figure out a way to make +/- predictable
899 # - Maybe use angel between edges and vector direction definition?
900 # --- TODO COMPLETED ON 2/9/2012 ---
901 class Ortho(bpy.types.Operator):
902 bl_idname = "mesh.edgetools_ortho"
903 bl_label = "Angle Off Edge"
905 bl_options = {'REGISTER', 'UNDO'}
907 vert1 = BoolProperty(name = "Vertice 1",
908 description = "Enable edge creation for vertice 1.",
910 vert2 = BoolProperty(name = "Vertice 2",
911 description = "Enable edge creation for vertice 2.",
913 vert3 = BoolProperty(name = "Vertice 3",
914 description = "Enable edge creation for vertice 3.",
916 vert4 = BoolProperty(name = "Vertice 4",
917 description = "Enable edge creation for vertice 4.",
919 pos = BoolProperty(name = "+",
920 description = "Enable positive direction edges.",
922 neg = BoolProperty(name = "-",
923 description = "Enable negitive direction edges.",
925 angle = FloatProperty(name = "Angle",
926 description = "Angle off of the originating edge",
927 min = 0.0, max = 180.0,
929 length = FloatProperty(name = "Length",
930 description = "Length of created edges.",
931 min = 0.0, max = 1024.0,
934 # For when only one edge is selected (Possible feature to be testd):
935 plane = EnumProperty(name = "Plane",
936 items = [("XY", "X-Y Plane", "Use the X-Y plane as the plane of creation"),
937 ("XZ", "X-Z Plane", "Use the X-Z plane as the plane of creation"),
938 ("YZ", "Y-Z Plane", "Use the Y-Z plane as the plane of creation")],
941 def draw(self, context):
944 layout.prop(self, "vert1")
945 layout.prop(self, "vert2")
946 layout.prop(self, "vert3")
947 layout.prop(self, "vert4")
948 row = layout.row(align = False)
949 row.alignment = 'EXPAND'
950 row.prop(self, "pos")
951 row.prop(self, "neg")
952 layout.prop(self, "angle")
953 layout.prop(self, "length")
956 def poll(cls, context):
957 ob = context.active_object
958 return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
961 def invoke(self, context, event):
962 return self.execute(context)
965 def execute(self, context):
966 bpy.ops.object.editmode_toggle()
968 bm.from_mesh(bpy.context.active_object.data)
973 edges = [e for e in bEdges if e.select]
976 # Until I can figure out a better way of handeling it:
978 bpy.ops.object.editmode_toggle()
979 self.report({'ERROR_INVALID_INPUT'},
980 "You must select two edges.")
983 verts = [edges[0].verts[0],
988 cos = intersect_line_line(verts[0].co, verts[1].co, verts[2].co, verts[3].co)
990 # If the two edges are parallel:
992 self.report({'WARNING'},
993 "Selected lines are parallel: results may be unpredictable.")
994 vectors.append(verts[0].co - verts[1].co)
995 vectors.append(verts[0].co - verts[2].co)
996 vectors.append(vectors[0].cross(vectors[1]))
997 vectors.append(vectors[2].cross(vectors[0]))
998 vectors.append(-vectors[3])
1000 # Warn the user if they have not chosen two planar edges:
1001 if not is_same_co(cos[0], cos[1]):
1002 self.report({'WARNING'},
1003 "Selected lines are not planar: results may be unpredictable.")
1005 # This makes the +/- behavior predictable:
1006 if (verts[0].co - cos[0]).length < (verts[1].co - cos[0]).length:
1007 verts[0], verts[1] = verts[1], verts[0]
1008 if (verts[2].co - cos[0]).length < (verts[3].co - cos[0]).length:
1009 verts[2], verts[3] = verts[3], verts[2]
1011 vectors.append(verts[0].co - verts[1].co)
1012 vectors.append(verts[2].co - verts[3].co)
1014 # Normal of the plane formed by vector1 and vector2:
1015 vectors.append(vectors[0].cross(vectors[1]))
1017 # Possible directions:
1018 vectors.append(vectors[2].cross(vectors[0]))
1019 vectors.append(vectors[1].cross(vectors[2]))
1022 vectors[3].length = self.length
1023 vectors[4].length = self.length
1025 # Perform any additional rotations:
1026 matrix = Matrix.Rotation(radians(90 + self.angle), 3, vectors[2])
1027 vectors.append(matrix * -vectors[3]) # vectors[5]
1028 matrix = Matrix.Rotation(radians(90 - self.angle), 3, vectors[2])
1029 vectors.append(matrix * vectors[4]) # vectors[6]
1030 vectors.append(matrix * vectors[3]) # vectors[7]
1031 matrix = Matrix.Rotation(radians(90 + self.angle), 3, vectors[2])
1032 vectors.append(matrix * -vectors[4]) # vectors[8]
1034 # Perform extrusions and displacements:
1035 # There will be a total of 8 extrusions. One for each vert of each edge.
1036 # It looks like an extrusion will add the new vert to the end of the verts
1037 # list and leave the rest in the same location.
1038 # ----------- EDIT -----------
1039 # It looks like I might be able to do this within "bpy.data" with the ".add"
1041 # ------- BMESH UPDATE -------
1042 # BMesh uses ".new()"
1044 for v in range(len(verts)):
1046 if (v == 0 and self.vert1) or (v == 1 and self.vert2) or (v == 2 and self.vert3) or (v == 3 and self.vert4):
1049 new.co = vert.co - vectors[5 + (v // 2) + ((v % 2) * 2)]
1050 bEdges.new((vert, new))
1053 new.co = vert.co + vectors[5 + (v // 2) + ((v % 2) * 2)]
1054 bEdges.new((vert, new))
1056 bm.to_mesh(bpy.context.active_object.data)
1057 bpy.ops.object.editmode_toggle()
1062 # Select an edge and a point or an edge and specify the radius (default is 1 BU)
1063 # You can select two edges but it might be unpredicatble which edge it revolves
1064 # around so you might have to play with the switch.
1065 class Shaft(bpy.types.Operator):
1066 bl_idname = "mesh.edgetools_shaft"
1068 bl_description = "Create a shaft mesh around an axis"
1069 bl_options = {'REGISTER', 'UNDO'}
1071 # Selection defaults:
1074 # For tracking if the user has changed selection:
1075 last_edge = IntProperty(name = "Last Edge",
1076 description = "Tracks if user has changed selected edge",
1081 edge = IntProperty(name = "Edge",
1082 description = "Edge to shaft around.",
1085 flip = BoolProperty(name = "Flip Second Edge",
1086 description = "Flip the percieved direction of the second edge.",
1088 radius = FloatProperty(name = "Radius",
1089 description = "Shaft Radius",
1090 min = 0.0, max = 1024.0,
1092 start = FloatProperty(name = "Starting Angle",
1093 description = "Angle to start the shaft at.",
1094 min = -360.0, max = 360.0,
1096 finish = FloatProperty(name = "Ending Angle",
1097 description = "Angle to end the shaft at.",
1098 min = -360.0, max = 360.0,
1100 segments = IntProperty(name = "Shaft Segments",
1101 description = "Number of sgements to use in the shaft.",
1102 min = 1, max = 4096,
1107 def draw(self, context):
1108 layout = self.layout
1110 if self.shaftType == 0:
1111 layout.prop(self, "edge")
1112 layout.prop(self, "flip")
1113 elif self.shaftType == 3:
1114 layout.prop(self, "radius")
1115 layout.prop(self, "segments")
1116 layout.prop(self, "start")
1117 layout.prop(self, "finish")
1121 def poll(cls, context):
1122 ob = context.active_object
1123 return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
1126 def invoke(self, context, event):
1127 # Make sure these get reset each time we run:
1131 return self.execute(context)
1134 def execute(self, context):
1135 bpy.ops.object.editmode_toggle()
1137 bm.from_mesh(bpy.context.active_object.data)
1148 # Pre-caclulated values:
1150 rotRange = [radians(self.start), radians(self.finish)]
1151 rads = radians((self.finish - self.start) / self.segments)
1153 numV = self.segments + 1
1154 numE = self.segments
1156 edges = [e for e in bEdges if e.select]
1158 # Robustness check: there should at least be one edge selected
1160 bpy.ops.object.editmode_toggle()
1161 self.report({'ERROR_INVALID_INPUT'},
1162 "At least one edge must be selected.")
1163 return {'CANCELLED'}
1165 # If two edges are selected:
1173 # By default, we want to shaft around the last selected edge (it
1174 # will be the active edge). We know we are using the default if
1175 # the user has not changed which edge is being shafted around (as
1176 # is tracked by self.last_edge). When they are not the same, then
1177 # the user has changed selection.
1179 # We then need to make sure that the active object really is an edge
1180 # (robustness check).
1182 # Finally, if the active edge is not the inital one, we flip them
1183 # and have the GUI reflect that.
1184 if self.last_edge == self.edge:
1185 if isinstance(bm.select_history.active, bmesh.types.BMEdge):
1186 if bm.select_history.active != edges[edge[0]]:
1187 self.last_edge, self.edge = edge[1], edge[1]
1188 edge = [edge[1], edge[0]]
1190 bpy.ops.object.editmode_toggle()
1191 self.report({'ERROR_INVALID_INPUT'},
1192 "Active geometry is not an edge.")
1193 return {'CANCELLED'}
1194 elif self.edge == 1:
1197 verts.append(edges[edge[0]].verts[0])
1198 verts.append(edges[edge[0]].verts[1])
1203 verts.append(edges[edge[1]].verts[vert[0]])
1204 verts.append(edges[edge[1]].verts[vert[1]])
1207 # If there is more than one edge selected:
1208 # There are some issues with it ATM, so don't expose is it to normal users:
1209 elif len(edges) > 2 and bpy.app.debug:
1210 if isinstance(bm.select_history.active, bmesh.types.BMEdge):
1211 active = bm.select_history.active
1212 edges.remove(active)
1213 # Get all the verts:
1214 edges = order_joined_edges(edges[0])
1217 if verts.count(e.verts[0]) == 0:
1218 verts.append(e.verts[0])
1219 if verts.count(e.verts[1]) == 0:
1220 verts.append(e.verts[1])
1222 bpy.ops.object.editmode_toggle()
1223 self.report({'ERROR_INVALID_INPUT'},
1224 "Active geometry is not an edge.")
1225 return {'CANCELLED'}
1228 verts.append(edges[0].verts[0])
1229 verts.append(edges[0].verts[1])
1232 if v.select and verts.count(v) == 0:
1240 # The vector denoting the axis of rotation:
1241 if self.shaftType == 1:
1242 axis = active.verts[1].co - active.verts[0].co
1244 axis = verts[1].co - verts[0].co
1246 # We will need a series of rotation matrices. We could use one which would be
1247 # faster but also might cause propagation of error.
1249 ## for i in range(numV):
1250 ## matrices.append(Matrix.Rotation((rads * i) + rotRange[0], 3, axis))
1251 matrices = [Matrix.Rotation((rads * i) + rotRange[0], 3, axis) for i in range(numV)]
1253 # New vertice coordinates:
1256 # If two edges were selected:
1257 # - If the lines are not parallel, then it will create a cone-like shaft
1258 if self.shaftType == 0:
1259 for i in range(len(verts) - 2):
1260 init_vec = distance_point_line(verts[i + 2].co, verts[0].co, verts[1].co)
1261 co = init_vec + verts[i + 2].co
1262 # These will be rotated about the orgin so will need to be shifted:
1263 for j in range(numV):
1264 verts_out.append(co - (matrices[j] * init_vec))
1265 elif self.shaftType == 1:
1267 init_vec = distance_point_line(i.co, active.verts[0].co, active.verts[1].co)
1268 co = init_vec + i.co
1269 # These will be rotated about the orgin so will need to be shifted:
1270 for j in range(numV):
1271 verts_out.append(co - (matrices[j] * init_vec))
1272 # Else if a line and a point was selected:
1273 elif self.shaftType == 2:
1274 init_vec = distance_point_line(verts[2].co, verts[0].co, verts[1].co)
1275 # These will be rotated about the orgin so will need to be shifted:
1276 verts_out = [(verts[i].co - (matrices[j] * init_vec)) for i in range(2) for j in range(numV)]
1277 # Else the above are not possible, so we will just use the edge:
1278 # - The vector defined by the edge is the normal of the plane for the shaft
1279 # - The shaft will have radius "radius".
1281 if is_axial(verts[0].co, verts[1].co) == None:
1282 proj = (verts[1].co - verts[0].co)
1284 norm = proj.cross(verts[1].co - verts[0].co)
1285 vec = norm.cross(verts[1].co - verts[0].co)
1286 vec.length = self.radius
1287 elif is_axial(verts[0].co, verts[1].co) == 'Z':
1288 vec = verts[0].co + Vector((0, 0, self.radius))
1290 vec = verts[0].co + Vector((0, self.radius, 0))
1291 init_vec = distance_point_line(vec, verts[0].co, verts[1].co)
1292 # These will be rotated about the orgin so will need to be shifted:
1293 verts_out = [(verts[i].co - (matrices[j] * init_vec)) for i in range(2) for j in range(numV)]
1295 # We should have the coordinates for a bunch of new verts. Now add the verts
1296 # and build the edges and then the faces.
1300 if self.shaftType == 1:
1302 for i in range(numV * len(verts)):
1304 new.co = verts_out[i]
1306 newVerts.append(new)
1309 for i in range(numE):
1310 for j in range(len(verts)):
1311 e = bEdges.new((newVerts[i + (numV * j)], newVerts[i + (numV * j) + 1]))
1313 for i in range(numV):
1314 for j in range(len(verts) - 1):
1315 e = bEdges.new((newVerts[i + (numV * j)], newVerts[i + (numV * (j + 1))]))
1319 # There is a problem with this right now:
1320 for i in range(len(edges)):
1321 for j in range(numE):
1322 f = bFaces.new((newVerts[i], newVerts[i + 1],
1323 newVerts[i + (numV * j) + 1], newVerts[i + (numV * j)]))
1327 for i in range(numV * 2):
1329 new.co = verts_out[i]
1331 newVerts.append(new)
1334 for i in range(numE):
1335 e = bEdges.new((newVerts[i], newVerts[i + 1]))
1337 e = bEdges.new((newVerts[i + numV], newVerts[i + numV + 1]))
1339 for i in range(numV):
1340 e = bEdges.new((newVerts[i], newVerts[i + numV]))
1344 for i in range(numE):
1345 f = bFaces.new((newVerts[i], newVerts[i + 1],
1346 newVerts[i + numV + 1], newVerts[i + numV]))
1349 bm.to_mesh(bpy.context.active_object.data)
1350 bpy.ops.object.editmode_toggle()
1354 # "Slices" edges crossing a plane defined by a face.
1355 class Slice(bpy.types.Operator):
1356 bl_idname = "mesh.edgetools_slice"
1358 bl_description = "Cuts edges at the plane defined by a selected face."
1359 bl_options = {'REGISTER', 'UNDO'}
1361 make_copy = BoolProperty(name = "Make Copy",
1362 description = "Make new vertices at intersection points instead of spliting the edge",
1364 rip = BoolProperty(name = "Rip",
1365 description = "Split into two edges that DO NOT share an intersection vertice.",
1367 pos = BoolProperty(name = "Positive",
1368 description = "Remove the portion on the side of the face normal",
1370 neg = BoolProperty(name = "Negative",
1371 description = "Remove the portion on the side opposite of the face normal",
1374 def draw(self, context):
1375 layout = self.layout
1377 layout.prop(self, "make_copy")
1378 if not self.make_copy:
1379 layout.prop(self, "rip")
1380 layout.label("Remove Side:")
1381 layout.prop(self, "pos")
1382 layout.prop(self, "neg")
1386 def poll(cls, context):
1387 ob = context.active_object
1388 return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
1391 def invoke(self, context, event):
1392 return self.execute(context)
1395 def execute(self, context):
1396 bpy.ops.object.editmode_toggle()
1398 bm.from_mesh(context.active_object.data)
1401 # For easy access to verts, edges, and faces:
1409 # Find the selected face. This will provide the plane to project onto:
1410 if isinstance(bm.select_history.active, bmesh.types.BMFace):
1411 face = bm.select_history.active
1412 normal = bm.select_history.active.normal
1413 bm.select_history.active.select = False
1423 bpy.ops.object.editmode_toggle()
1424 self.report({'ERROR_INVALID_INPUT'},
1425 "You must select a face as the cutting plane.")
1426 return {'CANCELLED'}
1427 elif len(face.verts) > 4 and not is_face_planar(face):
1428 self.report({'WARNING'},
1429 "Selected face is an n-gon. Results may be unpredictable.")
1434 if e.select and (v1 not in face.verts and v2 not in face.verts):
1435 if len(face.verts) < 5: # Not an n-gon
1436 intersection = intersect_line_face(e, face, True)
1438 intersection = intersect_line_plane(v1.co, v2.co, face.verts[0].co, normal)
1440 if intersection != None:
1441 d1 = distance_point_to_plane(v1.co, face.verts[0].co, normal)
1442 d2 = distance_point_to_plane(v2.co, face.verts[0].co, normal)
1443 # If they have different signs, then the edge crosses the plane:
1444 if abs(d1 + d2) < abs(d1 - d2):
1445 # Make the first vertice the positive vertice:
1450 new.co = intersection
1452 newV1 = bVerts.new()
1453 newV1.co = intersection
1454 newV2 = bVerts.new()
1455 newV2.co = intersection
1456 newE1 = bEdges.new((v1, newV1))
1457 newE2 = bEdges.new((v2, newV2))
1460 new = list(bmesh.utils.edge_split(e, v1, 0.5))
1461 new[1].co = intersection
1463 new[0].select = False
1465 bEdges.remove(new[0])
1469 bm.to_mesh(context.active_object.data)
1470 bpy.ops.object.editmode_toggle()
1474 class Project(bpy.types.Operator):
1475 bl_idname = "mesh.edgetools_project"
1476 bl_label = "Project"
1477 bl_description = "Projects the selected vertices/edges onto the selected plane."
1478 bl_options = {'REGISTER', 'UNDO'}
1480 make_copy = BoolProperty(name = "Make Copy",
1481 description = "Make a duplicate of the vertices instead of moving it",
1484 def draw(self, context):
1485 layout = self.layout
1486 layout.prop(self, "make_copy")
1489 def poll(cls, context):
1490 ob = context.active_object
1491 return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
1494 def invoke(self, context, event):
1495 return self.execute(context)
1498 def execute(self, context):
1499 bpy.ops.object.editmode_toggle()
1501 bm.from_mesh(context.active_object.data)
1510 # Find the selected face. This will provide the plane to project onto:
1524 d = distance_point_to_plane(v.co, fVerts[0].co, normal)
1530 vector.length = abs(d)
1531 v.co = v.co - (vector * sign(d))
1534 bm.to_mesh(context.active_object.data)
1535 bpy.ops.object.editmode_toggle()
1539 # Project_End is for projecting/extending an edge to meet a plane.
1540 # This is used be selecting a face to define the plane then all the edges.
1541 # The add-on will then move the vertices in the edge that is closest to the
1542 # plane to the coordinates of the intersection of the edge and the plane.
1543 class Project_End(bpy.types.Operator):
1544 bl_idname = "mesh.edgetools_project_end"
1545 bl_label = "Project (End Point)"
1546 bl_description = "Projects the vertice of the selected edges closest to a plane onto that plane."
1547 bl_options = {'REGISTER', 'UNDO'}
1549 make_copy = BoolProperty(name = "Make Copy",
1550 description = "Make a duplicate of the vertice instead of moving it",
1552 keep_length = BoolProperty(name = "Keep Edge Length",
1553 description = "Maintain edge lengths",
1555 use_force = BoolProperty(name = "Use opposite vertices",
1556 description = "Force the usage of the vertices at the other end of the edge",
1558 use_normal = BoolProperty(name = "Project along normal",
1559 description = "Use the plane's normal as the projection direction",
1562 def draw(self, context):
1563 layout = self.layout
1564 ## layout.prop(self, "keep_length")
1565 if not self.keep_length:
1566 layout.prop(self, "use_normal")
1568 ## self.report({'ERROR_INVALID_INPUT'}, "Maintaining edge length not yet supported")
1569 ## self.report({'WARNING'}, "Projection may result in unexpected geometry")
1570 layout.prop(self, "make_copy")
1571 layout.prop(self, "use_force")
1575 def poll(cls, context):
1576 ob = context.active_object
1577 return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
1580 def invoke(self, context, event):
1581 return self.execute(context)
1584 def execute(self, context):
1585 bpy.ops.object.editmode_toggle()
1587 bm.from_mesh(context.active_object.data)
1596 # Find the selected face. This will provide the plane to project onto:
1609 if v1 in fVerts or v2 in fVerts:
1612 intersection = intersect_line_plane(v1.co, v2.co, fVerts[0].co, normal)
1613 if intersection != None:
1614 # Use abs because we don't care what side of plane we're on:
1615 d1 = distance_point_to_plane(v1.co, fVerts[0].co, normal)
1616 d2 = distance_point_to_plane(v2.co, fVerts[0].co, normal)
1617 # If d1 is closer than we use v1 as our vertice:
1618 # "xor" with 'use_force':
1619 if (abs(d1) < abs(d2)) is not self.use_force:
1622 v1.co = e.verts[0].co
1623 if self.keep_length:
1624 v1.co = intersection
1625 elif self.use_normal:
1627 vector.length = abs(d1)
1628 v1.co = v1.co - (vector * sign(d1))
1630 v1.co = intersection
1634 v2.co = e.verts[1].co
1635 if self.keep_length:
1636 v2.co = intersection
1637 elif self.use_normal:
1639 vector.length = abs(d2)
1640 v2.co = v2.co - (vector * sign(d2))
1642 v2.co = intersection
1645 bm.to_mesh(context.active_object.data)
1646 bpy.ops.object.editmode_toggle()
1652 # Blender currently does not have a CAD-style edge-based fillet function. This
1653 # is my atempt to create one. It should take advantage of BMesh and the ngon
1654 # capabilities for non-destructive modeling, if possible. This very well may
1655 # not result in nice quads and it will be up to the artist to clean up the mesh
1656 # back into quads if necessary.
1659 # - Faces are planar. This should, however, do a check an warn otherwise.
1661 # Developement Process:
1662 # Because this will eventaully prove to be a great big jumble of code and
1663 # various functionality, this is to provide an outline for the developement
1664 # and functionality wanted at each milestone.
1665 # 1) intersect_line_face: function to find the intersection point, if it
1666 # exists, at which a line intersects a face. The face does not have to
1667 # be planar, and can be an ngon. This will allow for a point to be placed
1668 # on the actual mesh-face for non-planar faces.
1669 # 2) Minimal propagation, single edge: Filleting of a single edge without
1670 # propagation of the fillet along "tangent" edges.
1671 # 3) Minimal propagation, multiple edges: Perform said fillet along/on
1673 # 4) "Tangency" detection code: because we have a mesh based geometry, this
1674 # have to make an educated guess at what is actually supposed to be
1675 # treated as tangent and what constitutes a sharp edge. This should
1676 # respect edges marked as sharp (does not propagate passed an
1677 # intersecting edge that is marked as sharp).
1678 # 5) Tangent propagation, single edge: Filleting of a single edge using the
1679 # above tangency detection code to continue the fillet to adjacent
1681 # 6) Tangent propagation, multiple edges: Same as above, but with multiple
1682 # edges selected. If multiple edges were selected along the same
1683 # tangency path, only one edge will be filleted. The others must be
1684 # ignored/discarded.
1685 class Fillet(bpy.types.Operator):
1686 bl_idname = "mesh.edgetools_fillet"
1687 bl_label = "Edge Fillet"
1688 bl_description = "Fillet the selected edges."
1689 bl_options = {'REGISTER', 'UNDO'}
1691 radius = FloatProperty(name = "Radius",
1692 description = "Radius of the edge fillet",
1693 min = 0.00001, max = 1024.0,
1695 prop = EnumProperty(name = "Propagation",
1696 items = [("m", "Minimal", "Minimal edge propagation"),
1697 ("t", "Tangential", "Tangential edge propagation")],
1699 prop_fac = FloatProperty(name = "Propagation Factor",
1700 description = "Corner detection sensitivity factor for tangential propagation",
1701 min = 0.0, max = 100.0,
1703 deg_seg = FloatProperty(name = "Degrees/Section",
1704 description = "Approximate degrees per section",
1705 min = 0.00001, max = 180.0,
1707 res = IntProperty(name = "Resolution",
1708 description = "Resolution of the fillet",
1709 min = 1, max = 1024,
1712 def draw(self, context):
1713 layout = self.layout
1714 layout.prop(self, "radius")
1715 layout.prop(self, "prop")
1716 if self.prop == "t":
1717 layout.prop(self, "prop_fac")
1718 layout.prop(self, "deg_seg")
1719 layout.prop(self, "res")
1723 def poll(cls, context):
1724 ob = context.active_object
1725 return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
1728 def invoke(self, context, event):
1729 return self.execute(context)
1732 def execute(self, context):
1733 bpy.ops.object.editmode_toggle()
1735 bm.from_mesh(bpy.context.active_object.data)
1742 # Robustness check: this does not support n-gons (at least for now)
1743 # because I have no idea how to handle them righ now. If there is
1744 # an n-gon in the mesh, warn the user that results may be nuts because
1747 # I'm not going to cause it to exit if there are n-gons, as they may
1748 # not be encountered.
1749 # @todo I would like this to be a confirmation dialoge of some sort
1750 # @todo I would REALLY like this to just handle n-gons. . . .
1752 if len(face.verts) > 4:
1753 self.report({'WARNING'},
1754 "Mesh contains n-gons which are not supported. Operation may fail.")
1757 # Get the selected edges:
1758 # Robustness check: boundary and wire edges are not fillet-able.
1759 edges = [e for e in bEdges if e.select and not e.is_boundary and not e.is_wire]
1762 axis_points = fillet_axis(e, self.radius)
1765 bm.to_mesh(bpy.context.active_object.data)
1766 bpy.ops.object.editmode_toggle()
1770 # For testing the mess that is "intersect_line_face" for possible math errors.
1771 # This will NOT be directly exposed to end users: it will always require running
1772 # Blender in debug mode.
1773 # So far no errors have been found. Thanks to anyone who tests and reports bugs!
1774 class Intersect_Line_Face(bpy.types.Operator):
1775 bl_idname = "mesh.edgetools_ilf"
1776 bl_label = "ILF TEST"
1777 bl_description = "TEST ONLY: INTERSECT_LINE_FACE"
1778 bl_options = {'REGISTER', 'UNDO'}
1781 def poll(cls, context):
1782 ob = context.active_object
1783 return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
1786 def invoke(self, context, event):
1787 return self.execute(context)
1790 def execute(self, context):
1791 # Make sure we really are in debug mode:
1792 if not bpy.app.debug:
1793 self.report({'ERROR_INVALID_INPUT'},
1794 "This is for debugging only: you should not be able to run this!")
1795 return {'CANCELLED'}
1797 bpy.ops.object.editmode_toggle()
1799 bm.from_mesh(bpy.context.active_object.data)
1814 if e.select and not e in face.edges:
1818 point = intersect_line_face(edge, face, True)
1824 bpy.ops.object.editmode_toggle()
1825 self.report({'ERROR_INVALID_INPUT'}, "point was \"None\"")
1826 return {'CANCELLED'}
1828 bm.to_mesh(bpy.context.active_object.data)
1829 bpy.ops.object.editmode_toggle()
1833 class VIEW3D_MT_edit_mesh_edgetools(bpy.types.Menu):
1834 bl_label = "EdgeTools"
1836 def draw(self, context):
1837 layout = self.layout
1839 layout.operator("mesh.edgetools_extend")
1840 layout.operator("mesh.edgetools_spline")
1841 layout.operator("mesh.edgetools_ortho")
1842 layout.operator("mesh.edgetools_shaft")
1843 layout.operator("mesh.edgetools_slice")
1844 layout.operator("mesh.edgetools_project")
1845 layout.operator("mesh.edgetools_project_end")
1847 ## Not ready for prime-time yet:
1848 layout.operator("mesh.edgetools_fillet")
1849 ## For internal testing ONLY:
1850 layout.operator("mesh.edgetools_ilf")
1853 def menu_func(self, context):
1854 self.layout.menu("VIEW3D_MT_edit_mesh_edgetools")
1855 self.layout.separator()
1858 # define classes for registration
1859 classes = [VIEW3D_MT_edit_mesh_edgetools,
1868 Intersect_Line_Face]
1871 # registering and menu integration
1873 if int(bpy.app.build_revision[0:5]) < 44800:
1874 print("Error in Edgetools:")
1875 print("This version of Blender does not support the necessary BMesh API.")
1876 print("Please download Blender 2.63 or newer.")
1879 bpy.utils.register_class(c)
1880 bpy.types.VIEW3D_MT_edit_mesh_specials.prepend(menu_func)
1883 # unregistering and removing menus
1886 bpy.utils.unregister_class(c)
1887 bpy.types.VIEW3D_MT_edit_mesh_specials.remove(menu_func)
1890 if __name__ == "__main__":