-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexec.py
457 lines (391 loc) · 15.1 KB
/
exec.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import lsystem.lsystem
import lsystem.turtle
import lsystem.util
import math
import time
import random
import copy
import bpy
import bpy_extras.mesh_utils
import mathutils
class Exec:
def __init__(self):
self.objects = []
# self.object_base_pairs = []
self.axiom = lsystem.lsystem.ProductionRule("", "")
self.rules = []
self.constants = {}
self.replacements = None
self.interpretations = {}
self.tropism_vector = (0,0,0)
self.tropism_force = 0
self.instances = 1
self.seed = 0
self.min_iterations = 1
self.max_iterations = 1
self.angle = math.radians(25)
self.length = 1.0
self.radius = 0.1
self.expansion = 1.1
self.shrinkage = 0.9
self.fat = 1.2
self.slinkage = 0.8
self.animate = False
self.frame_delta = 5
def set_axiom(self, axiom_str):
self.axiom = lsystem.lsystem.ProductionRule("", axiom_str)
def set_rule(self, pattern, result, condition=None, index=None):
if index is None:
self.add_rule(pattern, result, condition)
new_rule = lsystem.lsystem.ProductionRule(pattern, result, condition)
self.rules[index] = new_rule
def add_rule(self, pattern, result, condition=None):
new_rule = lsystem.lsystem.ProductionRule(pattern, result, condition)
self.rules.append(new_rule)
def define(self, constant, value):
self.constants[constant] = value
def replace(self, pattern, result, condition=None):
new_rule = lsystem.lsystem.ProductionRule(pattern, result, condition)
if self.replacements is None:
self.replacements = []
self.replacements.append(new_rule)
def set_tropism(self, vector, force):
self.tropism_vector = vector
self.tropism_vector.normalize()
self.tropism_force = force
def set_interpretation(self, symbol, function):
self.interpretations[symbol] = function
def exec(self,
context=None,
instances=None,
seed=None,
min_iterations=None,
max_iterations=None,
angle=None,
length=None,
radius=None,
expansion=None,
shrinkage=None,
fat=None,
slinkage=None,
animate=None,
frame_delta=None):
if context is None:
context = bpy.context
if instances is not None:
self.instances = instances
if seed is not None:
self.seed = seed
if min_iterations is not None:
self.min_iterations = min_iterations
if max_iterations is not None:
self.max_iterations = max_iterations
if angle is not None:
self.angle = math.radians(angle)
if length is not None:
self.length = length
if radius is not None:
self.radius = radius
if expansion is not None:
self.expansion = expansion
if shrinkage is not None:
self.shrinkage = shrinkage
if fat is not None:
self.fat = fat
if slinkage is not None:
self.slinkage = slinkage
if animate is not None:
self.animate = animate
if frame_delta is not None:
self.frame_delta = frame_delta
self.delete()
axiom = self.axiom.copy_replace(self.constants)
rules = []
for rule in self.rules:
rules.append(rule.copy_replace(self.constants))
self.objects = execute(context,
axiom,
rules,
self.replacements,
instances=self.instances,
seed=self.seed,
min_iterations=self.min_iterations,
max_iterations=self.max_iterations,
angle=self.angle,
length=self.length,
radius=self.radius,
expansion=self.expansion,
shrinkage=self.shrinkage,
fat=self.fat,
slinkage=self.slinkage,
animate=self.animate,
frame_delta=self.frame_delta,
normal=(0.0, 0.0, 1.0),
tropism_vector=self.tropism_vector,
tropism_force=self.tropism_force,
interpretations=self.interpretations)
def select(self):
if hasattr(bpy.app, "version") and bpy.app.version >= (2, 80):
# This doesn't work for objects that aren't visible in the current keyframe
for ob in bpy.context.selected_objects:
ob.select_set(False)
for ob in self.objects:
ob.select_set(True)
else:
# deselect currently selected objects
for ob in bpy.context.selected_objects:
ob.select = False
# select objects belonging to this LSystem
for ob in self.objects:
ob.select = True
def delete(self):
old_selected = self.get_selection()
if hasattr(bpy.app, "version") and bpy.app.version >= (2, 80):
for obj in self.objects:
try:
bpy.data.objects.remove(obj, do_unlink=True)
except ReferenceError:
# object already deleted, ignore
pass
else:
self.select()
try:
bpy.ops.object.delete()
except ReferenceError:
# lsystem objects already deleted, ignore
pass
if hasattr(bpy.app, "version") and bpy.app.version >= (2, 80):
for ob in old_selected:
if ob not in self.objects:
ob.select_set(True)
else:
for ob in old_selected:
ob.select = True
def get_selection(self):
selected = []
for ob in bpy.context.selected_objects:
selected.append(ob)
return selected
def __str__(self):
str = "{}\n".format(self.axiom)
for rule in self.rules:
str += "{}\n".format(rule)
return str
class PosInfo:
def __init__(self, position, normal, obj, seed, iterations):
self.position = position
self.normal = normal
self.obj = obj
self.seed = seed
self.iterations = iterations
def execute(context,
axiom,
rules,
replacements,
instances=1,
seed=0,
min_iterations=1,
max_iterations=1,
angle=math.radians(25),
length=1.0,
radius=0.1,
expansion=1.1,
shrinkage=0.9,
fat=1.2,
slinkage=0.8,
animate=False,
frame_delta=5,
normal=(0.0, 0.0, 1.0),
tropism_vector=(0.0, 0.0, -1.0),
tropism_force=0.0,
interpretations=None):
turtle = lsystem.turtle.Turtle(seed)
turtle.set_angle(angle)
turtle.set_length(length)
turtle.set_radius(radius)
turtle.set_expansion(expansion)
turtle.set_shrinkage(shrinkage)
turtle.set_fat(fat)
turtle.set_slinkage(slinkage)
turtle.set_direction(mathutils.Vector((normal[0], normal[1], normal[2])))
turtle.set_tropism(tropism_vector, tropism_force)
if interpretations is not None:
for key in interpretations:
turtle.set_interpretation(key, interpretations[key])
lsys = lsystem.lsystem.LSystem(axiom, rules, replacements)
return exec_turtle(context, lsys, instances, min_iterations, max_iterations, animate, turtle, frame_delta)
def exec_turtle(context, lsys, instances, min_iterations, max_iterations, animate, turtle, frame_delta=5):
# Need to call scene.update for ray_cast method.
# See http://blender.stackexchange.com/questions/40429/error-object-has-no-mesh-data-to-be-used-for-ray-casting
if hasattr(bpy.app, "version") and bpy.app.version < (2, 80):
bpy.context.scene.update()
rmax_iter = max_iterations+1
if max_iterations <= min_iterations:
rmax_iter = min_iterations+1
pos_info_list = get_pos_info(instances, min_iterations, rmax_iter, turtle.seed, animate)
inst_list = []
seed = turtle.seed
pos_info_index = 0
for instance in range(0, instances):
iter_list = []
for iterations in range(min_iterations, rmax_iter):
new_turtle = copy.deepcopy(turtle)
new_turtle.seed = seed
pos_info = None
if pos_info_list:
pos_info = pos_info_list[pos_info_index]
if pos_info:
new_turtle.set_direction(pos_info.normal)
new_turtle.seed = pos_info.seed
object_base_pairs = run_once(context, new_turtle, instance, lsys, iterations)
if pos_info:
obj = object_base_pairs[0][0]
obj.location = pos_info.position
obj.parent = pos_info.obj
iter_list.append(object_base_pairs)
pos_info_index += 1
seed += 1
inst_list.append(iter_list)
if not pos_info_list:
grid(inst_list, not animate)
if hasattr(bpy.app, "version") and bpy.app.version >= (2, 80):
for ob in context.scene.objects:
ob.select_set(False)
else:
for ob in context.scene.objects:
ob.select = False
if animate:
animate_inst_list(inst_list, frame_delta)
objects = []
if hasattr(bpy.app, "version") and bpy.app.version >= (2, 80):
for iter_list in inst_list:
for object_base_pairs in iter_list:
for obj_base_pair in object_base_pairs:
obj = obj_base_pair[0]
obj.select_set(True)
objects.append(obj)
context.view_layer.objects.active = objects[-1]
else:
for iter_list in inst_list:
for object_base_pairs in iter_list:
for obj_base_pair in object_base_pairs:
base = obj_base_pair[1]
base.select = True # no bases in 2.80
objects.append(obj_base_pair[0])
context.scene.objects.active = object_base_pairs[-1][0] # this won't work in 2.80
return objects
def get_pos_info(instances, min_iterations, max_iterations, seed, animate):
selected = bpy.context.selected_objects
print("selected: " + str(selected))
if not selected:
return None
faces = get_selected_faces(selected)
pos_info_list = []
current_seed = seed
if animate:
for instances in range(0, instances):
face, ob = random.choice(faces)
new_positions = bpy_extras.mesh_utils.triangle_random_points(1, [face])
pos = new_positions[0]
for iter in range(min_iterations, max_iterations):
pos_info = PosInfo(pos, face.normal, ob, current_seed, iter)
pos_info_list.append(pos_info)
current_seed += 1
else:
for instances in range(0, instances):
for iter in range(min_iterations, max_iterations):
face, ob = random.choice(faces)
if hasattr(bpy.app, "version") and bpy.app.version >= (2, 80):
new_positions = bpy_extras.mesh_utils.triangle_random_points(1, [face])
else:
new_positions = bpy_extras.mesh_utils.face_random_points(1, [face])
pos_info = PosInfo(new_positions[0], face.normal, ob, current_seed, iter)
pos_info_list.append(pos_info)
current_seed += 1
return pos_info_list
def animate_inst_list(inst_list, frame_delta=5):
for iter_list in inst_list:
animate_iter_list(iter_list, frame_delta)
def animate_iter_list(iter_list, frame_delta=5):
frame = 0
for object_base_pair_list in iter_list:
for object_base_pair in object_base_pair_list:
obj = object_base_pair[0]
lsystem.util.hide_render(obj, True, 0)
lsystem.util.hide_render(obj, False, frame)
lsystem.util.hide_render(obj, True, frame+frame_delta)
frame += frame_delta
if hasattr(bpy.app, "version") and bpy.app.version >= (2, 80):
bpy.data.scenes["Scene"].frame_end = frame
def get_selected_faces(objects):
if hasattr(bpy.app, "version") and bpy.app.version >= (2, 80):
polygons = []
for ob in objects:
me = ob.data
# me.update(calc_loop_triangles=True)
me.calc_loop_triangles()
selected_tris = [(t, ob) for t in me.loop_triangles] # just get all faces for now
polygons.extend(selected_tris)
return polygons
tessfaces = []
for ob in objects:
me = ob.data
me.calc_tessface()
tessfaces_select = [(f, ob) for f in me.tessfaces if f.select]
tessfaces.extend(tessfaces_select)
return tessfaces
def add_to_selected_faces(inst_list, objects):
faces = get_selected_faces(objects)
for iter_list in inst_list:
for obj_base_pairs in iter_list:
face, ob = random.choice(faces)
new_positions = bpy_extras.mesh_utils.face_random_points(1, [face])
obj = obj_base_pairs[0][0]
obj.location = new_positions[0]
obj.parent = ob
def grid(inst_list, move_x=True):
if hasattr(bpy.app, "version") and bpy.app.version >= (2, 80):
cursor_loc = bpy.context.scene.cursor.location
else:
cursor_loc = bpy.context.scene.cursor_location
y = 0
for iter_list in inst_list:
x = 0
max_ydim = 0
for obj_base_pairs in iter_list:
obj = obj_base_pairs[0][0]
dx = get_max_x(obj_base_pairs)
dy = get_max_y(obj_base_pairs)
obj.location = (cursor_loc.x+x, cursor_loc.y+y, cursor_loc.z)
if move_x:
x += dx
if obj.dimensions.y > max_ydim:
max_ydim += dy
y += max_ydim
def get_max_x(obj_base_pairs):
x = 1.0
for obj, base in obj_base_pairs:
cx = obj.location.x + obj.dimensions.x
if cx > x:
x = cx
return x
def get_max_y(obj_base_pairs):
y = 1.0
for obj, base in obj_base_pairs:
cy = obj.location.y + obj.dimensions.y
if cy > y:
y = cy
return y
def run_once(context, turtle, instance, lsys, iterations):
start_time = time.time()
print_time(start_time, "lsystem: execute" +
"\n seed = " + str(turtle.seed) +
"\n iterations = " + str(iterations))
result = lsys.iterate(instance, iterations)
print_time(start_time, "turtle interpreting")
object_base_pairs = turtle.interpret(result, context)
print_time(start_time, "turtle finished")
return object_base_pairs
def print_time(start_time, message):
elapsed = time.time() - start_time
print("%.5fs: %s" % (elapsed, message))