I am running a script that applies a loose neuroevolutionary algorithm to wind turbine fans in blender. Here is the file. Whenever I run the script blender runs it at roughly 50-60% of my CPU computing power, then jumps to 100%, and then closes. The same thing happens for my memory. I am running blender 2.8 on windows 10. What is going on?
def print_array(a, size=1): # prints cubes based on the array
#create an array with all of the units filled in
#not that relevant but is useful for under standing
count = a.shape[0] * a.shape[1] * a.shape[2]
bpy.ops.mesh.primitive_cube_add(size=size, location=(0, 0, 0))
cube = bpy.context.selected_objects[0]
#0:x-axis 1:y-Axis 2:z-Axis
for i in range(3):
mod = cube.modifiers.new('Array', 'ARRAY')
mod.relative_offset_displace[0] = 0
mod.relative_offset_displace[i] = 1.01
mod.count = a.shape[i]
bpy.ops.object.modifier_apply(modifier='Array')
bpy.ops.mesh.separate(type='LOOSE')
for i in bpy.data.objects:
bpy.data.objects[i.name].select_set(False)
#destroy the "zeros" in the array
indexes = np.where(a == 0)
print('found indexes')
#create a list of the zeros
zeros = []
for i in range(indexes[0].shape[0]):
zeros.append([indexes[0][i], indexes[1][i], indexes[2][i]])
#find the number of the index
objs = bpy.data.objects
for i in zeros:
print(i)
coor = i[2] * a.shape[0] * a.shape[1] + i[1] * a.shape[0] + i[0]
print('coor: ', coor)
if coor == 0:
string = "Cube"
elif len(str(coor)) == 1:
string = "Cube.00" + str(coor)
elif len(str(coor)) == 2:
string = "Cube.0" + str(coor)
else:
string = "Cube." + str(coor)
bpy.data.objects[string].select_set(True)
bpy.ops.object.delete()
count -= 1
return count
def update_array(array, values_changed):
shape = array.shape
values = [0, 1]
coordinates = []
for i in range(values_changed):
coordinates.append([random.randint(0, array.shape[0] - 1), random.randint(0, array.shape[1] - 1), random.randint(0, array.shape[2] - 1)])
coor = []
for x in coordinates:
if x not in coor:
coor.append(x)
for i in coor:
array[i[0], i[1], i[2]] = random.choice(values)
print(i[0], i[1], i[2])
return array
def train_on_generation(generation, folder_name, creatures, values_changed):
losses = []
os.chdir(folder_name)
if not os.path.exists('Generation %s' % generation):
os.mkdir('Generation %s' % generation)
os.chdir(folder_name + ('Generation %s' % generation))
if generation == 0:
for i in range(creatures):
np.save('creature %s' % i, update_array(np.ones((25, 250, 5)), values_changed))
creature_num = 0
for i in os.listdir(folder_name + ('Generation %s' % generation)):
print_array(np.load(i))
losses.append(0) # is an actual loss function in original code
for i in bpy.data.objects:
if not i.name[0] == 'F':
bpy.data.objects[i.name].select_set(True)
bpy.ops.object.delete() # deletes everything but the wind force fields
#get the highest loss and the creature_num
highest_loss = 0
for i in losses:
if i > highest_loss:
highest_loss = i
index = losses.index(highest_loss)
losses = []
#set the best array as creature_best
best = np.load(folder_name + 'Generation 0/' + ('creature %s' % index))
np.save(folder_name + 'Generation 0/creature_best', best)
else: # same thing as the top
for i in range(creatures):
np.save('creature %s' % creature_num, update_array(np.load(folder_name + ('Generation %s/' % (generation - 1)) + 'creature_best')), values_changed)
creature_num = 0
for i in os.listdir(folder_name + ('Generation %s' % generation)):
print_array(np.load(i))
losses.append(0)
for i in bpy.data.objects:
if not i.name[0] == 'F':
bpy.data.objects[i.name].select_set(True)
bpy.ops.object.delete() # deletes everything but the wind force fields
highest_loss = 0
for i in losses:
if i > highest_loss:
highest_loss = i
index = losses.index(highest_loss)
#set the best array as creature_best
best = np.load(folder_name + ('Generation %s/' % generation) + ('creature %s' % index))
np.save(folder_name + ('Generation %s/creature_best' % generation), best)
losses = []
filepath = '' # the folder to store the creatures
for i in range(10):
train_on_generation(i, filepath, 10, 10)
Sorry I couldn't shorten it more than that :/.
objs = scene.objectswhen working with context. Selecting objects inbpy.data.objectscollection has no relevance if not linked or to be linked to scene. – batFINGER Apr 27 '20 at 15:59