1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
23 "name": "Extrude Along Curve",
24 "author": "Andrew Hale (TrumanBlending)",
26 "blender": (2, 63, 0),
28 "description": "Extrude a face along a Bezier Curve",
31 'tracker_url': 'https://developer.blender.org/T32585',
37 from mathutils import Vector, Quaternion
38 from math import ceil, floor, pi
41 def eval_bez_tan(mat, points, t):
48 return (mat * (points[upper].handle_right - points[upper].co)).normalized()
49 elif upper == num - 1:
50 return (mat * (points[upper].co - points[upper].handle_left)).normalized()
52 return (mat * (points[upper].co - points[upper].handle_left)).normalized()
55 pupper = points[upper]
56 plower = points[lower]
57 tangent = -3 * (1 - t) ** 2 * plower.co + (-6 * (1 - t) * t + 3 * (1 - t) ** 2) * plower.handle_right + (-3 * t ** 2 + 3 * (1 - t) * 2 * t) * pupper.handle_left + 3 * t ** 2 * pupper.co
58 tangent = mat * tangent
63 def eval_bez(mat, points, t):
69 return mat * points[upper].co
72 pupper = points[upper]
73 plower = points[lower]
74 pos = (1 - t) ** 3 * plower.co + 3 * (1 - t) ** 2 * t * plower.handle_right + 3 * (1 - t) * t ** 2 * pupper.handle_left + t ** 3 * pupper.co
78 def curve_ob_enum(self, context):
79 obs = context.scene.objects
80 cuobs = [(str(i), ob.name, ob.name) for i, ob in enumerate(obs) if ob.type == 'CURVE']
81 curve_ob_enum.temp = cuobs
85 class ExtrudeAlongCurve(bpy.types.Operator):
86 bl_idname = "mesh.extrude_along_curve"
87 bl_label = "Extrude Along Curve"
88 bl_options = {'REGISTER', 'UNDO'}
90 resolution = bpy.props.IntProperty(name="Resolution", default=1, min=1, soft_max=100)
91 scale = bpy.props.FloatProperty(name="Scale", default=1.0, soft_min=0.0, soft_max=5.0)
92 rotation = bpy.props.FloatProperty(name="Rotation", default=0.0, soft_min=-2 * pi, soft_max=2 * pi, subtype='ANGLE')
93 splineidx = bpy.props.IntProperty(name="Spline Index", default=0, min=0)
94 snapto = bpy.props.BoolProperty(name="Snap To Face", default=True)
95 curveob = bpy.props.EnumProperty(name="Curve", items=curve_ob_enum)
98 def poll(self, context):
99 ob = context.active_object
100 for cuob in context.scene.objects:
101 if cuob.type == 'CURVE':
106 return (ob is not None) and (ob.type == 'MESH') and (context.mode == 'EDIT_MESH')
108 def draw(self, context):
110 layout.prop(self, "curveob", text="", icon='CURVE_DATA')
111 layout.prop(self, "resolution")
112 layout.prop(self, "scale")
113 layout.prop(self, "rotation")
114 layout.prop(self, "splineidx")
115 layout.prop(self, "snapto")
117 def execute(self, context):
118 ob = bpy.context.active_object
120 bm = bmesh.from_edit_mesh(me)
122 # Get the selected curve object and the required spline
123 cuob = context.scene.objects[int(self.curveob)]
126 self.splineidx = min(self.splineidx, len(cu.splines) - 1)
127 p = cu.splines[self.splineidx].bezier_points
129 # Get the property values
130 res = self.resolution
132 rotation = self.rotation
133 dscale = (1 - scale) / res
134 drot = rotation / res
136 # Get the matrices to convert between spaces
137 cmat = ob.matrix_world.inverted() * cuob.matrix_world
138 ctanmat = cmat.to_3x3().inverted().transposed()
140 # The list of parameter values to evaluate the bezier curve at
141 tvals = [t / res for t in range(res + 1)]
143 # Get the first selected face, if none, cancel
150 # Get the position vecs on the curve and tangent values
151 bezval = [eval_bez(cmat, p, t) for t in tvals]
152 beztan = [eval_bez_tan(ctanmat, p, t) for t in tvals]
153 bezquat = [0] * len(tvals)
156 bezquat[0] = beztan[0].to_track_quat('Z', 'Y')
157 fquat = bezquat[0].inverted()
159 # Calculate the min twist orientations
160 for i in range(1, res + 1):
161 ang = beztan[i - 1].angle(beztan[i], 0.0)
163 axis = beztan[i - 1].cross(beztan[i])
164 q = Quaternion(axis, ang)
165 bezquat[i] = q * bezquat[i - 1]
167 bezquat[i] = bezquat[i - 1].copy()
169 # Get the faces to be modified
171 # no = f.normal.copy()
172 faces = [f.copy() for i in range(res)]
174 # Offset if we need to snap to the face
175 offset = Vector() if not self.snapto else (f.calc_center_median() - bezval[0])
177 # For each of the faces created, set their vert positions and create side faces
178 for i, data in enumerate(zip(faces, bezval[1:], bezquat[1:])):
181 cen = fn.calc_center_median()
183 rotquat = Quaternion((0, 0, 1), i * drot)
186 v.co = quat * rotquat * fquat * (v.co - cen) * (1 - (i + 1) * dscale) + pos + offset
188 for ll, ul in zip(fprev.loops, fn.loops):
189 ff = bm.faces.new((ll.vert, ll.link_loop_next.vert, ul.link_loop_next.vert, ul.vert))
192 bm.faces.remove(fprev)
203 bpy.utils.register_module(__name__)
207 bpy.utils.unregister_module(__name__)
210 if __name__ == "__main__":