1
'''# +---------------------------------------------------------+
2
# | Copyright (c) 2005-2010 Anthony D'Agostino |
3
# | http://home.comcast.net/~chronosphere |
4
# | scorpius@netzero.com |
5
# | February 12, 2005 |
6
# | Torus Knot Generator |
7
# | Adds the famous missing primitive to Blender |
8
# +---------------------------------------------------------+
10
# ***** BEGIN GPL LICENSE BLOCK *****
12
# This program is free software; you can redistribute it and/or
13
# modify it under the terms of the GNU General Public License
14
# as published by the Free Software Foundation; either version 2
15
# of the License, or (at your option) any later version.
17
# This program is distributed in the hope that it will be useful,
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
# GNU General Public License for more details.
22
# You should have received a copy of the GNU General Public License
23
# along with this program; if not, write to the Free Software Foundation,
24
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
# ***** END GPL LICENCE BLOCK *****
30
"author": "Anthony D'Agostino",
33
"location": "View3D > Add > Mesh ",
34
"url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/Add_TorusKnot",
35
"category": "Add Mesh"}
37
import bpy, mathutils, math
39
def create_mesh_object(context, verts, edges, faces, name):
41
mesh = bpy.data.meshes.new(name)
42
# Make a mesh from a list of verts/edges/faces.
43
mesh.from_pydata(verts, edges, faces)
44
# Update mesh geometry after adding stuff.
46
from bpy_extras import object_utils
47
return object_utils.object_data_add(context, mesh, operator=None)
49
# ========================
50
# === Torus Knot Block ===
51
# ========================
53
x = math.cos(t) - 2*math.cos(2*t)
54
y = math.sin(t) + 2*math.sin(2*t)
56
return mathutils.Vector([x,y,z])
59
x = 10 * (math.cos(t) + math.cos(3*t)) + math.cos(2*t) + math.cos(4*t)
60
y = 6 * math.sin(t) + 10 * math.sin(3*t)
61
z = 4 * math.sin(3*t) * math.sin(5*t/2) + 4*math.sin(4*t) - 2*math.sin(6*t)
62
return mathutils.Vector([x,y,z]) * 0.2
65
x = 2.5*math.cos(t+math.pi)/3 + 2*math.cos(3*t)
66
y = 2.5*math.sin(t)/3 + 2*math.sin(3*t)
67
z = 1.5*math.sin(4*t) + math.sin(2*t)/3
68
return mathutils.Vector([x,y,z])
70
def make_verts(ures, vres, r2, knotfunc):
73
t1 = (i+0) * 2*math.pi/ures
74
t2 = (i+1) * 2*math.pi/ures
75
a = knotfunc(t1) # curr point
76
b = knotfunc(t2) # next point
77
a,b = map(mathutils.Vector, (a,b))
85
k = j * 2*math.pi/vres
86
l = (math.cos(k),0.0,math.sin(k))
87
l = mathutils.Vector(l)
97
def make_faces(ures, vres):
99
for u in range(0, ures):
100
for v in range(0, vres):
102
p2 = v + ((u+1)%ures)*vres
103
p4 = (v+1)%vres + u*vres
104
p3 = (v+1)%vres + ((u+1)%ures)*vres
105
faces.append([p4, p3, p2, p1])
108
def make_knot(knotidx, ures):
110
knotfunc = knots[knotidx-1]
113
verts = make_verts(ures, vres, r2, knotfunc)
114
faces = make_faces(ures, vres)
115
return (verts, faces)
117
class AddTorusKnot(bpy.types.Operator):
118
'''Add a torus-knot mesh.'''
119
bl_idname = "mesh.primitive_torusknot_add"
120
bl_label = "Add Torus Knot"
121
bl_options = {"REGISTER", "UNDO"}
123
resolution = bpy.props.IntProperty(name="Resolution",
124
description="Resolution of the Torus Knot",
125
default=80, min=30, max=256)
127
objecttype = bpy.props.IntProperty(name="Knot Type",
128
description="Type of Knot",
129
default=1, min=1, max=3)
131
def execute(self, context):
132
verts, faces = make_knot(self.objecttype,
134
obj = create_mesh_object(context, verts, [], faces, "Torus Knot")
137
def menu_func(self, context):
138
self.layout.operator(AddTorusKnot.bl_idname, text="Torus Knot", icon="MESH_CUBE")
141
bpy.utils.register_module(__name__)
142
bpy.types.INFO_MT_mesh_add.append(menu_func)
145
bpy.utils.unregister_module(__name__)
146
bpy.types.INFO_MT_mesh_add.remove(menu_func)
148
if __name__ == "__main__":