Fix T52833: OBJ triangulate doesn't match viewport
[blender-addons.git] / io_mesh_stl / blender_utils.py
blob864335ab91adad0224b197d19acf88ef4726b5e2
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
22 import array
23 from itertools import chain
26 def create_and_link_mesh(name, faces, face_nors, points, global_matrix):
27 """
28 Create a blender mesh and object called name from a list of
29 *points* and *faces* and link it in the current scene.
30 """
32 mesh = bpy.data.meshes.new(name)
33 mesh.from_pydata(points, [], faces)
35 if face_nors:
36 # Note: we store 'temp' normals in loops, since validate() may alter final mesh,
37 # we can only set custom lnors *after* calling it.
38 mesh.create_normals_split()
39 lnors = tuple(chain(*chain(*zip(face_nors, face_nors, face_nors))))
40 mesh.loops.foreach_set("normal", lnors)
42 mesh.transform(global_matrix)
44 # update mesh to allow proper display
45 mesh.validate(clean_customdata=False) # *Very* important to not remove lnors here!
47 if face_nors:
48 clnors = array.array('f', [0.0] * (len(mesh.loops) * 3))
49 mesh.loops.foreach_get("normal", clnors)
51 mesh.polygons.foreach_set("use_smooth", [True] * len(mesh.polygons))
53 mesh.normals_split_custom_set(tuple(zip(*(iter(clnors),) * 3)))
54 mesh.use_auto_smooth = True
55 mesh.show_edge_sharp = True
56 mesh.free_normals_split()
58 mesh.update()
60 scene = bpy.context.scene
62 obj = bpy.data.objects.new(name, mesh)
63 scene.objects.link(obj)
64 scene.objects.active = obj
65 obj.select = True
68 def faces_from_mesh(ob, global_matrix, use_mesh_modifiers=False, triangulate=True):
69 """
70 From an object, return a generator over a list of faces.
72 Each faces is a list of his vertexes. Each vertex is a tuple of
73 his coordinate.
75 use_mesh_modifiers
76 Apply the preview modifier to the returned liste
78 triangulate
79 Split the quad into two triangles
80 """
82 # get the editmode data
83 ob.update_from_editmode()
85 # get the modifiers
86 try:
87 mesh = ob.to_mesh(bpy.context.scene, use_mesh_modifiers, "PREVIEW")
88 except RuntimeError:
89 raise StopIteration
91 mat = global_matrix * ob.matrix_world
92 mesh.transform(mat)
93 if mat.is_negative:
94 mesh.flip_normals()
95 mesh.calc_tessface()
97 if triangulate:
98 # From a list of faces, return the face triangulated if needed.
99 def iter_face_index():
100 for face in mesh.tessfaces:
101 vertices = face.vertices[:]
102 if len(vertices) == 4:
103 yield vertices[0], vertices[1], vertices[2]
104 yield vertices[2], vertices[3], vertices[0]
105 else:
106 yield vertices
107 else:
108 def iter_face_index():
109 for face in mesh.tessfaces:
110 yield face.vertices[:]
112 vertices = mesh.vertices
114 for indexes in iter_face_index():
115 yield [vertices[index].co.copy() for index in indexes]
117 bpy.data.meshes.remove(mesh)