-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
306 lines (269 loc) · 9.53 KB
/
plot.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
import bpy
import mathutils
import math
from mathutils import Matrix
import time
import json
def deselect():
bpy.ops.object.select_all(action='DESELECT')
def selectObjects(objs):
bpy.ops.object.select_all(action='DESELECT')
for obj in objs:
objectToSelect = bpy.data.objects[obj.name]
objectToSelect.select_set(True)
bpy.context.view_layer.objects.active = objectToSelect
def selectObject(obj, add = False, recursive = False):
if not add:
bpy.ops.object.select_all(action='DESELECT')
objectToSelect = bpy.data.objects[obj.name]
objectToSelect.select_set(True)
bpy.context.view_layer.objects.active = objectToSelect
if recursive:
for child in obj.children:
selectObject(child, True, True)
def getAllChildrenArr(parentObjs):
children = []
for parent in parentObjs:
if len(parent.children):
children.extend(getAllChildrenArr([parent]))
return children
def getChildrenArr(parentObjs, depth):
children = []
for parent in parentObjs:
for child in parent.children:
if depth > 0:
children.extend( getChildren(child, depth-1) )
else:
children.append(child)
return children
def getChildren(parentObj, depth):
return getChildrenArr([parentObj],depth)
def getFamily(parentObjs):
part = []
if isinstance(parentObjs, list):
part.extend(parentObjs)
else:
part.append(parentObjs)
al = []
while len(part):
part = getChildrenArr(part,0)
al.extend(part)
return al
def getUpperObject(parentObj):
if not parentObj:
return None
print("**** Getting the uppermost of:"+str(parentObj.name))
mh = -20.0
upmost = None
parentUp = parentObj.matrix_world.translation[2]
print("Parent:"+str(parentObj.matrix_world.translation[2]))
for child in getChildren(parentObj, 0):
if child:
childUp = child.matrix_world.translation[2]
print("Candidate:"+str(child.matrix_world.translation[2]))
if (childUp - parentUp) > mh:
upmost = child
mh = childUp - parentUp
print("wins:"+str(upmost.matrix_world.translation))
return upmost
def deleteSelected():
bpy.ops.object.delete(True)
def delete(obj):
selectObject(obj, False, True)
bpy.ops.object.delete(True)
def selectAll():
bpy.ops.object.select_all(action='SELECT')
def deselect():
bpy.ops.object.select_all(action='DESELECT')
def selectObject(obj, add = False, recursive = False):
if not obj:
return
if not add:
bpy.ops.object.select_all(action='DESELECT')
objectToSelect = bpy.data.objects[obj.name]
objectToSelect.select_set(True)
bpy.context.view_layer.objects.active = objectToSelect
if recursive:
for child in obj.children:
selectObject(child, True, True)
def selectObjects(objs):
bpy.ops.object.select_all(action='DESELECT')
for obj in objs:
objectToSelect = bpy.data.objects[obj.name]
objectToSelect.select_set(True)
bpy.context.view_layer.objects.active = objectToSelect
def createCube(size):
bpy.ops.mesh.primitive_cube_add()
box = bpy.context.active_object
bpy.ops.transform.resize(value=(size, size, size))
return box
def createSphere(size):
bpy.ops.mesh.primitive_ico_sphere_add()
sphere = bpy.context.active_object
bpy.ops.transform.resize(value=(size, size, size))
#setattr(sphere, "fios", "0")
return sphere
def upit(what, amnt):
# one blender unit in x-direction
bpy.ops.transform.translate(value=(0.0, 0.0, amnt), orient_type='GLOBAL')
pass
def getUpperObject(parentObj):
if not parentObj:
return None
mh = -20.0
upmost = None
parentUp = parentObj.matrix_world.translation[2]
parentX = parentObj.matrix_world.translation[0]
for child in getChildren(parentObj, 0):
if child:
childUp = child.matrix_world.translation[2]
childX = child.matrix_world.translation[0]
if abs(childX-parentX)<0.01: #epsilon
if (childUp - parentUp) > mh:
upmost = child
mh = childUp - parentUp
if mh<0:
return None
return upmost
class solution():
def __init__(self, c, x, y):
self.c = c
self.x = x
self.y = y
self.step_size = c
self.ks = []
self.t_cnt = False
self.l_cnt = False
def activateT(self):
self.t_cnt = True
def activateL(self):
self.l_cnt = True
def createNode(self):
#deselect all objects
last_ob = createCube(4/self.c)
cube1 = last_ob
return cube1
def parent(self, a, b, n, l):
b.parent = a
b.location = (0, 25*(self.x*self.y), l)
b.matrix_parent_inverse = a.matrix_world.inverted()
a.rotation_euler = (0, a.rotation_euler[1] + math.radians(360/self.c), 0)
#a.fios += 1
def adan(self, object, limit = None):
if not object or not object.parent:
return object
ad = object
while ad and ad.parent and ad.parent!=limit:
ad = ad.parent
return ad
def initialStep(self):
createCube(4)
def rotate(self, offset = 0):
spheres = []
for j in range(1, 2):
selectAll()
toduplicate = bpy.context.selected_objects
distance = offset
sphNumber = len(self.ks)
sphere = createSphere(2.8 / (1+sphNumber))
sphere.name = 'sph_'+str(sphNumber)
if sphere:
spheres.append(sphere)
newgroup = self.adan(toduplicate[0])
self.parent(sphere, newgroup, 1, distance)
for _ in range(1, self.c):
selectObjects(toduplicate)
bpy.ops.object.duplicate()
if self.adan(bpy.context.selected_objects[0], sphere):
newgroup2 = self.adan(bpy.context.selected_objects[0], sphere)
#spheres.append(newgroup2)
newgroup2.location = (0, 0, 0)
self.parent(sphere, newgroup2, 1, distance)
return spheres
def step(self):
self.step_size *= 2.6 #+ c/9
return self.rotate(self.step_size)
def leafs(self, arr = None):
if not isinstance(arr, list):
arr = bpy.data.objects
if not arr:
return []
return [obj for obj in bpy.data.objects if obj.name.startswith("Cube")]
def build(self):
s.initialStep()
gp = None
gsl = self.x-2
print("\n\n\n*****************************")
print("Start C:" + str(self.c))
print(" x:" + str(self.x))
print(" y:" + str(self.y))
print(" v_jump:" + str(gsl))
print("*****************************")
for i in range(0, self.x*self.y):
d = {}
print("K:---------------->" + str(i))
d["l"] = 0
d["t"] = 0
d["_t"] = []
d["_tryT"] = []
d["pk"] = 0
line = i%(self.x-1)
column = i%(self.x)
newSpheres = s.step()
deselect()
for sph in newSpheres:
if self.l_cnt and (i%self.x<(self.x-1)):
print("applying l")
upsph = getUpperObject(sph)
delete(upsph)
d["l"] = 1
if self.ks:
d["l"] = self.ks[-1]["pk"]
print("\t\tleafs L:" + str(d["l"]))
if self.t_cnt and (i>=(self.x-1)) and (i<(self.x*self.y)-1):
print("applying t")
gp = s.adan(sph)
selectObject(gp)
grandsons = getChildren(gp, gsl)
aa = len(self.leafs())
for gs in grandsons:
if gs:
upgs = getUpperObject(gs)
if upgs:
a = len(self.leafs())
#d["_tryT"].append(upgs.name)
delete(upgs)
b = len(self.leafs())
d["_t"].append(a-b)
k_amm = len(self.ks)
try_tt = self.ks[k_amm-self.x]["pk"]
d["_tryT"].append(try_tt)
bb = len(self.leafs())
d["t"] += aa-bb
print("\t\tleafs T:" + str(d["t"]))
t = len(self.leafs())
print("\tK:" + str(t))
if self.ks:
d["pk"] = (self.ks[-1]["pk"] * self.c) + (- d["t"] - d["l"])
else:
d["pk"] = self.c - 1
d["R_k"] = len(self.leafs())
print("K-Dict:"+json.dumps(d, indent=2))
self.ks.append(d)
if column==(self.x-1):
print("\n\n\n\n\n\n")
cube_objs = [obj for obj in bpy.data.objects if obj.name.startswith("Cube")]
selectObjects(cube_objs)
print("*****************************")
print("Final k = " + str(len(cube_objs)))
print("*****************************\n\n")
x = 3
y = 2
c = 6
bpy.ops.object.select_all(action='SELECT')
deleteSelected()
bpy.ops.object.select_all(action='DESELECT')
s = solution(c, x, y)
s.activateT()
s.activateL()
s.build()