[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [677] trunk/py/scripts/addons/ add_curve_torus_knots.py: addons/add_curve_torus_knots.py

Brendon Murphy meta.androcto1 at gmail.com
Sun May 23 06:22:16 CEST 2010


Revision: 677
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=677
Author:   meta-androcto
Date:     2010-05-23 06:22:09 +0200 (Sun, 23 May 2010)

Log Message:
-----------
addons/add_curve_torus_knots.py
script introduced to trunk.
the script makes use of mfoxdoggs revision 28923.
the script shows up in the add/curve menu.

Revision Links:
--------------
    http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=28923

Added Paths:
-----------
    trunk/py/scripts/addons/add_curve_torus_knots.py

Added: trunk/py/scripts/addons/add_curve_torus_knots.py
===================================================================
--- trunk/py/scripts/addons/add_curve_torus_knots.py	                        (rev 0)
+++ trunk/py/scripts/addons/add_curve_torus_knots.py	2010-05-23 04:22:09 UTC (rev 677)
@@ -0,0 +1,487 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+bl_addon_info = {
+    'name': 'Add Curve: Torus Knots',
+    'author': 'testscreenings',
+    'version': '0.1',
+    'blender': (2, 5, 2),
+    'location': 'Add Curve Menu',
+    'url': '',
+    'description': 'adds many types of knots',
+    'url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
+        'Scripts/Curve/Torus_Knot',
+    'category': 'Add Curve'}
+##------------------------------------------------------------
+#### import modules
+import bpy
+from bpy.props import *
+from mathutils import *
+from math import *
+
+##------------------------------------------------------------
+# calculates the matrix for the new object
+# depending on user pref
+def align_matrix(context):
+    loc = TranslationMatrix(context.scene.cursor_location)
+    obj_align = context.user_preferences.edit.object_align
+    if (context.space_data.type == 'VIEW_3D'
+        and obj_align == 'VIEW'):
+        rot = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
+    else:
+        rot = Matrix()
+    align_matrix = loc * rot
+    return align_matrix
+
+##------------------------------------------------------------
+#### Curve creation functions
+# sets bezierhandles to auto
+def setBezierHandles(obj, mode = 'AUTOMATIC'):
+    scene = bpy.context.scene
+    if obj.type != 'CURVE':
+        return
+    scene.objects.active = obj
+    bpy.ops.object.mode_set(mode='EDIT', toggle=True)
+    bpy.ops.curve.select_all(action='SELECT')
+    bpy.ops.curve.handle_type_set(type=mode)
+    bpy.ops.object.mode_set(mode='OBJECT', toggle=True)
+
+# get array of vertcoordinates acording to splinetype
+def vertsToPoints(Verts, splineType):
+    # main vars
+    vertArray = []
+
+    # array for BEZIER spline output (V3)
+    if splineType == 'BEZIER':
+        for v in Verts:
+            vertArray += v
+
+    # array for nonBEZIER output (V4)
+    else:
+        for v in Verts:
+            vertArray += v
+            if splineType == 'NURBS':
+                vertArray.append(1) #for nurbs w=1
+            else: #for poly w=0
+                vertArray.append(0)
+    return vertArray
+
+# create new CurveObject from vertarray and splineType
+def createCurve(vertArray, GEO, options, curveOptions, align_matrix):
+    # options to vars
+    splineType = options[0] # output splineType 'POLY' 'NURBS' 'BEZIER'
+    name = options[1] # KnotType as name
+
+    # create curve
+    scene = bpy.context.scene
+    newCurve = bpy.data.curves.new(name, type = 'CURVE') # curvedatablock
+    newSpline = newCurve.splines.new(type = splineType) # spline
+
+    # create spline from vertarray
+    if splineType == 'BEZIER':
+        newSpline.bezier_points.add(int(len(vertArray)*0.33))
+        newSpline.bezier_points.foreach_set('co', vertArray)
+    else:
+        newSpline.points.add(int(len(vertArray)*0.25 - 1))
+        newSpline.points.foreach_set('co', vertArray)
+        newSpline.endpoint_u = True
+
+    # set curveOptions
+    shape = curveOptions[0]
+    cyclic_u = curveOptions[1]
+    endp_u = curveOptions[2]
+    order_u = curveOptions[3]
+    handleType = curveOptions[4]
+
+    newCurve.dimensions = shape
+    newSpline.cyclic_u = cyclic_u
+    newSpline.endpoint_u = endp_u
+    newSpline.order_u = order_u
+
+    # GEO Options
+    surf = GEO[0]
+    bDepth = GEO[1]
+    bRes = GEO[2]
+    extrude = GEO[3]
+    width = GEO[4]
+    res = GEO[5]
+
+    if surf:
+        newCurve.bevel_depth = bDepth
+        newCurve.bevel_resolution = bRes
+        newCurve.front = False
+        newCurve.back = False
+        newCurve.extrude = extrude
+        newCurve.width = width
+        newCurve.resolution_u = res
+
+    # create object with newCurve
+    new_obj = bpy.data.objects.new(name, newCurve) # object
+    scene.objects.link(new_obj) # place in active scene
+    new_obj.selected = True # set as selected
+    scene.objects.active = new_obj  # set as active
+    new_obj.matrix = align_matrix # apply matrix
+
+    # set bezierhandles
+    if splineType == 'BEZIER':
+        setBezierHandles(new_obj, handleType)
+
+    return
+
+########################################################################
+####################### Knot Definitions ###############################
+########################################################################
+
+#### TORUS KNOT
+def Torus_Knot_Curve(p=2, q=3, w=1, res=24, formula=0, h=1, u=1 ,v=1, rounds=2):
+    newPoints = []
+    angle = (2.0/360.0)*360*rounds
+    #angle = 360/pi
+    step = angle/(res-1)
+    scale = h
+    height = w
+
+    if formula == 0:
+        for i in range(res-1):
+            t = ( i*step*pi)
+            
+            x = (2 * scale + cos((q*t)/p*v)) * cos(t * u)
+            y = (2 * scale + cos((q*t)/p*v)) * sin(t * u)
+            z = sin(q*t/p) * height
+            
+            newPoints.append([x,y,z])
+    
+    if formula == 1:
+        for i in range(res-1):
+            t = ( i*step)
+            
+            x = ((2*w + cos((q*t)/p)) * cos(t*p)) * sin(t/p)
+            y = ((2*w + cos((q*t)/p)) * sin(t*p)) * sin(t/p)
+            z = sin(q*t/p)
+            
+            newPoints.append([x,y,z])
+    
+    if formula == 2:
+        for i in range(res-1):
+            t = ( i*step)
+            beta = t*pi
+            
+            r = 0.8 + 1.6 * sin(q * beta/p)
+            theta = 2 * beta
+            phi = 0.6 * pi * sin(q * beta/p)
+            
+            x = r * cos(phi) * cos(theta)
+            y = r * cos(phi) * sin(theta)
+            z = r * sin(phi)
+            
+            newPoints.append([x,y,z])
+
+
+    #newPoints = [[-1,-1,0], [-1,1,0], [1,1,0], [1,-1,0]]
+    return newPoints
+
+##------------------------------------------------------------
+# Main Function
+def main(context, param, GEO, options, curveOptions, align_matrix):
+    # deselect all objects
+    bpy.ops.object.select_all(action='DESELECT')
+
+    # options
+    knotType = options[1]
+    splineType = options[0]
+
+
+    # get verts
+    if knotType == 'Torus_Knot':
+        verts = Torus_Knot_Curve(param[0], param[1], param[2], param[3], param[4],
+                                  param[5], param[6], param[7], param[8])
+
+    # turn verts into array
+    vertArray = vertsToPoints(verts, splineType)
+
+    # create object
+    createCurve(vertArray, GEO, options, curveOptions, align_matrix)
+
+    return
+
+class torus_knot_plus(bpy.types.Operator):
+    ''''''
+    bl_idname = "torus_knot_plus"
+    bl_label = "Torus Knot +"
+    bl_options = {'REGISTER', 'UNDO'}
+    bl_description = "adds many types of knots"
+
+    # align_matrix for the invoke
+    align_matrix = Matrix()
+
+    #### general options
+    KnotTypes = [
+                ('Torus_Knot', 'Torus Knot', 'Torus_Knot')
+                ]
+    KnotType = EnumProperty(name="Type",
+                description="Form of Curve to create",
+                items=KnotTypes)
+    SplineTypes = [
+                ('NURBS', 'Nurbs', 'NURBS'),
+                ('POLY', 'Poly', 'POLY'),
+                ('BEZIER', 'Bezier', 'BEZIER')]
+    outputType = EnumProperty(name="Output splines",
+                description="Type of splines to output",
+                items=SplineTypes)
+
+    #### GEO Options
+    geo_surf = BoolProperty(name="Surface",
+                default=True)
+    geo_bDepth = FloatProperty(name="bevel",
+                default=0.08,
+                min=0, soft_min=0)
+    geo_bRes = IntProperty(name="bevel res",
+                default=2,
+                min=0, soft_min=0,
+                max=4, soft_max=4)
+    geo_extrude = FloatProperty(name="extrude",
+                default=0.0,
+                min=0, soft_min=0)
+    geo_width = FloatProperty(name="width",
+                default=1.0,
+                min=0, soft_min=0)
+    geo_res = IntProperty(name="resolution",
+                default=12,
+                min=1, soft_min=1)
+
+    #### Curve Options
+    shapeItems = [
+                ('3D', '3D', '3D'),
+                ('2D', '2D', '2D')]
+    shape = EnumProperty(name="2D / 3D",
+                items=shapeItems,
+                description="2D or 3D Curve")
+    cyclic_u = BoolProperty(name="Cyclic",
+                default=True,
+                description="make curve closed")
+    endp_u = BoolProperty(name="endpoint_u",
+                default=True,
+                description="stretch to endpoints")
+    order_u = IntProperty(name="order_u",
+                default=4,
+                min=2, soft_min=2,
+                max=6, soft_max=6,
+                description="Order of nurbs spline")
+    bezHandles = [
+                ('VECTOR', 'Vector', 'VECTOR'),
+                ('AUTOMATIC', 'Auto', 'AUTOMATIC')]
+    handleType = EnumProperty(name="Handle type",
+                description="bezier handles type",
+                items=bezHandles)
+
+    #### Parameters
+    torus_res = IntProperty(name="Resoulution",
+                default=200,
+                min=3, soft_min=3,
+                description='Resolution')
+    torus_p = IntProperty(name="p",
+                default=2,
+                min=1, soft_min=1,
+                #max=1, soft_max=1,
+                description="p")
+    torus_q = IntProperty(name="q",
+                default=3,
+                min=1, soft_min=1,
+                #max=1, soft_max=1,
+                description="q")

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list