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 #####
22 def create_and_link_mesh(name
, faces
, face_nors
, points
, global_matrix
):
24 Create a blender mesh and object called name from a list of
25 *points* and *faces* and link it in the current scene.
29 from itertools
import chain
32 mesh
= bpy
.data
.meshes
.new(name
)
33 mesh
.from_pydata(points
, [], faces
)
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!
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()
60 obj
= bpy
.data
.objects
.new(name
, mesh
)
61 bpy
.context
.collection
.objects
.link(obj
)
62 bpy
.context
.view_layer
.objects
.active
= obj
66 def faces_from_mesh(ob
, global_matrix
, use_mesh_modifiers
=False):
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
74 Apply the preview modifier to the returned liste
77 Split the quad into two triangles
82 # get the editmode data
84 ob
.update_from_editmode()
87 if use_mesh_modifiers
:
88 depsgraph
= bpy
.context
.evaluated_depsgraph_get()
89 mesh_owner
= ob
.evaluated_get(depsgraph
)
93 # Object.to_mesh() is not guaranteed to return a mesh.
95 mesh
= mesh_owner
.to_mesh()
102 mat
= global_matrix
@ ob
.matrix_world
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()