-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.py
More file actions
executable file
·367 lines (272 loc) · 10.8 KB
/
functions.py
File metadata and controls
executable file
·367 lines (272 loc) · 10.8 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# 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 bpy
import random
import bgl
import blf
import bmesh
from bpy_extras import view3d_utils
import re
from mathutils import Vector, Matrix
from mathutils.geometry import tessellate_polygon
from .database import *
###### ______Utils Class Definition______ ######
class construction_point():
def __init__(self, point_coord, point_normal=Vector((0.0,0.0,1.0))):
self.point = point_coord
self.normal = point_normal
###### ______Utils Functions Definition______ ######
def get_db_categories(self,context):
"""Get a list of all the current categories stored in database"""
db_path = context.user_preferences.addons[__package__].preferences.dbPath
return get_all_categories(db_path)
def get_group_list(self,context):
"""Get an enum of all the groups in the file"""
result = []
for group in bpy.data.groups:
result.append((group.name,group.name,group.name))
return result
def link_groups_to_file(context,pFilePath,pGroupName):
with bpy.data.libraries.load(pFilePath, link=True, relative=True) as (data_from, data_to):
data_to.groups = [pGroupName]
def link_category_to_file(context):
dbPath = context.user_preferences.addons[__package__].preferences.dbPath
catId = context.scene.build_props.assets_categories
rows = get_group_list_in_category(dbPath,catId)
for row in rows:
filePath=get_blender_file_abs_path(dbPath, row[2])
link_groups_to_file(context,filePath,row[1])
def set_new_groupName(pGroupName):
"""Increment an existing groupName like that:
rocher => rocher_2
rocher_23 => rocher_24"""
rex = re.compile('_\d+$')
m = rex.search(pGroupName)
if m:
var = m.group(0)
varValue = int(var[1:])
pGroupName = pGroupName[:-len(var)]+"_"+str(varValue+1)
else:
pGroupName = '%s_2' %pGroupName
return pGroupName
def extract_groupName(pObj):
"""Extract the GroupName from the name of an object"""
rex = re.compile('\.\d{3}$')
groupName = pObj.name[2:]
if rex.search(pObj.name):
groupName = groupName[:-4]
return groupName
def area_of_type(type_name):
for area in bpy.context.screen.areas:
if area.type == type_name:
return area
def get_3d_view():
return area_of_type('VIEW_3D').spaces[0]
def get_faces_with_normal(pNormal, pTolerance):
"""Return a list of faces that have similar normal"""
#TODO
return []
def get_tuple(iterable, length, format=tuple):
it = iter(iterable)
while True:
yield format(chain((next(it),), islice(it, length - 1)))
def get_group_dimension_x(context,pGroupName):
minx = 99999.0
maxx = -99999.0
group = bpy.data.groups[pGroupName]
for obj in group.objects:
for v in obj.bound_box:
v_world = obj.matrix_world * Vector((v[0],v[1],v[2]))
if v_world[0] < minx:
minx = v_world[0]
if v_world[0] > maxx:
maxx = v_world[0]
return maxx-minx
def get_group_min_x_offset(context,pGroupName):
minX = 99999.0
group = bpy.data.groups[pGroupName]
for obj in group.objects:
for v in obj.bound_box:
v_world = obj.matrix_world * Vector((v[0],v[1],v[2]))
if v_world[0] < minX:
minX = v_world[0]
centerX = group.dupli_offset[0]
return centerX-minX
def add_prop_instance(context,groupName):
"""Add a new empty to the scene with a dupligroup on it.
We assume that the group is already linked to scene!"""
group = bpy.data.groups[groupName]
o = bpy.data.objects.new('i_{}'.format(groupName), None )
o.dupli_type = 'GROUP'
o.dupli_group = group
o.empty_draw_size = 1
o.empty_draw_type = 'PLAIN_AXES'
context.scene.objects.link(o)
return o
def generate_random_tuple(nb_tuple,v0,v1,v2):
result = []
for i in range(nb_tuple):
s, t = sorted([random.random(), random.random()])
result.append(s*v0.co+(t-s)*v1.co+(1-t)*v2.co)
return result
def get_db_path(context):
return context.user_preferences.addons[__package__].preferences.dbPath
###### ______Functions Definition______ ######
def unwrap_mesh_to_box(context,pScale):
bpy.ops.uv.cube_project()
def generate_room(context,height):
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(0, 0, height)})
#on déplie tout le monde
#on separe les murs et le toit
unwrap_mesh_to_box(context,0.1)
def orient_object_to_normal(context,pNormVect,pObject):
ori = pObject.orientation
mat = Matrix(ori[0], ori[1], ori[2])
pos = pObject.position
vec1 = Vector(pos[0], pos[1], pos[2]) # Convert the position into a vector
vec2 = Vector(0,0,-1) # Make a vector for the end point of the ray (below the object)
vec3 = vec2 * mat # Convert the end point into local coords of the ob
vec4 = vec1 + vec3 # Add the end point to the position vector to account for translation
ray = pObject.rayCast(vec4, ob, 15, '') # Cast a ray
if ray[0]:
pObject.alignAxisToVect(ray[2], 2, 1) # Align the object to the hit normal of the ray
def get_props_order(context,edge_length,rows):
"""Used for ModalDrawLineOperator.
Return a list containing group, its X dimension and its potential
scale value. The list is randomized based on the current context
seed value"""
#row schema : ID,GROUP_NAME,FILE_PATH,GROUP_LENGTH,OFFSET_X
propsOrder = []
i = 0
curLen = 0.0
cur_index = 0
scale_value = context.scene.build_props.line_scale_factor
while curLen < edge_length:
cur_index = i % len(rows)
dimX = rows[cur_index][3]*scale_value
offsetX = rows[cur_index][4]
groupName = rows[cur_index][1]
propsOrder.append((dimX,groupName,1.0,offsetX)) #Last value is scale_value
curLen += dimX
i +=1
overflow = curLen - edge_length
elem_size = rows[cur_index][3]
scale_factor = (elem_size-overflow)/elem_size
(dimX,groupName,scale_value,offsetX) = propsOrder[-1]
propsOrder[-1] = (dimX,groupName,scale_factor,offsetX)
random.seed(context.scene.build_props.seed)
random.shuffle(propsOrder)
return propsOrder
def obj_ray_cast(obj, matrix,ray_origin,ray_target):
"""Wrapper for ray casting that moves the ray into object space"""
# get the ray relative to the object
print(obj.name)
print(matrix)
matrix_inv = matrix.inverted()
ray_origin_obj = matrix_inv * ray_origin
ray_target_obj = matrix_inv * ray_target
ray_direction_obj = ray_target_obj - ray_origin_obj
# cast the ray
success, location, normal, face_index = obj.ray_cast(ray_origin_obj, ray_direction_obj)
if success:
return location, normal, face_index
else:
return None, None, None
def visibles_objects_without_builder_props(context):
"""Loop over (object, matrix) pairs (mesh only)"""
for obj in context.visible_objects:
if obj.type == 'MESH':
if obj.parent is not None:
print(obj.parent.name)
yield (obj, obj.matrix_world.copy())
def visible_objects_and_duplis(context):
"""Loop over (object, matrix) pairs (mesh only)"""
for obj in context.visible_objects:
if obj.type == 'MESH':
yield (obj, obj.matrix_world.copy())
if obj.dupli_type != 'NONE':
obj.dupli_list_create(context.scene)
for dob in obj.dupli_list:
obj_dupli = dob.object
if obj_dupli.type == 'MESH':
yield (obj_dupli, dob.matrix.copy())
obj.dupli_list_clear()
def get_ray_cast_result(context,coord_mouse,use_all_objects=False):
region = context.region
rv3d = context.space_data.region_3d
# get the ray from the viewport and mouse
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord_mouse)
ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord_mouse)
ray_target = ray_origin + view_vector
# cast rays and find the closest object
best_length_squared = -1.0
best_obj = None
best_hit = None #best hit location
best_normal = None
best_face_index = None
obj_list = None
if use_all_objects:
obj_list = visible_objects_and_duplis(context)
else:
obj_list = visibles_objects_without_builder_props(context)
for obj, matrix in obj_list:
if obj.type != 'MESH':
continue
hit, normal, face_index = obj_ray_cast(obj, matrix,ray_origin,ray_target)
if hit is not None:
hit_world = matrix * hit
length_squared = (hit_world - ray_origin).length_squared
if best_obj is None or length_squared < best_length_squared:
best_length_squared = length_squared
best_obj = obj
best_hit = hit_world
rot_quaternion = matrix.decompose()[1]
best_normal = rot_quaternion.to_matrix() * normal
best_face_index = face_index
return best_hit,best_obj,best_normal,best_face_index
def delete_temp_objects(context,obj_to_delete):
for obj in context.visible_objects:
obj.select = False
for name in obj_to_delete:
context.scene.objects[name].hide_select = False
context.scene.objects[name].select = True
bpy.ops.object.delete(use_global=True)
def define_random_points_in_ngon(context,construction_points,density=0.1):
scene = bpy.context.scene
bm = bmesh.new()
points = []
for point in construction_points:
points.append(point.point)
tess = tessellate_polygon((points,))
bm_tess = bmesh.new()
faces = []
random_points = []
for tri in tess:
verts = []
for p in tri:
verts.append(bm_tess.verts.new(points[p]))
faces.append(bm_tess.faces.new(verts))
dens = int(density*faces[-1].calc_area())
v = generate_random_tuple(dens,verts[0],verts[1],verts[2])
random_points.extend(v)
me_tess = bpy.data.meshes.new("Tris")
bm_tess.to_mesh(me_tess)
ob = bpy.data.objects.new("Tris", me_tess)
scene.objects.link(ob)
return ob, random_points