Update BubbleSort.py
This commit is contained in:
@@ -1,23 +1,27 @@
|
|||||||
import bpy
|
import bpy
|
||||||
import random
|
import random
|
||||||
from mathutils import Vector, Matrix
|
from mathutils import Vector, Matrix
|
||||||
|
|
||||||
#variables
|
#variables
|
||||||
count = 50
|
count = 50
|
||||||
cubes=[]
|
cubes=[]
|
||||||
locations=[]
|
|
||||||
scales=[]
|
|
||||||
#delete every existing node_group
|
#delete every existing node_group
|
||||||
for grp in bpy.data.node_groups:
|
for grp in bpy.data.node_groups:
|
||||||
bpy.data.node_groups.remove(grp)
|
bpy.data.node_groups.remove(grp)
|
||||||
|
|
||||||
#add counter object
|
#add counter object
|
||||||
bpy.ops.mesh.primitive_cube_add(location = (-2.5, 0, -3.375))
|
bpy.ops.mesh.primitive_cube_add(location = (-2.5, 0, -3.375))
|
||||||
bpy.context.active_object.name = 'Counter'
|
bpy.context.active_object.name = 'Counter'
|
||||||
|
|
||||||
#add geometry node modifier
|
#add geometry node modifier
|
||||||
bpy.ops.object.modifier_add(type='NODES')
|
bpy.ops.object.modifier_add(type='NODES')
|
||||||
|
|
||||||
#get, rename and clear node_group
|
#get, rename and clear node_group
|
||||||
node_grp = bpy.data.node_groups[-1]
|
node_grp = bpy.data.node_groups[-1]
|
||||||
node_grp.name = "Counter"
|
node_grp.name = "Counter"
|
||||||
node_grp.nodes.clear()
|
node_grp.nodes.clear()
|
||||||
|
|
||||||
#add nodes
|
#add nodes
|
||||||
stringToCurves = node_grp.nodes.new("GeometryNodeStringToCurves")
|
stringToCurves = node_grp.nodes.new("GeometryNodeStringToCurves")
|
||||||
fillCurve = node_grp.nodes.new("GeometryNodeFillCurve")
|
fillCurve = node_grp.nodes.new("GeometryNodeFillCurve")
|
||||||
@@ -28,12 +32,14 @@ counter1String = node_grp.nodes.new("FunctionNodeValueToString")
|
|||||||
arrayString = node_grp.nodes.new("FunctionNodeInputString")
|
arrayString = node_grp.nodes.new("FunctionNodeInputString")
|
||||||
counter2String = node_grp.nodes.new("FunctionNodeValueToString")
|
counter2String = node_grp.nodes.new("FunctionNodeValueToString")
|
||||||
groupOutput = node_grp.nodes.new('NodeGroupOutput')
|
groupOutput = node_grp.nodes.new('NodeGroupOutput')
|
||||||
|
|
||||||
#set default values of some nodes
|
#set default values of some nodes
|
||||||
transform.inputs[2].default_value[0] = 1.5708
|
transform.inputs[2].default_value[0] = 1.5708
|
||||||
comparisonString.string = "Comparisons:"
|
comparisonString.string = "Comparisons:"
|
||||||
arrayString.string = "Array Accesses:"
|
arrayString.string = "Array Accesses:"
|
||||||
stringToCurves.inputs[1].default_value = 2
|
stringToCurves.inputs[1].default_value = 2
|
||||||
joinStrings.inputs[0].default_value = " "
|
joinStrings.inputs[0].default_value = " "
|
||||||
|
|
||||||
#connect nodes to eachother
|
#connect nodes to eachother
|
||||||
node_grp.links.new(fillCurve.outputs[0], groupOutput.inputs[0])
|
node_grp.links.new(fillCurve.outputs[0], groupOutput.inputs[0])
|
||||||
node_grp.links.new(transform.outputs[0], fillCurve.inputs[0])
|
node_grp.links.new(transform.outputs[0], fillCurve.inputs[0])
|
||||||
@@ -43,15 +49,15 @@ node_grp.links.new(counter1String.outputs[0], joinStrings.inputs[1])
|
|||||||
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(comparisonString.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(counter2String.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(counter2String.outputs[0], joinStrings.inputs[1])
|
||||||
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
i = 1
|
locations = list(range(1,count+1))
|
||||||
while i < count+1:
|
scales = list(range(1,count+1))
|
||||||
locations.append(i)
|
|
||||||
scales.append(i)
|
|
||||||
i += 1
|
|
||||||
#randomize array order
|
#randomize array order
|
||||||
random.shuffle(locations)
|
random.shuffle(locations)
|
||||||
random.shuffle(scales)
|
random.shuffle(scales)
|
||||||
|
|
||||||
#sets origin of cube to bottom of mesh
|
#sets origin of cube to bottom of mesh
|
||||||
def origin_to_bottom(ob, matrix=Matrix()):
|
def origin_to_bottom(ob, matrix=Matrix()):
|
||||||
me = ob.data
|
me = ob.data
|
||||||
@@ -62,11 +68,11 @@ def origin_to_bottom(ob, matrix=Matrix()):
|
|||||||
o = matrix.inverted() @ o
|
o = matrix.inverted() @ o
|
||||||
me.transform(Matrix.Translation(-o))
|
me.transform(Matrix.Translation(-o))
|
||||||
mw.translation = mw @ o
|
mw.translation = mw @ o
|
||||||
|
|
||||||
#create cubes with random location
|
#create cubes with random location
|
||||||
i = 0
|
for i in range(count):
|
||||||
while i < count:
|
cube = bpy.ops.mesh.primitive_cube_add(location=(locations[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
|
|
||||||
#assign random scale to all cubes and add them to array
|
#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:
|
||||||
@@ -75,31 +81,41 @@ for ob in bpy.data.objects:
|
|||||||
ob.scale.z = scales[i]
|
ob.scale.z = scales[i]
|
||||||
cubes.append(ob)
|
cubes.append(ob)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
#insert keyframes on starting location for all cubes
|
#insert keyframes on starting location for all cubes
|
||||||
i=0
|
for i in range(count):
|
||||||
while i < count:
|
|
||||||
cubes[i].keyframe_insert(data_path="location", frame=0)
|
cubes[i].keyframe_insert(data_path="location", frame=0)
|
||||||
i += 1
|
|
||||||
#bubble sort
|
#bubble sort
|
||||||
n = len(cubes)
|
for i in range(count):
|
||||||
for i in range(n):
|
|
||||||
#insert keyframe for every cube on every frame
|
#insert keyframe for every cube on every frame
|
||||||
for cube in cubes:
|
for cube in cubes:
|
||||||
cube.keyframe_insert(data_path="location", frame=i)
|
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(count - i -1):
|
||||||
|
|
||||||
|
#add 1 to comparison counter
|
||||||
|
counter1String.inputs[0].default_value += 1
|
||||||
|
counter1String.inputs[0].keyframe_insert(data_path='default_value', frame=i)
|
||||||
|
|
||||||
|
#add 2 to array counter
|
||||||
|
counter2String.inputs[0].default_value += 2
|
||||||
|
counter2String.inputs[0].keyframe_insert(data_path='default_value', frame=i)
|
||||||
|
|
||||||
if cubes[j].scale.z > cubes[j + 1].scale.z:
|
if cubes[j].scale.z > cubes[j + 1].scale.z:
|
||||||
#add 1 to comparison counter
|
|
||||||
counter1String.inputs[0].default_value += 1
|
|
||||||
counter1String.inputs[0].keyframe_insert(data_path='default_value', frame=i)
|
|
||||||
#add 6 to array counter
|
|
||||||
counter2String.inputs[0].default_value += 6
|
|
||||||
counter2String.inputs[0].keyframe_insert(data_path='default_value', frame=i)
|
|
||||||
#change location & insert keyframes based on bubble sort
|
#change location & insert keyframes based on bubble sort
|
||||||
cubes[j].location.x = j
|
cubes[j].location.x = j
|
||||||
cubes[j].keyframe_insert(data_path="location", frame=i)
|
cubes[j].keyframe_insert(data_path="location", frame=i)
|
||||||
cubes[j+1].location.x = j-1
|
cubes[j+1].location.x = j-1
|
||||||
cubes[j+1].keyframe_insert(data_path="location", frame=i)
|
cubes[j+1].keyframe_insert(data_path="location", frame=i)
|
||||||
|
|
||||||
|
#add 4 to array counter
|
||||||
|
counter2String.inputs[0].default_value += 4
|
||||||
|
counter2String.inputs[0].keyframe_insert(data_path='default_value', frame=i)
|
||||||
|
|
||||||
#rearrange arrays
|
#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
|
||||||
|
|||||||
Reference in New Issue
Block a user