~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/scripts/addons/add_mesh_extra_objects/add_mesh_torusknot.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
# +---------------------------------------------------------+
 
9
 
 
10
# ***** BEGIN GPL LICENSE BLOCK *****
 
11
#
 
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.
 
16
#
 
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.
 
21
#
 
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.
 
25
#
 
26
# ***** END GPL LICENCE BLOCK *****
 
27
 
 
28
bl_info = {
 
29
        "name": "Torus Knot",
 
30
        "author": "Anthony D'Agostino",
 
31
        "version": (1, 0),
 
32
        "blender": (2, 5, 7),
 
33
        "location": "View3D > Add > Mesh ",
 
34
        "url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/Add_TorusKnot",
 
35
        "category": "Add Mesh"}
 
36
'''     
 
37
import bpy, mathutils, math
 
38
 
 
39
def create_mesh_object(context, verts, edges, faces, name):
 
40
    # Create new mesh
 
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.
 
45
    mesh.update()
 
46
    from bpy_extras import object_utils
 
47
    return object_utils.object_data_add(context, mesh, operator=None)
 
48
 
 
49
# ========================
 
50
# === Torus Knot Block ===
 
51
# ========================
 
52
def k1(t):
 
53
        x = math.cos(t) - 2*math.cos(2*t)
 
54
        y = math.sin(t) + 2*math.sin(2*t)
 
55
        z = math.sin(3*t)
 
56
        return mathutils.Vector([x,y,z])
 
57
 
 
58
def k2(t):
 
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
 
63
 
 
64
def k3(t):
 
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])
 
69
 
 
70
def make_verts(ures, vres, r2, knotfunc):
 
71
        verts = []
 
72
        for i in range(ures):
 
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))
 
78
                e = a-b
 
79
                f = a+b
 
80
                g = e.cross(f)
 
81
                h = e.cross(g)
 
82
                g.normalize()
 
83
                h.normalize()
 
84
                for j in range(vres):
 
85
                        k = j * 2*math.pi/vres
 
86
                        l = (math.cos(k),0.0,math.sin(k))
 
87
                        l = mathutils.Vector(l)
 
88
                        m = l * r2
 
89
                        x,y,z = m
 
90
                        n = h*x
 
91
                        o = g*z
 
92
                        p = n+o
 
93
                        q = a+p
 
94
                        verts.append(q)
 
95
        return verts
 
96
 
 
97
def make_faces(ures, vres):
 
98
        faces = []
 
99
        for u in range(0, ures):
 
100
                for v in range(0, vres):
 
101
                        p1 = v + u*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])
 
106
        return faces
 
107
 
 
108
def make_knot(knotidx, ures):
 
109
        knots = [k1,k2,k3]
 
110
        knotfunc = knots[knotidx-1]
 
111
        vres = ures//10
 
112
        r2 = 0.5
 
113
        verts = make_verts(ures, vres, r2, knotfunc)
 
114
        faces = make_faces(ures, vres)
 
115
        return (verts, faces)
 
116
 
 
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"}
 
122
 
 
123
        resolution = bpy.props.IntProperty(name="Resolution",
 
124
                description="Resolution of the Torus Knot",
 
125
                default=80, min=30, max=256)
 
126
 
 
127
        objecttype = bpy.props.IntProperty(name="Knot Type",
 
128
                description="Type of Knot",
 
129
                default=1, min=1, max=3)
 
130
 
 
131
        def execute(self, context):
 
132
                verts, faces = make_knot(self.objecttype,
 
133
                                                                 self.resolution)
 
134
                obj = create_mesh_object(context, verts, [], faces, "Torus Knot")
 
135
                return {"FINISHED"}
 
136
'''
 
137
def menu_func(self, context):
 
138
        self.layout.operator(AddTorusKnot.bl_idname, text="Torus Knot", icon="MESH_CUBE")
 
139
 
 
140
def register():
 
141
    bpy.utils.register_module(__name__)
 
142
    bpy.types.INFO_MT_mesh_add.append(menu_func)
 
143
 
 
144
def unregister():
 
145
    bpy.utils.unregister_module(__name__)
 
146
    bpy.types.INFO_MT_mesh_add.remove(menu_func)
 
147
 
 
148
if __name__ == "__main__":
 
149
        register()
 
150
'''