~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to release/scripts/addons/add_curve_torus_knots.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# ##### BEGIN GPL LICENSE BLOCK #####
2
 
#
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.
7
 
#
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.
12
 
#
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.
16
 
#
17
 
# ##### END GPL LICENSE BLOCK #####
18
 
 
19
 
 
20
 
bl_info = {
21
 
    "name": "Torus Knots",
22
 
    "author": "testscreenings",
23
 
    "version": (0,1),
24
 
    "blender": (2, 5, 9),
25
 
    "location": "View3D > Add > Curve",
26
 
    "description": "Adds many types of (torus) knots",
27
 
    "warning": "",
28
 
    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
29
 
        "Scripts/Curve/Torus_Knot",
30
 
    "tracker_url": "https://projects.blender.org/tracker/index.php?"\
31
 
        "func=detail&aid=22403",
32
 
    "category": "Add Curve"}
33
 
    
34
 
    
35
 
##------------------------------------------------------------
36
 
#### import modules
37
 
import bpy
38
 
from bpy.props import *
39
 
from math import sin, cos, pi
40
 
from bpy_extras.object_utils import AddObjectHelper, object_data_add
41
 
 
42
 
    
43
 
########################################################################
44
 
####################### Knot Definitions ###############################
45
 
########################################################################
46
 
def Torus_Knot(self):
47
 
    p = self.torus_p
48
 
    q = self.torus_q
49
 
    w = self.torus_w
50
 
    res = self.torus_res
51
 
    h = self.torus_h
52
 
    u = self.torus_u
53
 
    v = self.torus_v
54
 
    rounds = self.torus_rounds
55
 
    
56
 
    newPoints = []
57
 
    angle = 2*rounds
58
 
    step = angle/(res-1)
59
 
    scale = h
60
 
    height = w
61
 
 
62
 
    for i in range(res-1):
63
 
        t = ( i*step*pi)
64
 
        
65
 
        x = (2 * scale + cos((q*t)/p*v)) * cos(t * u)
66
 
        y = (2 * scale + cos((q*t)/p*v)) * sin(t * u)
67
 
        z = sin(q*t/p) * height
68
 
        
69
 
        newPoints.extend([x,y,z,1])
70
 
 
71
 
    return newPoints
72
 
 
73
 
 
74
 
##------------------------------------------------------------
75
 
# Main Function
76
 
def create_torus_knot(self, context):
77
 
    verts = Torus_Knot(self)
78
 
 
79
 
    curve_data = bpy.data.curves.new(name='Torus Knot', type='CURVE')
80
 
    spline = curve_data.splines.new(type='NURBS')
81
 
    spline.points.add(int(len(verts)*0.25 - 1))
82
 
    spline.points.foreach_set('co', verts)
83
 
    spline.use_endpoint_u = True
84
 
    spline.use_cyclic_u = True
85
 
    spline.order_u = 4
86
 
    curve_data.dimensions = '3D'    
87
 
    
88
 
    if self.geo_surf:
89
 
        curve_data.bevel_depth = self.geo_bDepth
90
 
        curve_data.bevel_resolution = self.geo_bRes
91
 
        curve_data.fill_mode = 'FULL'
92
 
        curve_data.extrude = self.geo_extrude
93
 
        #curve_data.offset = self.geo_width # removed, somehow screws things up all of a sudden
94
 
        curve_data.resolution_u = self.geo_res
95
 
    
96
 
    new_obj = object_data_add(context, curve_data, operator=self)
97
 
 
98
 
 
99
 
class torus_knot_plus(bpy.types.Operator, AddObjectHelper):
100
 
    ''''''
101
 
    bl_idname = "curve.torus_knot_plus"
102
 
    bl_label = "Torus Knot +"
103
 
    bl_options = {'REGISTER', 'UNDO'}
104
 
    bl_description = "adds many types of knots"
105
 
 
106
 
    #### general options
107
 
    options_plus = BoolProperty(name="plus options",
108
 
                default=False,
109
 
                description="Show more options (the plus part)")
110
 
 
111
 
    #### GEO Options
112
 
    geo_surf = BoolProperty(name="Surface",
113
 
                default=True)
114
 
    geo_bDepth = FloatProperty(name="bevel",
115
 
                default=0.08,
116
 
                min=0, soft_min=0)
117
 
    geo_bRes = IntProperty(name="bevel res",
118
 
                default=2,
119
 
                min=0, soft_min=0,
120
 
                max=4, soft_max=4)
121
 
    geo_extrude = FloatProperty(name="extrude",
122
 
                default=0.0,
123
 
                min=0, soft_min=0)
124
 
    geo_res = IntProperty(name="resolution",
125
 
                default=12,
126
 
                min=1, soft_min=1)
