Update insertion_sort_color.py
This commit is contained in:
parent
488811991c
commit
1c8f5a036a
@ -1,6 +1,8 @@
|
||||
import bpy
|
||||
import random
|
||||
import random
|
||||
import math
|
||||
from array import *
|
||||
from math import pi
|
||||
|
||||
############################################################
|
||||
# Insertion Sort Algorithm
|
||||
@ -30,8 +32,8 @@ def insertion_sort(arr, count):
|
||||
arr[j + 1] = arr[j]
|
||||
|
||||
#sets location
|
||||
arr[j + 1].location.x = j / 2
|
||||
arr[j].location.x = (j + 1) / 2
|
||||
arr[j + 1].location.x = j * 2
|
||||
arr[j].location.x = (j + 1) * 2
|
||||
|
||||
j -= 1
|
||||
|
||||
@ -39,16 +41,16 @@ def insertion_sort(arr, count):
|
||||
mat1 = arr[j].active_material.diffuse_color
|
||||
mat2 = key_item.active_material.diffuse_color
|
||||
|
||||
#adding keyframes to all cubes whenever one position/location is shifted
|
||||
for cube in arr:
|
||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||
#adding keyframes to all planes whenever one position/location is shifted
|
||||
for plane in arr:
|
||||
plane.keyframe_insert(data_path="location", frame=iframe)
|
||||
|
||||
#next frame
|
||||
iframe+=1
|
||||
|
||||
#place key_item into correct position/location
|
||||
arr[j + 1] = key_item
|
||||
arr[j + 1].location.x = i / 2
|
||||
arr[j + 1].location.x = i * 2
|
||||
|
||||
#origin and target index of key_item in array
|
||||
origin = i
|
||||
@ -57,7 +59,7 @@ def insertion_sort(arr, count):
|
||||
#set location/position for key_item + add keyframes
|
||||
x = 0
|
||||
while x <= (origin-target):
|
||||
key_item.location.x = (origin - x) / 2
|
||||
key_item.location.x = (origin - x) * 2
|
||||
key_item.keyframe_insert(data_path="location", frame= originFrame + x - 1)
|
||||
|
||||
x += 1
|
||||
@ -69,61 +71,54 @@ def insertion_sort(arr, count):
|
||||
############################################################
|
||||
|
||||
def setup_array(count):
|
||||
|
||||
#fill array with numbers between 0 & count - 1
|
||||
index = list(range(count))
|
||||
|
||||
#initialize 2d array
|
||||
Matrix = [[0 for x in range(count)] for y in range(count)]
|
||||
|
||||
#delete all materials
|
||||
for material in bpy.data.materials:
|
||||
material.user_clear()
|
||||
bpy.data.materials.remove(material)
|
||||
#initialize object and material array
|
||||
planes = [0 for i in range(count*count)]
|
||||
materials = [0 for i in range(count)]
|
||||
|
||||
#delete every existing object
|
||||
for ob in bpy.data.objects:
|
||||
bpy.data.objects.remove(ob)
|
||||
|
||||
#fill arrays with numbers between 1 & count
|
||||
index = list(range(count))
|
||||
|
||||
#randomize array order
|
||||
random.shuffle(index)
|
||||
|
||||
#initialize 2d array
|
||||
Matrix = [[0 for x in range(count)] for y in range(count)]
|
||||
|
||||
#add count * count cubes
|
||||
|
||||
#delete all existing materials
|
||||
for material in bpy.data.materials:
|
||||
bpy.data.materials.remove(material, do_unlink=True)
|
||||
|
||||
for i 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))
|
||||
|
||||
#assign random scale to all cubes and add them to array
|
||||
i = 0
|
||||
j = 0
|
||||
for ob in bpy.data.objects:
|
||||
if ob.type == 'MESH':
|
||||
if i == count:
|
||||
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)
|
||||
for x in range(count):
|
||||
for i in range(count):
|
||||
Matrix[x].remove(0)
|
||||
bpy.ops.mesh.primitive_plane_add(location=(j*2, 0, i*2), rotation=(pi / 2, 0, 0), scale=(0.1, 0.1, 0.1))
|
||||
|
||||
#presort arrayorder based on location
|
||||
for i in range(count):
|
||||
Matrix[i].sort(key = lambda obj: obj.location.x)
|
||||
|
||||
return (Matrix, count)
|
||||
i=0
|
||||
for ob in bpy.data.objects:
|
||||
planes[i]= ob
|
||||
i+=1
|
||||
|
||||
#sorts list of all objects based primary on their location.z and secondary on their location.x
|
||||
planes.sort(key = lambda obj: obj.location.z + obj.location.x/(count*count))
|
||||
|
||||
for i in range(count):
|
||||
material = bpy.data.materials.new(name="")
|
||||
material.diffuse_color = (index[i]/3, 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)
|
||||
|
||||
############################################################
|
||||
# Call Functions
|
||||
############################################################
|
||||
|
||||
#max count = 31 (needs to be fixed)
|
||||
Matrix, count = setup_array(31)
|
||||
|
||||
#insertion_sort every array
|
||||
|
Loading…
x
Reference in New Issue
Block a user