Fix T38788: zero area faces raised exception with overhang test
[blender-addons.git] / io_mesh_raw / export_raw.py
blobb5c5ef36fea3b28cc3b7d5627738ef87e4a9815a
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-80 compliant>
21 """
22 This script exports a Mesh to a RAW triangle format file.
24 The raw triangle format is very simple; it has no verts or faces lists.
25 It's just a simple ascii text file with the vertices of each triangle
26 listed on each line. In addition, also quads can be exported as a line
27 of 12 values (this was the default before blender 2.5). Now default
28 settings will triangulate the mesh.
30 Usage:
31 Execute this script from the "File->Export" menu. You can select
32 whether modifiers should be applied and if the mesh is triangulated.
34 """
36 import bpy
39 def faceToTriangles(face):
40 triangles = []
41 if len(face) == 4:
42 triangles.append([face[0], face[1], face[2]])
43 triangles.append([face[2], face[3], face[0]])
44 else:
45 triangles.append(face)
47 return triangles
50 def faceValues(face, mesh, matrix):
51 fv = []
52 for verti in face.vertices:
53 fv.append((matrix * mesh.vertices[verti].co)[:])
54 return fv
57 def faceToLine(face):
58 return " ".join([("%.6f %.6f %.6f" % v) for v in face] + ["\n"])
61 def write(filepath,
62 applyMods=True,
63 triangulate=True,
66 scene = bpy.context.scene
68 faces = []
69 for obj in bpy.context.selected_objects:
70 if applyMods or obj.type != 'MESH':
71 try:
72 me = obj.to_mesh(scene, True, "PREVIEW")
73 except:
74 me = None
75 is_tmp_mesh = True
76 else:
77 me = obj.data
78 if not me.tessfaces and me.polygons:
79 me.calc_tessface()
80 is_tmp_mesh = False
82 if me is not None:
83 matrix = obj.matrix_world.copy()
84 for face in me.tessfaces:
85 fv = faceValues(face, me, matrix)
86 if triangulate:
87 faces.extend(faceToTriangles(fv))
88 else:
89 faces.append(fv)
91 if is_tmp_mesh:
92 bpy.data.meshes.remove(me)
94 # write the faces to a file
95 file = open(filepath, "w")
96 for face in faces:
97 file.write(faceToLine(face))
98 file.close()