FBX: reformat props.
[blender-addons.git] / io_mesh_stl / blender_utils.py
bloba13277adaf0931f25b51cae8afc91e6d8097e61f
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8 compliant>
21 import bpy
24 def create_and_link_mesh(name, faces, points):
25 """
26 Create a blender mesh and object called name from a list of
27 *points* and *faces* and link it in the current scene.
28 """
30 mesh = bpy.data.meshes.new(name)
31 mesh.from_pydata(points, [], faces)
33 # update mesh to allow proper display
34 mesh.validate()
35 mesh.update()
37 scene = bpy.context.scene
39 obj = bpy.data.objects.new(name, mesh)
40 scene.objects.link(obj)
41 scene.objects.active = obj
42 obj.select = True
45 def faces_from_mesh(ob, global_matrix, use_mesh_modifiers=False, triangulate=True):
46 """
47 From an object, return a generator over a list of faces.
49 Each faces is a list of his vertexes. Each vertex is a tuple of
50 his coordinate.
52 use_mesh_modifiers
53 Apply the preview modifier to the returned liste
55 triangulate
56 Split the quad into two triangles
57 """
59 # get the editmode data
60 ob.update_from_editmode()
62 # get the modifiers
63 try:
64 mesh = ob.to_mesh(bpy.context.scene, use_mesh_modifiers, "PREVIEW")
65 except RuntimeError:
66 raise StopIteration
68 mesh.transform(global_matrix * ob.matrix_world)
70 if triangulate:
71 # From a list of faces, return the face triangulated if needed.
72 def iter_face_index():
73 for face in mesh.tessfaces:
74 vertices = face.vertices[:]
75 if len(vertices) == 4:
76 yield vertices[0], vertices[1], vertices[2]
77 yield vertices[2], vertices[3], vertices[0]
78 else:
79 yield vertices
80 else:
81 def iter_face_index():
82 for face in mesh.tessfaces:
83 yield face.vertices[:]
85 vertices = mesh.vertices
87 for indexes in iter_face_index():
88 yield [vertices[index].co.copy() for index in indexes]
90 bpy.data.meshes.remove(mesh)