Update bubble_sort_color.py

This commit is contained in:
ForeignGods
2022-06-07 21:08:10 +02:00
parent 1ed920de6d
commit 488811991c

View File

@@ -1,6 +1,8 @@
import bpy import bpy
import random import random
import math
from array import * from array import *
from math import pi
############################################################ ############################################################
# Bubble Sort Algorithm # Bubble Sort Algorithm
@@ -24,11 +26,11 @@ def bubble_sort(arr, count):
if mat1[0] > mat2[0]: if mat1[0] > mat2[0]:
#change location & insert keyframes based on bubble sort #change location & insert keyframes based on bubble sort
arr[j].location.x = (j+1)/2 arr[j].location.x = (j+1)*2
arr[j].keyframe_insert(data_path="location", frame=i) arr[j].keyframe_insert(data_path="location", frame=i+1)
arr[j+1].location.x = j/2 arr[j+1].location.x = j*2
arr[j+1].keyframe_insert(data_path="location", frame=i) arr[j+1].keyframe_insert(data_path="location", frame=i+1)
#rearrange arrays #rearrange arrays
arr[j], arr[j + 1] = arr[j + 1], arr[j] arr[j], arr[j + 1] = arr[j + 1], arr[j]
@@ -43,15 +45,15 @@ def bubble_sort(arr, count):
def setup_array(count): def setup_array(count):
#fill arrays with numbers between 0 & count #fill array with numbers between 0 & count - 1
index = list(range(count)) index = list(range(count))
#randomize array order
random.shuffle(index)
#initialize 2d array #initialize 2d array
h, w = count, count Matrix = [[0 for x in range(count)] for y in range(count)]
Matrix = [[0 for x in range(w)] for y in range(h)]
#initialize object and material array
planes = [0 for i in range(count*count)]
materials = [0 for i in range(count)]
#delete every existing object #delete every existing object
for ob in bpy.data.objects: for ob in bpy.data.objects:
@@ -59,37 +61,30 @@ def setup_array(count):
#delete all existing materials #delete all existing materials
for material in bpy.data.materials: for material in bpy.data.materials:
material.user_clear() bpy.data.materials.remove(material, do_unlink=True)
bpy.data.materials.remove(material)
#add count * count cubes
for i in range(count): for i in range(count):
for j in range(count): for j in range(count):
bpy.ops.mesh.primitive_cube_add(location=(j/2, 0, i/2), scale=(0.25, 0.25, 0.25)) bpy.ops.mesh.primitive_plane_add(location=(j*2, 0, i*2), rotation=(pi / 2, 0, 0), scale=(0.1, 0.1, 0.1))
#assign random colors to all cubes and add them to array i=0
i = 0
j = 0
for ob in bpy.data.objects: for ob in bpy.data.objects:
if ob.type == 'MESH': planes[i]= ob
if i == count: i+=1
j += 1
random.shuffle(index)
i = 0
mat = bpy.data.materials.new(name="MaterialName") #set new material to variable
mat.diffuse_color = (index[i], 255, 255, 255)
ob.data.materials.append(mat) #add the material to the object
Matrix[j].append(ob)
i += 1
#remove values that are 0 (needs to be solved beforehand) #sorts list of all objects based primary on their location.x and secondary on their location.z
for x in range(count): planes.sort(key = lambda obj: obj.location.z + obj.location.x/(count*count))
for i in range(count):
Matrix[x].remove(0)
#presort arrayorder based on location
for i in range(count): for i in range(count):
Matrix[i].sort(key = lambda obj: obj.location.x) material = bpy.data.materials.new(name="")
material.diffuse_color = (index[i], 255, 255, 255)
materials[i] = material
for i in range(count):
random.shuffle(materials)
for j in range(count):
planes[j+i*count].data.materials.append(materials[j]) #add the material to the object
Matrix[i][j] = planes[j+i*count]
return(Matrix, count) return(Matrix, count)
@@ -97,9 +92,7 @@ def setup_array(count):
# Call Functions # Call Functions
############################################################ ############################################################
#max count = 31 (needs to be fixed) Matrix, count = setup_array(50)
Matrix, count = setup_array(31)
#bubble_sort every array
for i in range(count): for i in range(count):
bubble_sort(Matrix[i], count) bubble_sort(Matrix[i], count)