Update BubbleSort.py
This commit is contained in:
parent
2e52ee90db
commit
f8f35f18ec
@ -1,36 +1,51 @@
|
|||||||
import bpy
|
import bpy
|
||||||
import random
|
import random
|
||||||
|
#variables
|
||||||
count = 50
|
count = 50
|
||||||
cubes=[]
|
cubes=[]
|
||||||
locationList=[]
|
locations=[]
|
||||||
scaleList=[]
|
scales=[]
|
||||||
|
#fill arrays with numbers between 1 & count
|
||||||
i = 1
|
i = 1
|
||||||
while i < count+1:
|
while i < count+1:
|
||||||
locationList.append(i)
|
locations.append(i)
|
||||||
scaleList.append(i)
|
scales.append(i)
|
||||||
i += 1
|
i += 1
|
||||||
random.shuffle(locationList)
|
#randomize array order
|
||||||
random.shuffle(scaleList)
|
random.shuffle(locations)
|
||||||
|
random.shuffle(scales)
|
||||||
|
#create cubes with random location
|
||||||
i = 0
|
i = 0
|
||||||
while i < count:
|
while i < count:
|
||||||
cube = bpy.ops.mesh.primitive_cube_add(location=(locationList[i], 0, 0), scale=(0.25, 0.25, 0.25))
|
cube = bpy.ops.mesh.primitive_cube_add(location=(locations[i], 0, 0), scale=(0.25, 0.25, 0.25))
|
||||||
i+=1
|
i+=1
|
||||||
|
#assign random scale to all cubes and add them to array
|
||||||
i = 0
|
i = 0
|
||||||
for ob in bpy.data.objects:
|
for ob in bpy.data.objects:
|
||||||
if ob.type == 'MESH':
|
if ob.type == 'MESH':
|
||||||
ob.scale.z = scaleList[i]
|
ob.scale.z = scales[i]
|
||||||
cubes.append(ob)
|
cubes.append(ob)
|
||||||
i += 1
|
i += 1
|
||||||
|
#insert keyframes on starting location for all cubes
|
||||||
|
i=0
|
||||||
|
while i < count:
|
||||||
|
cubes[i].keyframe_insert(data_path="location", frame=0)
|
||||||
|
i += 1
|
||||||
|
#bubble sort
|
||||||
n = len(cubes)
|
n = len(cubes)
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
|
#insert keyframe for every cube on every frame
|
||||||
|
for cube in cubes:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=i)
|
||||||
already_sorted = True
|
already_sorted = True
|
||||||
for j in range(n - i - 1):
|
for j in range(n - i - 1):
|
||||||
if cubes[j].scale.z > cubes[j + 1].scale.z:
|
if cubes[j].scale.z > cubes[j + 1].scale.z:
|
||||||
|
#insert keyframes based on bubble sort
|
||||||
cubes[j].location = (j, 0.0, 0.0)
|
cubes[j].location = (j, 0.0, 0.0)
|
||||||
cubes[j].keyframe_insert(data_path="location", frame=i)
|
cubes[j].keyframe_insert(data_path="location", frame=i)
|
||||||
cubes[j+1].location = (j-1, 0.0, 0.0)
|
cubes[j+1].location = (j-1, 0.0, 0.0)
|
||||||
cubes[j+1].keyframe_insert(data_path="location", frame=i)
|
cubes[j+1].keyframe_insert(data_path="location", frame=i)
|
||||||
|
#rearrange arrays
|
||||||
cubes[j], cubes[j + 1] = cubes[j + 1], cubes[j]
|
cubes[j], cubes[j + 1] = cubes[j + 1], cubes[j]
|
||||||
already_sorted = False
|
already_sorted = False
|
||||||
if already_sorted:
|
if already_sorted:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user