[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3226] contrib/py/scripts/addons/ add_curve_objects: removing panels, some cleanup

Brendon Murphy meta.androcto1 at gmail.com
Fri Apr 6 08:45:10 CEST 2012


Revision: 3226
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3226
Author:   meta-androcto
Date:     2012-04-06 06:45:10 +0000 (Fri, 06 Apr 2012)
Log Message:
-----------
removing panels, some cleanup
added curve_simplify.py to this menu as it adds a curve object & the menu provides easy access to this hard to find tool.

Modified Paths:
--------------
    contrib/py/scripts/addons/add_curve_objects/__init__.py
    contrib/py/scripts/addons/add_curve_objects/cotejrp1_particle_tracer.py
    contrib/py/scripts/addons/add_curve_objects/cotejrp1_string_it.py

Added Paths:
-----------
    contrib/py/scripts/addons/add_curve_objects/curve_simplify.py

Modified: contrib/py/scripts/addons/add_curve_objects/__init__.py
===================================================================
--- contrib/py/scripts/addons/add_curve_objects/__init__.py	2012-04-06 05:55:27 UTC (rev 3225)
+++ contrib/py/scripts/addons/add_curve_objects/__init__.py	2012-04-06 06:45:10 UTC (rev 3226)
@@ -37,12 +37,14 @@
     imp.reload(add_curve_spirals)
     imp.reload(cotejrp1_particle_tracer)
     imp.reload(cotejrp1_string_it)
-
+    imp.reload(curve_simplify)
+	
 else:
     from . import add_curve_rectangle_259
     from . import add_curve_spirals
     from . import cotejrp1_particle_tracer
     from . import cotejrp1_string_it
+    from . import curve_simplify
 
 import bpy
 
@@ -63,8 +65,9 @@
             text="Particle Tracer")
         layout.operator("curve.string_it_operator",
             text="String It")
+        layout.operator("curve.simplify",
+            text="Curve Simplify")
 
-
 # Register all operators and panels
 
 # Define "Extras" menu

Modified: contrib/py/scripts/addons/add_curve_objects/cotejrp1_particle_tracer.py
===================================================================
--- contrib/py/scripts/addons/add_curve_objects/cotejrp1_particle_tracer.py	2012-04-06 05:55:27 UTC (rev 3225)
+++ contrib/py/scripts/addons/add_curve_objects/cotejrp1_particle_tracer.py	2012-04-06 06:45:10 UTC (rev 3226)
@@ -33,9 +33,6 @@
 # advanced the timeline.  If you don't, the current frame changes on you 
 # when you change the particle tracer options.
 '''
-import bpy
-
-
 bl_info = {
     'name': 'Particle Tracer',
     'author': 'Phil Cote, cotejrp1, (http://www.blenderaddons.com)',
@@ -47,12 +44,8 @@
     'warning': '', # used for warning icon and text in addons panel
     'category': 'Add Curve'}
 
-
 import bpy
 
-
-    
-
 def getParticleSys( ob ):
     """
     Grab the first particle system available or None if there aren't any.
@@ -144,33 +137,18 @@
         for point in spline.bezier_points:
             point.handle_left_type = "AUTO"
             point.handle_right_type = "AUTO"
-        
-        
+       
         scn = context.scene
         crvob = bpy.data.objects.new( self.curveName, crv )
         scn.objects.link( crvob )
         
         return {'FINISHED'}
-
-class PTracerPanel( bpy.types.Panel ):
     
-    bl_label = "Particle Tracer"
-    bl_region_type = "TOOLS"
-    bl_space_type = "VIEW_3D"
-    
-    def draw( self, context ):
-        layout = self.layout
-        layout.row().operator( "curve.particle_tracer", text="Make Curve" )
-    
 def register():
     bpy.utils.register_class(PTracerOp)
-    bpy.utils.register_class(PTracerPanel)
 
-
 def unregister():
     bpy.utils.unregister_class(PTracerOp)
-    bpy.utils.unregister_class(PTracerPanel)
 
-    
 if __name__ == "__main__":
     register()

Modified: contrib/py/scripts/addons/add_curve_objects/cotejrp1_string_it.py
===================================================================
--- contrib/py/scripts/addons/add_curve_objects/cotejrp1_string_it.py	2012-04-06 05:55:27 UTC (rev 3225)
+++ contrib/py/scripts/addons/add_curve_objects/cotejrp1_string_it.py	2012-04-06 06:45:10 UTC (rev 3226)
@@ -1,3 +1,4 @@
+'''
 # string_it.py (c) 2011 Phil Cote (cotejrp1)
 #
 # ***** BEGIN GPL LICENSE BLOCK *****
@@ -19,8 +20,6 @@
 #
 # ***** END GPL LICENCE BLOCK *****
 
-import bpy
-
 bl_info = {
     'name': 'String It',
     'author': 'Phil Cote, cotejrp1, (http://www.blenderpythontutorials.com)',
@@ -31,8 +30,9 @@
     'description': 'Run a curve through each selected object in a scene.',
     'warning': '', # used for warning icon and text in addons panel
     'category': 'Add Curve'}
