Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'release/scripts/modules/bpy_extras/mesh_utils.py')
-rw-r--r--release/scripts/modules/bpy_extras/mesh_utils.py131
1 files changed, 103 insertions, 28 deletions
diff --git a/release/scripts/modules/bpy_extras/mesh_utils.py b/release/scripts/modules/bpy_extras/mesh_utils.py
index b6d8a1fcf16..7bc6dae3cc6 100644
--- a/release/scripts/modules/bpy_extras/mesh_utils.py
+++ b/release/scripts/modules/bpy_extras/mesh_utils.py
@@ -16,7 +16,7 @@
#
# ##### END GPL LICENSE BLOCK #####
-# <pep8 compliant>
+# <pep8-80 compliant>
__all__ = (
"mesh_linked_faces",
@@ -25,7 +25,9 @@ __all__ = (
"edge_loops_from_faces",
"edge_loops_from_edges",
"ngon_tesselate",
-)
+ "face_random_points",
+ )
+
def mesh_linked_faces(mesh):
"""
@@ -33,7 +35,7 @@ def mesh_linked_faces(mesh):
other mesh elements within 1 mesh datablock.
:arg mesh: the mesh used to group with.
- :type mesh: :class:`Mesh`
+ :type mesh: :class:`bpy.types.Mesh`
:return: lists of lists containing faces.
:rtype: list
"""
@@ -66,7 +68,8 @@ def mesh_linked_faces(mesh):
if mapped_index != nxt_mapped_index:
ok = True
- # Assign mapping to this group so they all map to this group
+ # Assign mapping to this group so they
+ # all map to this group
for grp_f in face_groups[nxt_mapped_index]:
face_mapping[grp_f.index] = mapped_index
@@ -104,9 +107,9 @@ def edge_face_count(mesh):
:return: list face users for each item in mesh.edges.
:rtype: list
"""
- edge_face_count_dict = edge_face_count_dict(mesh)
+ edge_face_count = edge_face_count_dict(mesh)
get = dict.get
- return [get(edge_face_count_dict, ed.key, 0) for ed in mesh.edges]
+ return [get(edge_face_count, ed.key, 0) for ed in mesh.edges]
def edge_loops_from_faces(mesh, faces=None, seams=()):
@@ -122,9 +125,9 @@ def edge_loops_from_faces(mesh, faces=None, seams=()):
[[(0, 1), (4, 8), (3, 8)], ...]
:arg mesh: the mesh used to get edge loops from.
- :type mesh: :class:`Mesh`
+ :type mesh: :class:`bpy.types.Mesh`
:arg faces: optional face list to only use some of the meshes faces.
- :type faces: :class:`MeshFaces`, sequence or or NoneType
+ :type faces: :class:`bpy.types.MeshFaces`, sequence or or NoneType
:return: return a list of edge vertex index lists.
:rtype: list
"""
@@ -167,8 +170,8 @@ def edge_loops_from_faces(mesh, faces=None, seams=()):
# from knowing the last 2, look for th next.
ed_adj = edges[context_loop[-1]]
if len(ed_adj) != 2:
-
- if other_dir and flipped == False: # the original edge had 2 other edges
+ # the original edge had 2 other edges
+ if other_dir and flipped == False:
flipped = True # only flip the list once
context_loop.reverse()
ed_adj[:] = []
@@ -211,8 +214,6 @@ def edge_loops_from_edges(mesh, edges=None):
if not hasattr(edges, "pop"):
edges = edges[:]
- edge_dict = {ed.key: ed for ed in mesh.edges if ed.select}
-
while edges:
current_edge = edges.pop()
vert_end, vert_start = current_edge.vertices[:]
@@ -258,15 +259,18 @@ def edge_loops_from_edges(mesh, edges=None):
def ngon_tesselate(from_data, indices, fix_loops=True):
'''
- Takes a polyline of indices (fgon)
- and returns a list of face indicie lists.
- Designed to be used for importers that need indices for an fgon to create from existing verts.
+ Takes a polyline of indices (fgon) and returns a list of face
+ indicie lists. Designed to be used for importers that need indices for an
+ fgon to create from existing verts.
from_data: either a mesh, or a list/tuple of vectors.
- indices: a list of indices to use this list is the ordered closed polyline to fill, and can be a subset of the data given.
- fix_loops: If this is enabled polylines that use loops to make multiple polylines are delt with correctly.
+ indices: a list of indices to use this list is the ordered closed polyline
+ to fill, and can be a subset of the data given.
+ fix_loops: If this is enabled polylines that use loops to make multiple
+ polylines are delt with correctly.
'''
+ from mathutils.geometry import tesselate_polygon
from mathutils import Vector
vector_to_tuple = Vector.to_tuple
@@ -274,7 +278,8 @@ def ngon_tesselate(from_data, indices, fix_loops=True):
return []
def mlen(co):
- return abs(co[0]) + abs(co[1]) + abs(co[2]) # manhatten length of a vector, faster then length
+ # manhatten length of a vector, faster then length
+ return abs(co[0]) + abs(co[1]) + abs(co[2])
def vert_treplet(v, i):
return v, vector_to_tuple(v, 6), i, mlen(v)
@@ -285,31 +290,34 @@ def ngon_tesselate(from_data, indices, fix_loops=True):
else:
return v1[1], v2[1]
- if not PREF_FIX_LOOPS:
+ if not fix_loops:
'''
Normal single concave loop filling
'''
- if type(from_data) in (tuple, list):
+ if type(from_data) in {tuple, list}:
verts = [Vector(from_data[i]) for ii, i in enumerate(indices)]
else:
verts = [from_data.vertices[i].co for ii, i in enumerate(indices)]
- for i in range(len(verts) - 1, 0, -1): # same as reversed(xrange(1, len(verts))):
+ # same as reversed(range(1, len(verts))):
+ for i in range(len(verts) - 1, 0, -1):
if verts[i][1] == verts[i - 1][0]:
verts.pop(i - 1)
- fill = fill_polygon([verts])
+ fill = tesselate_polygon([verts])
else:
'''
- Seperate this loop into multiple loops be finding edges that are used twice
- This is used by lightwave LWO files a lot
+ Seperate this loop into multiple loops be finding edges that are
+ used twice. This is used by lightwave LWO files a lot
'''
- if type(from_data) in (tuple, list):
- verts = [vert_treplet(Vector(from_data[i]), ii) for ii, i in enumerate(indices)]
+ if type(from_data) in {tuple, list}:
+ verts = [vert_treplet(Vector(from_data[i]), ii)
+ for ii, i in enumerate(indices)]
else:
- verts = [vert_treplet(from_data.vertices[i].co, ii) for ii, i in enumerate(indices)]
+ verts = [vert_treplet(from_data.vertices[i].co, ii)
+ for ii, i in enumerate(indices)]
edges = [(i, i - 1) for i in range(len(verts))]
if edges:
@@ -418,7 +426,7 @@ def ngon_tesselate(from_data, indices, fix_loops=True):
# See if its flipped the wrong way.
flip = None
for fi in fill:
- if flip != None:
+ if flip is not None:
break
for i, vi in enumerate(fi):
if vi == 0 and fi[i - 1] == 1:
@@ -433,3 +441,70 @@ def ngon_tesselate(from_data, indices, fix_loops=True):
fill[i] = tuple([ii for ii in reversed(fi)])
return fill
+
+
+def face_random_points(num_points, faces):
+ """
+ Generates a list of random points over mesh faces.
+
+ :arg num_points: the number of random points to generate on each face.
+ :type int:
+ :arg faces: list of the faces to generate points on.
+ :type faces: :class:`bpy.types.MeshFaces`, sequence
+ :return: list of random points over all faces.
+ :rtype: list
+ """
+
+ from random import random
+ from mathutils.geometry import area_tri
+
+ # Split all quads into 2 tris, tris remain unchanged
+ tri_faces = []
+ for f in faces:
+ tris = []
+ verts = f.id_data.vertices
+ fv = f.vertices[:]
+ tris.append((verts[fv[0]].co,
+ verts[fv[1]].co,
+ verts[fv[2]].co,
+ ))
+ if len(fv) == 4:
+ tris.append((verts[fv[0]].co,
+ verts[fv[3]].co,
+ verts[fv[2]].co,
+ ))
+ tri_faces.append(tris)
+
+ # For each face, generate the required number of random points
+ sampled_points = [None] * (num_points * len(faces))
+ for i, tf in enumerate(tri_faces):
+ for k in range(num_points):
+ # If this is a quad, we need to weight its 2 tris by their area
+ if len(tf) != 1:
+ area1 = area_tri(*tf[0])
+ area2 = area_tri(*tf[1])
+ area_tot = area1 + area2
+
+ area1 = area1 / area_tot
+ area2 = area2 / area_tot
+
+ vecs = tf[0 if (random() < area1) else 1]
+ else:
+ vecs = tf[0]
+
+ u1 = random()
+ u2 = random()
+ u_tot = u1 + u2
+
+ if u_tot > 1:
+ u1 = 1.0 - u1
+ u2 = 1.0 - u2
+
+ side1 = vecs[1] - vecs[0]
+ side2 = vecs[2] - vecs[0]
+
+ p = vecs[0] + u1 * side1 + u2 * side2
+
+ sampled_points[num_points * i + k] = p
+
+ return sampled_points