[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3383] contrib/py/scripts/addons/ mesh_looptools.py: Version 4
Bart Crouch
bartius.crouch at gmail.com
Mon May 21 23:46:11 CEST 2012
Revision: 3383
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3383
Author: crouch
Date: 2012-05-21 21:46:10 +0000 (Mon, 21 May 2012)
Log Message:
-----------
Version 4
- Works with BMesh
- Fixed and re-enabled bridge and loft
- Removed workaround for bridge selection states (abusing bevel-weights)
Modified Paths:
--------------
contrib/py/scripts/addons/mesh_looptools.py
Modified: contrib/py/scripts/addons/mesh_looptools.py
===================================================================
--- contrib/py/scripts/addons/mesh_looptools.py 2012-05-21 20:47:02 UTC (rev 3382)
+++ contrib/py/scripts/addons/mesh_looptools.py 2012-05-21 21:46:10 UTC (rev 3383)
@@ -19,19 +19,21 @@
bl_info = {
'name': "LoopTools",
'author': "Bart Crouch",
- 'version': (3, 2, 4),
- 'blender': (2, 6, 2),
+ 'version': (4, 0, 0),
+ 'blender': (2, 6, 3),
'location': "View3D > Toolbar and View3D > Specials (W-key)",
- 'warning': "Bridge & Loft functions removed",
+ 'warning': "",
'description': "Mesh modelling toolkit. Several tools to aid modelling",
- 'wiki_url': "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
+ 'wiki_url': "http://wiki.blender.org/index.php/Extensions:2.6/Py/"\
"Scripts/Modeling/LoopTools",
'tracker_url': "http://projects.blender.org/tracker/index.php?"\
"func=detail&aid=26189",
'category': 'Mesh'}
+import bmesh
import bpy
+import collections
import mathutils
import math
@@ -52,7 +54,7 @@
# check cache for stored information
-def cache_read(tool, object, mesh, input_method, boundaries):
+def cache_read(tool, object, bm, input_method, boundaries):
# current tool not cached yet
if tool not in looptools_cache:
return(False, False, False, False, False)
@@ -68,7 +70,7 @@
and mod.type == 'MIRROR']
if modifiers != looptools_cache[tool]["modifiers"]:
return(False, False, False, False, False)
- input = [v.index for v in mesh.vertices if v.select and not v.hide]
+ input = [v.index for v in bm.verts if v.select and not v.hide]
if input != looptools_cache[tool]["input"]:
return(False, False, False, False, False)
# reading values
@@ -81,13 +83,13 @@
# store information in the cache
-def cache_write(tool, object, mesh, input_method, boundaries, single_loops,
+def cache_write(tool, object, bm, input_method, boundaries, single_loops,
loops, derived, mapping):
# clear cache of current tool
if tool in looptools_cache:
del looptools_cache[tool]
# prepare values to be saved to cache
- input = [v.index for v in mesh.vertices if v.select and not v.hide]
+ input = [v.index for v in bm.verts if v.select and not v.hide]
modifiers = [mod.name for mod in object.modifiers if mod.show_viewport \
and mod.type == 'MIRROR']
# update cache
@@ -98,7 +100,7 @@
# calculates natural cubic splines through all given knots
-def calculate_cubic_splines(mesh_mod, tknots, knots):
+def calculate_cubic_splines(bm_mod, tknots, knots):
# hack for circular loops
if knots[0] == knots[-1] and len(knots) > 1:
circular = True
@@ -142,7 +144,7 @@
if n < 2:
return False
x = tknots[:]
- locs = [mesh_mod.vertices[k].co[:] for k in knots]
+ locs = [bm_mod.verts[k].co[:] for k in knots]
result = []
for j in range(3):
a = []
@@ -189,11 +191,11 @@
# calculates linear splines through all given knots
-def calculate_linear_splines(mesh_mod, tknots, knots):
+def calculate_linear_splines(bm_mod, tknots, knots):
splines = []
for i in range(len(knots)-1):
- a = mesh_mod.vertices[knots[i]].co
- b = mesh_mod.vertices[knots[i+1]].co
+ a = bm_mod.verts[knots[i]].co
+ b = bm_mod.verts[knots[i+1]].co
d = b-a
t = tknots[i]
u = tknots[i+1]-t
@@ -203,9 +205,9 @@
# calculate a best-fit plane to the given vertices
-def calculate_plane(mesh_mod, loop, method="best_fit", object=False):
+def calculate_plane(bm_mod, loop, method="best_fit", object=False):
# getting the vertex locations
- locs = [mesh_mod.vertices[v].co.copy() for v in loop[0]]
+ locs = [bm_mod.verts[v].co.copy() for v in loop[0]]
# calculating the center of masss
com = mathutils.Vector()
@@ -260,7 +262,7 @@
elif method == 'normal':
# averaging the vertex normals
- v_normals = [mesh_mod.vertices[v].normal for v in loop[0]]
+ v_normals = [bm_mod.verts[v].normal for v in loop[0]]
normal = mathutils.Vector()
for v_normal in v_normals:
normal += v_normal
@@ -280,17 +282,17 @@
# calculate splines based on given interpolation method (controller function)
-def calculate_splines(interpolation, mesh_mod, tknots, knots):
+def calculate_splines(interpolation, bm_mod, tknots, knots):
if interpolation == 'cubic':
- splines = calculate_cubic_splines(mesh_mod, tknots, knots[:])
+ splines = calculate_cubic_splines(bm_mod, tknots, knots[:])
else: # interpolations == 'linear'
- splines = calculate_linear_splines(mesh_mod, tknots, knots[:])
+ splines = calculate_linear_splines(bm_mod, tknots, knots[:])
return(splines)
# check loops and only return valid ones
-def check_loops(loops, mapping, mesh_mod):
+def check_loops(loops, mapping, bm_mod):
valid_loops = []
for loop, circular in loops:
# loop needs to have at least 3 vertices
@@ -308,8 +310,8 @@
# vertices can not all be at the same location
stacked = True
for i in range(len(loop) - 1):
- if (mesh_mod.vertices[loop[i]].co - \
- mesh_mod.vertices[loop[i+1]].co).length > 1e-6:
+ if (bm_mod.verts[loop[i]].co - \
+ bm_mod.verts[loop[i+1]].co).length > 1e-6:
stacked = False
break
if stacked:
@@ -320,28 +322,30 @@
return(valid_loops)
-# input: mesh, output: dict with the edge-key as key and face-index as value
-def dict_edge_faces(mesh):
- edge_faces = dict([[edge.key, []] for edge in mesh.edges if not edge.hide])
- for face in mesh.tessfaces:
+# input: bmesh, output: dict with the edge-key as key and face-index as value
+def dict_edge_faces(bm):
+ edge_faces = dict([[edgekey(edge), []] for edge in bm.edges if \
+ not edge.hide])
+ for face in bm.faces:
if face.hide:
continue
- for key in face.edge_keys:
+ for key in face_edgekeys(face):
edge_faces[key].append(face.index)
return(edge_faces)
-# input: mesh (edge-faces optional), output: dict with face-face connections
-def dict_face_faces(mesh, edge_faces=False):
+
+# input: bmesh (edge-faces optional), output: dict with face-face connections
+def dict_face_faces(bm, edge_faces=False):
if not edge_faces:
- edge_faces = dict_edge_faces(mesh)
+ edge_faces = dict_edge_faces(bm)
- connected_faces = dict([[face.index, []] for face in mesh.tessfaces if \
+ connected_faces = dict([[face.index, []] for face in bm.faces if \
not face.hide])
- for face in mesh.tessfaces:
+ for face in bm.faces:
if face.hide:
continue
- for edge_key in face.edge_keys:
+ for edge_key in face_edgekeys(face):
for connected_face in edge_faces[edge_key]:
if connected_face == face.index:
continue
@@ -350,25 +354,26 @@
return(connected_faces)
-# input: mesh, output: dict with the vert index as key and edge-keys as value
-def dict_vert_edges(mesh):
- vert_edges = dict([[v.index, []] for v in mesh.vertices if not v.hide])
- for edge in mesh.edges:
+# input: bmesh, output: dict with the vert index as key and edge-keys as value
+def dict_vert_edges(bm):
+ vert_edges = dict([[v.index, []] for v in bm.verts if not v.hide])
+ for edge in bm.edges:
if edge.hide:
continue
- for vert in edge.key:
- vert_edges[vert].append(edge.key)
+ ek = edgekey(edge)
+ for vert in ek:
+ vert_edges[vert].append(ek)
return(vert_edges)
-# input: mesh, output: dict with the vert index as key and face index as value
-def dict_vert_faces(mesh):
- vert_faces = dict([[v.index, []] for v in mesh.vertices if not v.hide])
- for face in mesh.tessfaces:
+# input: bmesh, output: dict with the vert index as key and face index as value
+def dict_vert_faces(bm):
+ vert_faces = dict([[v.index, []] for v in bm.verts if not v.hide])
+ for face in bm.faces:
if not face.hide:
- for vert in face.vertices:
- vert_faces[vert].append(face.index)
+ for vert in face.verts:
+ vert_faces[vert.index].append(face.index)
return(vert_faces)
@@ -387,23 +392,34 @@
return(vert_verts)
+# return the edgekey ([v1.index, v2.index]) of a bmesh edge
+def edgekey(edge):
+ return((edge.verts[0].index, edge.verts[1].index))
+
+
+# returns the edgekeys of a bmesh face
+def face_edgekeys(face):
+ return([(edge.verts[0].index, edge.verts[1].index) for \
+ edge in face.edges])
+
+
# calculate input loops
-def get_connected_input(object, mesh, scene, input):
+def get_connected_input(object, bm, scene, input):
# get mesh with modifiers applied
- derived, mesh_mod = get_derived_mesh(object, mesh, scene)
+ derived, bm_mod = get_derived_bmesh(object, bm, scene)
# calculate selected loops
- edge_keys = [edge.key for edge in mesh_mod.edges if \
+ edge_keys = [edgekey(edge) for edge in bm_mod.edges if \
edge.select and not edge.hide]
loops = get_connected_selections(edge_keys)
# if only selected loops are needed, we're done
if input == 'selected':
- return(derived, mesh_mod, loops)
+ return(derived, bm_mod, loops)
# elif input == 'all':
- loops = get_parallel_loops(mesh_mod, loops)
+ loops = get_parallel_loops(bm_mod, loops)
- return(derived, mesh_mod, loops)
+ return(derived, bm_mod, loops)
# sorts all edge-keys into a list of loops
@@ -478,7 +494,7 @@
# get the derived mesh data, if there is a mirror modifier
-def get_derived_mesh(object, mesh, scene):
+def get_derived_bmesh(object, bm, scene):
# check for mirror modifiers
if 'MIRROR' in [mod.type for mod in object.modifiers if mod.show_viewport]:
derived = True
@@ -489,32 +505,35 @@
if mod.type != 'MIRROR':
mod.show_viewport = False
# get derived mesh
+ bm_mod = bmesh.new()
mesh_mod = object.to_mesh(scene, True, 'PREVIEW')
+ bm_mod.from_mesh(mesh_mod)
+ bpy.context.blend_data.meshes.remove(mesh_mod)
# re-enable other modifiers
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list