+'''
+import bpy
 
-
 def makeBezier( spline, vertList ):
     numPoints = ( len( vertList ) / 3 ) - 1
     spline.bezier_points.add( numPoints )
@@ -94,25 +94,11 @@
         scn.objects.link( crvOb )            
         return {'FINISHED'}
 
-
-class StringItPanel( bpy.types.Panel ):
-    bl_label = "String It"
-    bl_region_type = "TOOLS"
-    bl_space_type = "VIEW_3D"
-    
-    def draw( self, context ):
-        self.layout.row().operator( "curve.string_it_operator", text="Make Curve" )
-    
-    
 def register():
     bpy.utils.register_class(StringItOperator)
-    bpy.utils.register_class(StringItPanel)
 
-
 def unregister():
     bpy.utils.unregister_class(StringItOperator)
-    bpy.utils.unregister_class(StringItPanel)
 
-
 if __name__ == "__main__":
     register()

Added: contrib/py/scripts/addons/add_curve_objects/curve_simplify.py
===================================================================
--- contrib/py/scripts/addons/add_curve_objects/curve_simplify.py	                        (rev 0)
+++ contrib/py/scripts/addons/add_curve_objects/curve_simplify.py	2012-04-06 06:45:10 UTC (rev 3226)
@@ -0,0 +1,593 @@
+# ##### 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_info = {
+    "name": "Simplify curves",
+    "author": "testscreenings",
+    "version": (1,),
+    "blender": (2, 5, 9),
+    "location": "Search > Simplify Curves",
+    "description": "Simplifies 3D curves and fcurves",
+    "warning": "",
+    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
+        "Scripts/Curve/Curve_Simplify",
+    "tracker_url": "https://projects.blender.org/tracker/index.php?"\
+        "func=detail&aid=22327",
+    "category": "Add Curve"}
+
+"""
+This script simplifies Curves.
+"""
+
+####################################################
+import bpy
+from bpy.props import *
+import mathutils
+import math
+
+##############################
+#### simplipoly algorithm ####
+##############################
+# get SplineVertIndices to keep
+def simplypoly(splineVerts, options):
+    # main vars
+    newVerts = [] # list of vertindices to keep
+    points = splineVerts # list of 3dVectors
+    pointCurva = [] # table with curvatures
+    curvatures = [] # averaged curvatures per vert
+    for p in points:
+        pointCurva.append([])
+    order = options[3] # order of sliding beziercurves
+    k_thresh = options[2] # curvature threshold
+    dis_error = options[6] # additional distance error
+
+    # get curvatures per vert
+    for i, point in enumerate(points[:-(order-1)]):
+        BVerts = points[i:i+order]
+        for b, BVert in enumerate(BVerts[1:-1]):
+            deriv1 = getDerivative(BVerts, 1/(order-1), order-1)
+            deriv2 = getDerivative(BVerts, 1/(order-1), order-2)
+            curva = getCurvature(deriv1, deriv2)
+            pointCurva[i+b+1].append(curva)
+
+    # average the curvatures
+    for i in range(len(points)):
+        avgCurva = sum(pointCurva[i]) / (order-1)
+        curvatures.append(avgCurva)
+
+    # get distancevalues per vert - same as Ramer-Douglas-Peucker
+    # but for every vert
+    distances = [0.0] #first vert is always kept
+    for i, point in enumerate(points[1:-1]):
+        dist = altitude(points[i], points[i+2], points[i+1])
+        distances.append(dist)
+    distances.append(0.0) # last vert is always kept
+
+    # generate list of vertindices to keep
+    # tested against averaged curvatures and distances of neighbour verts
+    newVerts.append(0) # first vert is always kept
+    for i, curv in enumerate(curvatures):
+        if (curv >= k_thresh*0.01
+        or distances[i] >= dis_error*0.1):
+            newVerts.append(i)
+    newVerts.append(len(curvatures)-1) # last vert is always kept
+
+    return newVerts
+
+# get binomial coefficient
+def binom(n, m):
+    b = [0] * (n+1)
+    b[0] = 1
+    for i in range(1, n+1):
+        b[i] = 1
+        j = i-1
+        while j > 0:
+            b[j] += b[j-1]
+            j-= 1
+    return b[m]
+
+# get nth derivative of order(len(verts)) bezier curve
+def getDerivative(verts, t, nth):
+    order = len(verts) - 1 - nth
+    QVerts = []
+
+    if nth:
+        for i in range(nth):
+            if QVerts:
+                verts = QVerts
+            derivVerts = []
+            for i in range(len(verts)-1):
+                derivVerts.append(verts[i+1] - verts[i])
+            QVerts = derivVerts
+    else:
+        QVerts = verts
+
+    if len(verts[0]) == 3:
+        point = mathutils.Vector((0, 0, 0))
+    if len(verts[0]) == 2:
+        point = mathutils.Vector((0, 0))
+
+    for i, vert in enumerate(QVerts):
+        point += binom(order, i) * math.pow(t, i) * math.pow(1-t, order-i) * vert
+    deriv = point
+
+    return deriv
+
+# get curvature from first, second derivative
+def getCurvature(deriv1, deriv2):
+    if deriv1.length == 0: # in case of points in straight line
+        curvature = 0
+        return curvature
+    curvature = (deriv1.cross(deriv2)).length / math.pow(deriv1.length, 3)
+    return curvature
+
+#########################################
+#### Ramer-Douglas-Peucker algorithm ####
+#########################################
+# get altitude of vert
+def altitude(point1, point2, pointn):
+    edge1 = point2 - point1
+    edge2 = pointn - point1
+    if edge2.length == 0:
+        altitude = 0
+        return altitude
+    if edge1.length == 0:
+        altitude = edge2.length
+        return altitude
+    alpha = edge1.angle(edge2)
+    altitude = math.sin(alpha) * edge2.length
+    return altitude
+
+# iterate through verts
+def iterate(points, newVerts, error):
+    new = []
+    for newIndex in range(len(newVerts)-1):
+        bigVert = 0
+        alti_store = 0
+        for i, point in enumerate(points[newVerts[newIndex]+1:newVerts[newIndex+1]]):
+            alti = altitude(points[newVerts[newIndex]], points[newVerts[newIndex+1]], point)
+            if alti > alti_store:
+                alti_store = alti
+                if alti_store >= error:
+                    bigVert = i+1+newVerts[newIndex]
+        if bigVert:
+            new.append(bigVert)

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list