127
 
 
128
 
 
129
 
    #### Parameters
130
 
    torus_res = IntProperty(name="Resoulution",
131
 
                default=100,
132
 
                min=3, soft_min=3,
133
 
                description='Resolution, Number of controlverticies')
134
 
    torus_p = IntProperty(name="p",
135
 
                default=2,
136
 
                min=1, soft_min=1,
137
 
                #max=1, soft_max=1,
138
 
                description="p")
139
 
    torus_q = IntProperty(name="q",
140
 
                default=3,
141
 
                min=1, soft_min=1,
142
 
                #max=1, soft_max=1,
143
 
                description="q")
144
 
    torus_w = FloatProperty(name="Height",
145
 
                default=1,
146
 
                #min=0, soft_min=0,
147
 
                #max=1, soft_max=1,
148
 
                description="Height in Z")
149
 
    torus_h = FloatProperty(name="Scale",
150
 
                default=1,
151
 
                #min=0, soft_min=0,
152
 
                #max=1, soft_max=1,
153
 
                description="Scale, in XY")
154
 
    torus_u = IntProperty(name="u",
155
 
                default=1,
156
 
                min=1, soft_min=1,
157
 
                #max=1, soft_max=1,
158
 
                description="u")
159
 
    torus_v = IntProperty(name="v",
160
 
                default=1,
161
 
                min=1, soft_min=1,
162
 
                #max=1, soft_max=1,
163
 
                description="v")
164
 
    torus_rounds = IntProperty(name="Rounds",
165
 
                default=2,
166
 
                min=1, soft_min=1,
167
 
                #max=1, soft_max=1,
168
 
                description="Rounds")
169
 
 
170
 
    ##### DRAW #####
171
 
    def draw(self, context):
172
 
        layout = self.layout
173
 
 
174
 
        # general options        
175
 
        layout.label(text="Torus Knot Parameters:")
176
 
 
177
 
        # Parameters 
178
 
        box = layout.box()
179
 
        box.prop(self, 'torus_res')
180
 
        box.prop(self, 'torus_w')
181
 
        box.prop(self, 'torus_h')
182
 
        box.prop(self, 'torus_p')
183
 
        box.prop(self, 'torus_q')
184
 
        box.prop(self, 'options_plus')
185
 
        if self.options_plus:
186
 
            box.prop(self, 'torus_u')
187
 
            box.prop(self, 'torus_v')
188
 
            box.prop(self, 'torus_rounds')
189
 
 
190
 
        # surface Options
191
 
        col = layout.column()
192
 
        col.label(text="Geometry Options:")
193
 
        box = layout.box()
194
 
        box.prop(self, 'geo_surf')
195
 
        if self.geo_surf:
196
 
            box.prop(self, 'geo_bDepth')
197
 
            box.prop(self, 'geo_bRes')
198
 
            box.prop(self, 'geo_extrude')
199
 
            box.prop(self, 'geo_res')
200
 
 
201
 
        col = layout.column()
202
 
        col.prop(self, 'location')
203
 
        col.prop(self, 'rotation')
204
 
    
205
 
    ##### POLL #####
206
 
    @classmethod
207
 
    def poll(cls, context):
208
 
        return context.scene != None
209
 
 
210
 
    ##### EXECUTE #####
211
 
    def execute(self, context):
212
 
        # turn off undo
213
 
        undo = bpy.context.user_preferences.edit.use_global_undo
214
 
        bpy.context.user_preferences.edit.use_global_undo = False
215
 
        
216
 
        if not self.options_plus:
217
 
            self.torus_rounds = self.torus_p
218
 
 
219
 
        #recoded for add_utils
220
 
        create_torus_knot(self, context)
221
 
        
222
 
        # restore pre operator undo state
223
 
        bpy.context.user_preferences.edit.use_global_undo = undo
224
 
 
225
 
        return {'FINISHED'}
226
 
 
227
 
################################################################################
228
 
##### REGISTER #####
229
 
 
230
 
def torus_knot_plus_button(self, context):
231
 
    self.layout.operator(torus_knot_plus.bl_idname, text="Torus Knot +", icon="PLUGIN")
232
 
 
233
 
 
234
 
def register():
235
 
    bpy.utils.register_module(__name__)
236
 
 
237
 
    bpy.types.INFO_MT_curve_add.append(torus_knot_plus_button)
238
 
 
239
 
def unregister():
240
 
    bpy.utils.unregister_module(__name__)
241
 
 
242
 
    bpy.types.INFO_MT_curve_add.remove(torus_knot_plus_button)
243
 
 
244
 
if __name__ == "__main__":
245
 
    register()