Update BubbleSort.py

This commit is contained in:
ForeignGods
2022-05-11 19:10:38 +02:00
parent 0b426abb22
commit 602d974c17

View File

@@ -9,21 +9,16 @@ cubes=[]
#delete every existing object
for ob in bpy.data.objects:
bpy.data.objects.remove(ob)
#delete every existing node_group
for grp in bpy.data.node_groups:
bpy.data.node_groups.remove(grp)
#add counter object
bpy.ops.mesh.primitive_cube_add(location = (-2.5, 0, -3.375))
#add counter object, set position of counter object below other cube
bpy.ops.mesh.primitive_cube_add(location = (-2.5, 0, -3.375))
bpy.context.active_object.name = 'Counter'
#add geometry node modifier
bpy.ops.object.modifier_add(type='NODES')
#get, rename and clear node_group
#get and clear node_group
node_grp = bpy.data.node_groups[-1]
node_grp.name = "Counter"
node_grp.nodes.clear()
#add nodes
@@ -32,13 +27,14 @@ fillCurve = node_grp.nodes.new("GeometryNodeFillCurve")
transform = node_grp.nodes.new("GeometryNodeTransform")
joinStrings = node_grp.nodes.new("GeometryNodeStringJoin")
comparisonString = node_grp.nodes.new("FunctionNodeInputString")
counter1String = node_grp.nodes.new("FunctionNodeValueToString")
comparisonCounter = node_grp.nodes.new("FunctionNodeValueToString")
arrayString = node_grp.nodes.new("FunctionNodeInputString")
counter2String = node_grp.nodes.new("FunctionNodeValueToString")
arrayCounter = node_grp.nodes.new("FunctionNodeValueToString")
groupOutput = node_grp.nodes.new('NodeGroupOutput')
#set default values of some nodes
transform.inputs[2].default_value[0] = 1.5708
#90 degree rotation of the counter object
transform.inputs[2].default_value[0] = 1.5708 #
#set default values of some nodes.
comparisonString.string = "Comparisons:"
arrayString.string = "Array Accesses:"
stringToCurves.inputs[1].default_value = 2
@@ -49,18 +45,16 @@ node_grp.links.new(fillCurve.outputs[0], groupOutput.inputs[0])
node_grp.links.new(transform.outputs[0], fillCurve.inputs[0])
node_grp.links.new(stringToCurves.outputs[0], transform.inputs[0])
node_grp.links.new(joinStrings.outputs[0], stringToCurves.inputs[0])
node_grp.links.new(counter1String.outputs[0], joinStrings.inputs[1])
node_grp.links.new(comparisonCounter.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(arrayCounter.outputs[0], joinStrings.inputs[1])
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
#fill arrays with numbers between 1 & count
locations = list(range(1,count+1))
scales = list(range(1,count+1))
ran = list(range(1,count+1))
#randomize array order
random.shuffle(locations)
random.shuffle(scales)
random.shuffle(ran)
#sets origin of cube to bottom of mesh
def origin_to_bottom(ob, matrix=Matrix()):
@@ -75,21 +69,17 @@ def origin_to_bottom(ob, matrix=Matrix()):
#create cubes with random location
for i in range(count):
cube = bpy.ops.mesh.primitive_cube_add(location=(locations[i], 0, 0), scale=(0.25, 0.25, 0.25))
bpy.ops.mesh.primitive_cube_add(location=(ran[i], 0, 0), scale=(0.25, 0.25, 0.25))
#assign random scale to all cubes and add them to array
i = 0
for ob in bpy.data.objects:
if ob.type == 'MESH' and ob.name != "Counter":
origin_to_bottom(ob)
ob.scale.z = scales[i]
ob.scale.z = ran[i]
cubes.append(ob)
i += 1
#insert keyframes on starting location for all cubes
for i in range(count):
cubes[i].keyframe_insert(data_path="location", frame=0)
#bubble sort
for i in range(count):
@@ -101,12 +91,12 @@ for i in range(count):
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)
comparisonCounter.inputs[0].default_value += 1
comparisonCounter.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)
arrayCounter.inputs[0].default_value += 2
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=i)
if cubes[j].scale.z > cubes[j + 1].scale.z:
@@ -117,8 +107,8 @@ for i in range(count):
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)
arrayCounter.inputs[0].default_value += 4
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=i)
#rearrange arrays
cubes[j], cubes[j + 1] = cubes[j + 1], cubes[j]