[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [588] trunk/py/scripts/addons/ add_mesh_gears.py: * Version 2.2 - Reformatted and made PEP8 compatible
Martin Buerbaum
martin.buerbaum at gmx.at
Thu Apr 15 21:17:37 CEST 2010
Revision: 588
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=588
Author: pontiac
Date: 2010-04-15 21:17:37 +0200 (Thu, 15 Apr 2010)
Log Message:
-----------
* Version 2.2 - Reformatted and made PEP8 compatible
* Added _lots_ of "telling" variable names ... far from done :-/
* Support "recall" operator.
* @todo: This script definitly needs some comments on what's going on.
* @todo: Lots of hardcoded stuff (also deepcopy) - is there a better way?
* @todo: Do not create "double" vertices in the first place.
* @todo: Is it really a good idea to mix gears & worm in the same operator?
Modified Paths:
--------------
trunk/py/scripts/addons/add_mesh_gears.py
Modified: trunk/py/scripts/addons/add_mesh_gears.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_gears.py 2010-04-15 13:18:47 UTC (rev 587)
+++ trunk/py/scripts/addons/add_mesh_gears.py 2010-04-15 19:17:37 UTC (rev 588)
@@ -1,195 +1,355 @@
# add_mesh_gear.py (c) 2009, 2010 Michel J. Anders (varkenvarken)
#
-# add gears/cogwheels to the blender 2.50 add->mesh menu
+# ***** BEGIN GPL LICENSE BLOCK *****
#
-# tested with the official blender 2.50 alpha 0 32-bit windows
-# also tested with trunk svn 26208 on 32-bit windows
#
-# ##### 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 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.
#
-# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
-# 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 #####
+# ***** END GPL LICENCE BLOCK *****
bl_addon_info = {
'name': 'Add Mesh: Gears',
'author': 'varkenvarken',
- 'version': '2.1',
+ 'version': '2.2',
'blender': (2, 5, 3),
'location': 'View3D > Add > Mesh ',
'description': 'Adds a mesh Gear to the Add Mesh menu',
'url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
- 'Scripts/Add_Mesh/Add_Gear',
+ 'Scripts/Add_Mesh/Add_Gear',
'category': 'Add Mesh'}
"""
What was needed to port it from 2.49 -> 2.50 alpha 0?
-The basic functions that calculate the geometry (verts and faces) are unchanged
-( add_tooth(), add_spoke2(), add_gear() )
+The basic functions that calculate the geometry (verts and faces) are mostly
+unchanged (add_tooth, add_spoke2, add_gear)
-These functions were designed to return lists of tuples (x,y,z) (for the vertices) and
-lists of lists [i,k,l,m] (for the faces). Because the Blender 2.50 API does not provide
-facilties to alter individual elements of the the verts and faces attributes of a mesh
-directly we have to add the calculated vertices and faces in bulk by using the
-mesh.add_geometry(nverts,nedges,nfaces) methodfolowed by
-mesh.verts.foreach_set("co", verts_loc) and mesh.faces.foreach_set("verts_raw", faces).
-
-Both the foreach_set() methods take flattened lists as arguments, not lists of tuples, so we
-added a simple function to flatten a list of lists or tuples.
-
-Also, the vertex group API is changed a little bit but the concepts are the same:
-vertexgroup = ob.add_vertex_group('NAME_OF_VERTEXGROUP') # add a vertex group
+Also, the vertex group API is changed a little bit but the concepts
+are the same:
+=========
+vertexgroup = ob.add_vertex_group('NAME_OF_VERTEXGROUP')
for i in vertexgroup_vertex_indices:
ob.add_vertex_to_group(i, vertexgroup, weight, 'ADD')
+=========
Now for some reason the name does not 'stick' and we have to set it this way:
vertexgroup.name = 'NAME_OF_VERTEXGROUP'
-Conversion to 2.50 also meant we could simply do away with our crude user interface.
-Just definining the appropriate properties in the AddGear() operator will display the
-properties in the Blender GUI with the added benefit of making it interactive: changing
-a property will redo the AddGear() operator providing the user with instant feedback.
+Conversion to 2.50 also meant we could simply do away with our crude user
+interface.
+Just definining the appropriate properties in the AddGear() operator will
+display the properties in the Blender GUI with the added benefit of making
+it interactive: changing a property will redo the AddGear() operator providing
+the user with instant feedback.
-FInally we had to convert/throw away some print statements to print functions as Blender
-nows uses Python 3.x
+Finally we had to convert/throw away some print statements to print functions
+as Blender nows uses Python 3.x
-The most puzzling issue was that the built in Python zip() function changed its behavior.
-In 3.x it returns a zip object (that can be iterated over) instead of a list of tuples. This meant
-we could no longer use deepcopy(zip(...)) but had to convert the zip object to a list of tuples
-first.
+The most puzzling issue was that the built in Python zip()
+function changed its behavior.
+In 3.x it returns a zip object (that can be iterated over)
+instead of a list of tuples.
+This meant we could no longer use deepcopy(zip(...)) but had to convert the
+zip object to a list of tuples first.
-The code to actually implement the AddGear() function is mostly copied from add_mesh_torus()
-(distributed with Blender).
+The code to actually implement the AddGear() function is mostly copied from
+add_mesh_torus() (distributed with Blender).
Unresolved issues:
-- removing doubles:
- the code that produces the teeth of the gears produces some duplicate vertices. The original
- script just called remove_doubles() but if we do that in 2.50 we have a problem. To apply
- the bpy.ops.mesh.remove_doubles() operator we have to change to edit mode. The moment
- we do that we loose to possibilty to interactively change the properties. Also changing back
- to object mode raises a strange exception (to investigate). So for now, removing doubles is left
- to the user once satisfied with the chosen setting for a gear,
+- Removing doubles:
+ The algorithm should not generate a full section to begin with.
+ At least the last 4 vertices don't need to be created, because the
+ next section will have them as their first 4 vertices anyway.
+ OLD COMMENT ON DOUBLES:
+ "The code that produces the teeth of the gears produces some duplicate
+ vertices. The original script just called remove_doubles() but if we
+ do that in 2.50 we have a problem.
+ To apply the bpy.ops.mesh.remove_doubles() operator we have to change
+ to edit mode. The moment we do that we loose to possibilty to
+ interactively change the properties.
+ Also changing back to object mode raises a strange exception (to
+ investigate). So for now, removing doubles is left to the user
+ once satisfied with the chosen setting for a gear."
-- no suitable icon:
- a rather minor point but I reused the torus icon for the add->mesh->gear menu entry as there
- doesn't seem to be a generic mesh icon or a way to add custom icons. Too bad, but as this is
- just eye candy it's no big deal.
-
+- No suitable icon:
+ A rather minor point but I reused the torus icon for the add->mesh->gear
+ menu entry as there doesn't seem to be a generic mesh icon or a way to
+ add custom icons. Too bad, but as this is just eye candy it's no big deal.
"""
import bpy
import mathutils
-from math import cos, sin, tan, atan, asin, pi,radians as rad
-from copy import deepcopy as dc
+from math import *
+from copy import deepcopy
+from bpy.props import *
-def flatten(alist):
- "Flatten a list of lists or tuples."
- return sum([list(a) for a in alist],[])
+# Constants
+FACES = [
+ [0, 5, 6, 1],
+ [1, 6, 7, 2],
+ [2, 7, 8, 3],
+ [3, 8, 9, 4],
+ [6, 10, 11, 7],
+ [7, 11, 12, 8],
+ [10, 13, 14, 11],
+ [11, 14, 15, 12]]
-#constants
-faces=[[0, 5, 6, 1],
- [1, 6, 7, 2],
- [2, 7, 8, 3],
- [3, 8, 9, 4],
- [6, 10, 11, 7],
- [7, 11, 12, 8],
- [10, 13, 14, 11],
- [11, 14, 15, 12]]
+VERT_NUM = 16 # Number of vertices
-L = 16 # number of vertices
-#edgefaces
-ef = [5, 6, 10, 13, 14, 15, 12, 8, 9]
-ef2 = [i + L for i in ef]
-# in python 3, zip() returns a zip object so we have to force the result into a list of lists to keep
-# deepcopy happy later on in the script.
-efc = [[i, j, k, l] for i, j, k, l in zip(ef[:-1], ef2[:-1], ef2[1:], ef[1:])]
-vv = [5, 6, 8, 9, 21,22, 24, 25] #vertices in a valley
-tv = [13, 14, 15, 29, 30, 31] #vertices on a tooth
+# Edgefaces
+EDGEFACES = [5, 6, 10, 13, 14, 15, 12, 8, 9]
+EDGEFACES2 = [i + VERT_NUM for i in EDGEFACES] # i.e. Indices are offset by 16
-spokefaces=((0, 1, 2, 5),
- (2, 3, 4, 7),
- (5, 2, 7, 6),
- (5, 6, 9, 8),
- (6, 7, 10, 9),
- (11, 8, 13, 12),
- (8, 9, 10, 13),
- (13, 10, 15, 14))
+# In python 3, zip() returns a zip object so we have to force the
+# result into a list of lists to keep deepcopy happy later on in the script.
+EFC = [[i, j, k, l] for i, j, k, l
+ in zip(EDGEFACES[:-1], EDGEFACES2[:-1], EDGEFACES2[1:], EDGEFACES[1:])]
+VERTS_TOOTH = [13, 14, 15, 29, 30, 31] # Vertices on a tooth
+VERTS_VALLEY = [5, 6, 8, 9, 21, 22, 24, 25] # Vertices in a valley
+#SPOKEFACES = (
+# (0, 1, 2, 5),
+# (2, 3, 4, 7),
+# (5, 2, 7, 6),
+# (5, 6, 9, 8),
+# (6, 7, 10, 9),
+# (11, 8, 13, 12),
+# (8, 9, 10, 13),
+# (13, 10, 15, 14))
-def add_tooth(a, t, d, r, Ad, De, b, p, rack=0, crown=0.0):
+
+# Stores the values of a list of properties and the
+# operator id in a property group ('recall_op') inside the object.
+# Could (in theory) be used for non-objects.
+# Note: Replaces any existing property group with the same name!
+# ob ... Object to store the properties in.
+# op ... The operator that should be used.
+# op_args ... A dictionary with valid Blender
+# properties (operator arguments/parameters).
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list