Merge branch 'blender-v2.92-release'
[blender-addons.git] / io_mesh_stl / blender_utils.py
blob97621d112bb572be152ae3c6c3cc1d3af336c7b5
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>
22 def create_and_link_mesh(name, faces, face_nors, points, global_matrix):
23 """
24 Create a blender mesh and object called name from a list of
25 *points* and *faces* and link it in the current scene.
26 """
28 import array
29 from itertools import chain
30 import bpy
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 obj = bpy.data.objects.new(name, mesh)
61 bpy.context.collection.objects.link(obj)
62 bpy.context.view_layer.objects.active = obj
63 obj.select_set(True)
66 def faces_from_mesh(ob, global_matrix, use_mesh_modifiers=False):
67 """
68 From an object, return a generator over a list of faces.
70 Each faces is a list of his vertexes. Each vertex is a tuple of
71 his coordinate.
73 use_mesh_modifiers
74 Apply the preview modifier to the returned liste
76 triangulate
77 Split the quad into two triangles
78 """
80 import bpy
82 # get the editmode data
83 if ob.mode == "EDIT":
84 ob.update_from_editmode()
86 # get the modifiers
87 if use_mesh_modifiers:
88 depsgraph = bpy.context.evaluated_depsgraph_get()
89 mesh_owner = ob.evaluated_get(depsgraph)
90 else:
91 mesh_owner = ob
93 # Object.to_mesh() is not guaranteed to return a mesh.
94 try:
95 mesh = mesh_owner.to_mesh()
96 except RuntimeError:
97 return
99 if mesh is None:
100 return
102 mat = global_matrix @ ob.matrix_world
103 mesh.transform(mat)
104 if mat.is_negative:
105 mesh.flip_normals()
106 mesh.calc_loop_triangles()
108 vertices = mesh.vertices
110 for tri in mesh.loop_triangles:
111 yield [vertices[index].co.copy() for index in tri.vertices]
113 mesh_owner.to_mesh_clear()