Fix io_anim_camera error exporting cameras with quotes in their name
[blender-addons.git] / curve_tools / outline.py
blob8efd6f41512c7ddac4c4e71e8772bd7fe34fd877
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # by Yann Bertrand, january 2014.
5 bl_info = {
6 "name": "Curve Outline",
7 "description": "creates an Outline",
8 "author": "Yann Bertrand (jimflim), Vladimir Spivak (cwolf3d)",
9 "version": (0, 5),
10 "blender": (2, 69, 0),
11 "category": "Object",
14 import bpy
15 from mathutils import Vector
16 from mathutils.geometry import intersect_line_line
18 from . import util
21 def createOutline(curve, outline):
23 for spline in curve.data.splines[:]:
24 if spline.type == 'BEZIER':
25 p = spline.bezier_points
26 if len(p) < 2:
27 return
28 out = []
30 n = ((p[0].handle_right - p[0].co).normalized() - (p[0].handle_left - p[0].co).normalized()).normalized()
31 n = Vector((-n[1], n[0], n[2]))
32 o = p[0].co + outline * n
33 out.append(o)
35 for i in range(1,len(p)-1):
36 n = ((p[i].handle_right - p[i].co).normalized() - (p[i].handle_left - p[i].co).normalized()).normalized()
37 n = Vector((-n[1], n[0], n[2]))
38 o = intersect_line_line(out[-1], (out[-1]+p[i].co - p[i-1].co), p[i].co, p[i].co + n)[0]
39 out.append(o)
41 n = ((p[-1].handle_right - p[-1].co).normalized() - (p[-1].handle_left - p[-1].co).normalized()).normalized()
42 n = Vector((-n[1], n[0], n[2]))
43 o = p[-1].co + outline * n
44 out.append(o)
46 curve.data.splines.new(spline.type)
47 if spline.use_cyclic_u:
48 curve.data.splines[-1].use_cyclic_u = True
49 p_out = curve.data.splines[-1].bezier_points
50 p_out.add(len(out)-1)
52 for i in range(len(out)):
53 p_out[i].handle_left_type = 'FREE'
54 p_out[i].handle_right_type = 'FREE'
56 p_out[i].co = out[i]
58 if i<len(out)-1:
59 l = (p[i + 1].co - p[i].co).length
60 l2 = (out[i] - out[i + 1]).length
62 if i==0:
63 p_out[i].handle_left = out[i] + ((p[i].handle_left - p[i].co) * l2/l)
64 if i<len(out)-1:
65 p_out[i + 1].handle_left = out[i + 1] + ((p[i + 1].handle_left - p[i + 1].co) * l2/l)
66 p_out[i].handle_right = out[i] + ((p[i].handle_right - p[i].co) * l2/l)
68 for i in range(len(p)):
69 p_out[i].handle_left_type = p[i].handle_left_type
70 p_out[i].handle_right_type = p[i].handle_right_type
72 else:
73 if len(spline.points) < 2:
74 return
75 p = []
76 for point in spline.points:
77 v = Vector((point.co[0], point.co[1], point.co[2]))
78 p.append(v)
79 out = []
81 n = ((p[1] - p[0]).normalized() - (p[-1] - p[0]).normalized()).normalized()
82 n = Vector((-n[1], n[0], n[2]))
83 o = p[0] + outline * n
84 out.append(o)
86 for i in range(1,len(p)-1):
87 n = ((p[i+1] - p[i]).normalized() - (p[i-1] - p[i]).normalized()).normalized()
88 n = Vector((-n[1], n[0], n[2]))
89 o = intersect_line_line(out[-1], (out[-1]+p[i] - p[i-1]), p[i], p[i] + n)[0]
90 out.append(o)
92 n = ((p[0] - p[-1]).normalized() - (p[-2] - p[-1]).normalized()).normalized()
93 n = Vector((-n[1], n[0], n[2]))
94 o = p[-1] + outline * n
95 out.append(o)
97 curve.data.splines.new(spline.type)
98 if spline.use_cyclic_u:
99 curve.data.splines[-1].use_cyclic_u = True
100 p_out = curve.data.splines[-1].points
101 p_out.add(len(out)-1)
103 for i in range(len(out)):
104 p_out[i].co = (out[i][0], out[i][1], out[i][2], 0.0)
106 return
109 class CurveOutline(bpy.types.Operator):
110 """Curve Outliner"""
111 bl_idname = "curvetools.outline"
112 bl_label = "Create Outline"
113 bl_options = {'REGISTER', 'UNDO'}
114 outline: bpy.props.FloatProperty(name="Amount", default=0.1)
116 @classmethod
117 def poll(cls, context):
118 return util.Selected1OrMoreCurves()
120 def execute(self, context):
121 createOutline(context.object, self.outline)
122 return {'FINISHED'}
124 def invoke(self, context, event):
125 return context.window_manager.invoke_props_popup(self, event)
127 def menu_func(self, context):
128 self.layout.operator(CurveOutline.bl_idname)
130 def register():
131 for cls in classes:
132 bpy.utils.register_class(operators)
134 def unregister():
135 for cls in classes:
136 bpy.utils.unregister_class(operators)
138 if __name__ == "__main__":
139 register()
141 operators = [CurveOutline]