== Blender 2.56 tag ==
[blender-addons.git] / io_mesh_stl / blender_utils.py
bloba043a8f34c65eba4090c1f90995ac64e17a89b16
1 import bpy
4 def create_and_link_mesh(name, faces, points):
5 '''
6 Create a blender mesh and object called name from a list of
7 *points* and *faces* and link it in the current scene.
8 '''
10 mesh = bpy.data.meshes.new(name)
11 mesh.from_pydata(points, [], faces)
13 ob = bpy.data.objects.new(name, mesh)
14 bpy.context.scene.objects.link(ob)
16 # update mesh to allow proper display
17 mesh.update()
20 def faces_from_mesh(ob, apply_modifier=False, triangulate=True):
21 '''
22 From an object, return a generator over a list of faces.
24 Each faces is a list of his vertexes. Each vertex is a tuple of
25 his coordinate.
27 apply_modifier
28 Apply the preview modifier to the returned liste
30 triangulate
31 Split the quad into two triangles
32 '''
34 # get the modifiers
35 try:
36 mesh = ob.create_mesh(bpy.context.scene,
37 apply_modifier, "PREVIEW")
38 except SystemError:
39 return ()
41 def iter_face_index():
42 '''
43 From a list of faces, return the face triangulated if needed.
44 '''
45 for face in mesh.faces:
46 if triangulate and len(face.vertices) == 4:
47 yield face.vertices[:3]
48 yield face.vertices[2:] + [face.vertices[0]]
49 else:
50 yield list(face.vertices)
52 return ([tuple(mesh.vertices[index].co * ob.matrix_world)
53 for index in indexes] for indexes in iter_face_index())