-
Notifications
You must be signed in to change notification settings - Fork 3
/
mesh_tools.py
301 lines (242 loc) · 10.6 KB
/
mesh_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import math
import random
import bpy
import bpy_extras.mesh_utils
from mathutils import Vector
def get_random_points_on_verts(mesh, amount, transform_matrix, seed=0):
"""
get_random_points_on_verts(mesh mesh, int amount,
matrix transform_matrix, int seed)
-> list of vector points
Gets <amount> number of random vert coordinates.
mesh mesh - the mesh to get the points from
int amount - the amount of points to return
matrix transform_matrix - the matrix to transform the points by
int seed - the seed for the randomization
"""
random.seed(seed)
points = []
for _ in range(amount):
points.append(transform_matrix * random.choice(mesh.vertices).co)
return points
def get_random_points_on_edges(mesh, amount, transform_matrix, seed=0):
"""
get_random_points_on_edges(mesh mesh, int amount,
matrix transform_matrix, int seed)
-> list of vector points
Gets <amount> number of random points on the edges of the mesh.
mesh mesh - the mesh to get the points from
int amount - the amount of points to return
matrix transform_matrix - the matrix to transform the points by
int seed - the seed for the randomization
"""
random.seed(seed)
points = []
for _ in range(amount):
edge = random.choice(mesh.edges)
v1 = mesh.vertices[edge.vertices[0]]
v2 = mesh.vertices[edge.vertices[1]]
p = v1.co + random.random() * (v2.co - v1.co)
points.append(transform_matrix * p)
return points
# def get_random_points_on_surface(mesh, amount, transform_matrix):
# """
# get_random_points_on_surface(mesh mesh, int amount, matrix transform_matrix)
# -> list of vector points
# Gets <amount> number of random points on the surface of the mesh.
# mesh mesh - the mesh to get the points from
# int amount - the amount of points to return
# matrix transform_matrix - the matrix to transform the points by
# """
# tessfaces = mesh.tessfaces
# num_points = math.ceil(amount / len(tessfaces))
# points = bpy_extras.mesh_utils.face_random_points(num_points, tessfaces)
# # points = random.sample(points, amount)
# random.shuffle(points)
# points = [transform_matrix * p for p in points[:amount]]
# # points = []
# # for _ in range(amount):
# # face = random.choice(mesh.tessfaces)
# # p = bpy_extras.mesh_utils.face_random_points(1, [face])[0]
# # points.append(transform_matrix * p)
# return points
def get_random_points_on_surface(obj, amount, seed=0):
"""
get_random_points_on_surface(objet obj, int amount, int seed)
-> list of vector points
Gets <amount> number of random points on the surface of the object.
object obj - the object to get the points from
int amount - the amount of points to return
int seed - the seed for the randomization
"""
m = obj.modifiers.new('points', 'PARTICLE_SYSTEM')
ps = m.particle_system
ps.seed = seed
ps.settings.count = amount
ps.settings.frame_start = 1
ps.settings.frame_end = 1
ps.settings.emit_from = 'FACE'
ps.settings.physics_type = 'NO'
ps.settings.use_modifier_stack = True
bpy.context.scene.update()
points = [p.location.copy() for p in ps.particles]
obj.modifiers.remove(m)
return points
# def get_random_points_in_volume(obj, amount):
# # !!! NEEDS FIXING!!!
# # !!! PROBABLY USE PARTICLES AS WELL!
# """
# get_random_points_in_volume(object obj, int amount)
# -> list of vector points
# Gets <amount> number of random points inside the volume of the mesh.
# object obj - the object to get the points from
# int amount - the amount of points to return
# Adopted from code by CoDEmanX and pi (19.01.2014)
# """
# def point_in_box(O, W):
# return Vector((random.uniform(O.x, W.x),
# random.uniform(O.y, W.y),
# random.uniform(O.z, W.z)))
# points = []
# max_attempts = 999
# bbox = [Vector(b) for b in obj.bound_box]
# O = bbox[0]
# W = bbox[6]
# X = Vector((W.x - O.x, 0.0, 0.0))
# errors = 0
# for _ in range(amount):
# got_point = False
# for _ in range(max_attempts):
# p = point_in_box(O, W)
# _, normal, index = obj.ray_cast(p, p + X)
# if index > -1 and normal.x > 0.0:
# got_point = True
# break
# if not got_point:
# errors += 1
# continue
# points.append(obj.matrix_world * p)
# if errors:
# print("Max attempts reached, got {} points less "
# "then specified...".format(errors))
# return points
def get_random_points_in_volume(obj, amount, seed=0):
"""
get_random_points_in_volume(object obj, int amount, int seed)
-> list of vector points
Gets <amount> number of random points inside the volume of the object.
object obj - the object to get the points from
int amount - the amount of points to return
int seed - the seed for the randomization
"""
m = obj.modifiers.new('points', 'PARTICLE_SYSTEM')
ps = m.particle_system
ps.seed = seed
ps.settings.count = amount
ps.settings.frame_start = 1
ps.settings.frame_end = 1
ps.settings.emit_from = 'VOLUME'
ps.settings.physics_type = 'NO'
ps.settings.use_modifier_stack = True
bpy.context.scene.update()
points = [p.location.copy() for p in ps.particles]
obj.modifiers.remove(m)
return points
# def get_point_on_edge(edge, transform_matrix, method='RANDOM'):
# """
# get_point_on_edge(edge edge, string method) -> vector
# Calculates a point on an edge according to method in world space.
# edge edge - the edge to calculate a point on
# matrix transform_matrix - the matrix to transform the points by
# string method - the method to calculate the point
# valid options: - 'RANDOM'
# - 'HALFWAY'
# - 'END_POINTS'
# """
# valid_methods = {'RANDOM', 'HALFWAY', 'END_POINTS'}
# if not method in valid_methods:
# return
# if method == 'RANDOM' or 'HALFWAY':
# v1 = edge.id_data.vertices[edge.vertices[0]]
# v2 = edge.id_data.vertices[edge.vertices[1]]
# if method == 'RANDOM':
# fac = random.random()
# else:
# fac = 0.5
# return transform_matrix * (v1.co + fac * (v2.co - v1.co))
# elif method == 'END_POINTS':
# v = edge.id_data.vertices[random.choice(edge.vertices)]
# return transform_matrix * v.co
def get_points(obj, amount=1, method='SURFACE', apply_modifiers=True, seed=0):
"""
get_points(object obj,
int amount,
string method,
bool apply_modifiers) -> tuple of vector points
Calculates points on the object according to method in world space.
!!! For now, apart from "pivot", they will be random.
Later there might be an option to change this behavior. !!!
object obj - the object to calculate the points on
int amount - the amount of points to calculate
string method - the method to calculate the points
valid options: - 'VERTS'
- 'EDGES'
- 'SURFACE'
- 'VOLUME'
- 'PIVOT'
bool apply_modifiers - use the deformed or original mesh
int seed - the seed for the randomization
"""
valid_methods = {'VERTS', 'EDGES', 'SURFACE', 'VOLUME', 'PIVOT'}
if not method in valid_methods:
return
if apply_modifiers and not method == 'PIVOT':
mesh = obj.to_mesh(bpy.context.scene, True, 'PREVIEW')
elif method != 'PIVOT':
mesh = obj.data.copy()
transform_matrix = obj.matrix_world.copy()
if method == 'VERTS':
return get_random_points_on_verts(mesh, amount,
transform_matrix, seed=seed)
# points.append(transform_matrix * random.choice(mesh.vertices).co)
if method == 'EDGES':
return get_random_points_on_edges(mesh, amount,
transform_matrix, seed=seed)
# edge = random.choice(mesh.edges)
# points.append(get_point_on_edge(edge, transform_matrix))
if method == 'SURFACE':
return get_random_points_on_surface(obj, amount, seed=seed)
# face = random.choice(mesh.tessfaces)
# point = bpy_extras.mesh_utils.face_random_points(1, [face])[0]
# points.append(transform_matrix * point)
if method == 'VOLUME':
return get_random_points_in_volume(obj, amount, seed=seed)
if method == 'PIVOT':
# Only return the pivot point
return [transform_matrix.to_translation()]
# obj = bpy.data.objects['Suzanne']
# points = get_points(obj, amount=33, method='SURFACE')
# for p in points:
# bpy.ops.mesh.primitive_cube_add(location=p, radius=.05)
# bpy.ops.object.material_slot_add()
# bpy.context.object.material_slots[0].material = bpy.data.materials['red']
# for obj in bpy.data.objects:
# if 'Cube' in obj.name:
# obj.select = True