renamed scripts + added folders

This commit is contained in:
ForeignGods
2022-06-03 17:45:21 +02:00
parent 9ce25f7e82
commit c5441e2a8a
7 changed files with 105 additions and 0 deletions

View File

@@ -0,0 +1,132 @@
import bpy
import random
from mathutils import Vector, Matrix
#variables
count = 50
cubes=[]
#delete every existing node_group
for grp in bpy.data.node_groups:
bpy.data.node_groups.remove(grp)
#delete every existing object
for ob in bpy.data.objects:
bpy.data.objects.remove(ob)
#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 and clear node_group
node_grp = bpy.data.node_groups[-1]
node_grp.nodes.clear()
#add nodes
stringToCurves = node_grp.nodes.new("GeometryNodeStringToCurves")
fillCurve = node_grp.nodes.new("GeometryNodeFillCurve")
transform = node_grp.nodes.new("GeometryNodeTransform")
joinStrings = node_grp.nodes.new("GeometryNodeStringJoin")
comparisonString = node_grp.nodes.new("FunctionNodeInputString")
comparisonCounter = node_grp.nodes.new("FunctionNodeValueToString")
arrayString = node_grp.nodes.new("FunctionNodeInputString")
arrayCounter = node_grp.nodes.new("FunctionNodeValueToString")
groupOutput = node_grp.nodes.new('NodeGroupOutput')
#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
joinStrings.inputs[0].default_value = " "
#connect nodes to eachother
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(comparisonCounter.outputs[0], joinStrings.inputs[1])
node_grp.links.new(comparisonString.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
ran = list(range(1,count+1))
#randomize array order
random.shuffle(ran)
#sets origin of cube to bottom of mesh
def origin_to_bottom(ob, matrix=Matrix()):
me = ob.data
mw = ob.matrix_world
local_verts = [matrix @ Vector(v[:]) for v in ob.bound_box]
o = sum(local_verts, Vector()) / 8
o.z = min(v.z for v in local_verts)
o = matrix.inverted() @ o
me.transform(Matrix.Translation(-o))
mw.translation = mw @ o
#create cubes with random location
for i in range(count):
bpy.ops.mesh.primitive_cube_add(location=(ran[i], 0, 0), scale=(0.25, 0.25, 0.25))
#shuffle array
random.shuffle(ran)
#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 = ran[i]
cubes.append(ob)
i += 1
#sort array based on location.x
cubes.sort(key = lambda obj: obj.location.x)
#bubble sort
for i in range(count):
#insert keyframe for every cube on every frame
for cube in cubes:
cube.keyframe_insert(data_path="location", frame=i)
already_sorted = True
for j in range(count - i -1):
#add 1 to comparison counter
comparisonCounter.inputs[0].default_value += 1
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=i)
#add 2 to array counter
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:
#change location & insert keyframes based on bubble sort
cubes[j].location.x = j
cubes[j].keyframe_insert(data_path="location", frame=i)
cubes[j+1].location.x = j-1
cubes[j+1].keyframe_insert(data_path="location", frame=i)
#add 4 to array counter
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]
already_sorted = False
if already_sorted:
break

View File

@@ -0,0 +1,181 @@
import bpy
import random
from mathutils import Vector, Matrix
############################################################
# Insertion Sort Algorithm
############################################################
def insertionSort(cubes, arrayCounter, comparisonCounter):
#start at frame 0
iframe=0
originFrame = 0
for i in range(0, len(cubes)):
#defines key_item that is compared until correct location
key_item = cubes[i]
key_item.location.x = i
j = i - 1
#add 2 to array counter
arrayCounter.inputs[0].default_value += 2
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
while j >= 0 and cubes[j].scale.z > key_item.scale.z:
#sets position of item in array
cubes[j + 1] = cubes[j]
#sets location
cubes[j + 1].location.x = j
cubes[j].location.x = j + 1
j -= 1
#adding keyframes to all cubes whenever one position/location is shifted
for cube in cubes:
cube.keyframe_insert(data_path="location", frame=iframe)
#add 2 to array counter
arrayCounter.inputs[0].default_value += 2
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
#add 1 to comparison counter
comparisonCounter.inputs[0].default_value += 1
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
#next frame
iframe+=1
#place key_item into correct position/location
cubes[j + 1] = key_item
cubes[j + 1].location.x = i
#add 1 to array counter
arrayCounter.inputs[0].default_value += 1
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
#origin and target index of key_item in array
origin = i
target = j + 1
#set location/position for key_item + add keyframes
x = 0
while x <= (origin-target):
key_item.location.x = origin - x
key_item.keyframe_insert(data_path="location", frame= originFrame + x - 1)
x += 1
originFrame = iframe
############################################################
# Setup Random Cubes + Array to be sorted
############################################################
def setUpCubeArray():
#variables
count = 50
cubes=[]
#delete every existing node_group
for grp in bpy.data.node_groups:
bpy.data.node_groups.remove(grp)
#delete every existing object
for ob in bpy.data.objects:
bpy.data.objects.remove(ob)
#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 and clear node_group
node_grp = bpy.data.node_groups[-1]
node_grp.nodes.clear()
#add nodes
stringToCurves = node_grp.nodes.new("GeometryNodeStringToCurves")
fillCurve = node_grp.nodes.new("GeometryNodeFillCurve")
transform = node_grp.nodes.new("GeometryNodeTransform")
joinStrings = node_grp.nodes.new("GeometryNodeStringJoin")
comparisonString = node_grp.nodes.new("FunctionNodeInputString")
comparisonCounter = node_grp.nodes.new("FunctionNodeValueToString")
arrayString = node_grp.nodes.new("FunctionNodeInputString")
arrayCounter = node_grp.nodes.new("FunctionNodeValueToString")
groupOutput = node_grp.nodes.new('NodeGroupOutput')
#90 degree rotation of the transform node of 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
joinStrings.inputs[0].default_value = " "
#connect nodes to eachother
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(comparisonCounter.outputs[0], joinStrings.inputs[1])
node_grp.links.new(comparisonString.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
ran = list(range(0,count-1))
#randomize array order
random.shuffle(ran)
#sets origin of cube to bottom of mesh
def origin_to_bottom(ob, matrix=Matrix()):
me = ob.data
mw = ob.matrix_world
local_verts = [matrix @ Vector(v[:]) for v in ob.bound_box]
o = sum(local_verts, Vector()) / 8
o.z = min(v.z for v in local_verts)
o = matrix.inverted() @ o
me.transform(Matrix.Translation(-o))
mw.translation = mw @ o
#create cubes with random location
for i in range(count-1):
bpy.ops.mesh.primitive_cube_add(location=(ran[i], 0, 0), scale=(0.25, 0.25, 0.25))
#shuffle array
random.shuffle(ran)
#assign random scale to all cubes and add them to array
s = 0
for ob in bpy.data.objects:
if ob.type == 'MESH' and ob.name != "Counter":
origin_to_bottom(ob)
ob.scale.z = ran[s]+1
cubes.append(ob)
s += 1
#sort array based on location.x
cubes.sort(key = lambda obj: obj.location.x)
return (cubes, arrayCounter, comparisonCounter)
############################################################
# Call Functions
############################################################
cubes, arrayCounter, comparisonCounter = setUpCubeArray()
insertionSort(cubes, arrayCounter, comparisonCounter)

View File

@@ -0,0 +1,148 @@
import bpy
import random
from mathutils import Vector, Matrix
#variables
count = 50
cubes=[]
#delete every existing node_group
for grp in bpy.data.node_groups:
bpy.data.node_groups.remove(grp)
#delete every existing object
for ob in bpy.data.objects:
bpy.data.objects.remove(ob)
#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 and clear node_group
node_grp = bpy.data.node_groups[-1]
node_grp.nodes.clear()
#add nodes
stringToCurves = node_grp.nodes.new("GeometryNodeStringToCurves")
fillCurve = node_grp.nodes.new("GeometryNodeFillCurve")
transform = node_grp.nodes.new("GeometryNodeTransform")
joinStrings = node_grp.nodes.new("GeometryNodeStringJoin")
comparisonString = node_grp.nodes.new("FunctionNodeInputString")
comparisonCounter = node_grp.nodes.new("FunctionNodeValueToString")
arrayString = node_grp.nodes.new("FunctionNodeInputString")
arrayCounter = node_grp.nodes.new("FunctionNodeValueToString")
groupOutput = node_grp.nodes.new('NodeGroupOutput')
#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
joinStrings.inputs[0].default_value = " "
#connect nodes to eachother
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(comparisonCounter.outputs[0], joinStrings.inputs[1])
node_grp.links.new(comparisonString.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
ran = list(range(1,count+1))
#randomize array order
random.shuffle(ran)
#sets origin of cube to bottom of mesh
def origin_to_bottom(ob, matrix=Matrix()):
me = ob.data
mw = ob.matrix_world
local_verts = [matrix @ Vector(v[:]) for v in ob.bound_box]
o = sum(local_verts, Vector()) / 8
o.z = min(v.z for v in local_verts)
o = matrix.inverted() @ o
me.transform(Matrix.Translation(-o))
mw.translation = mw @ o
#create cubes with random location
for i in range(count):
bpy.ops.mesh.primitive_cube_add(location=(ran[i], 0, 0), scale=(0.25, 0.25, 0.25))
#shuffle array
random.shuffle(ran)
#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 = ran[i]
cubes.append(ob)
i += 1
#sort array based on location.x
cubes.sort(key = lambda obj: obj.location.x)
def mergeSort(cubes):
if len(cubes) > 1:
mid = len(cubes) // 2
left = cubes[:mid]
right = cubes[mid:]
# Recursive call on each half
mergeSort(left)
mergeSort(right)
# Two iterators for traversing the two halves
i = 0
j = 0
# Iterator for the main list
k = 0
while i < len(left) and j < len(right):
if left[i].scale.z <= right[j].scale.z:
left[i].location.x = k
# The value from the left half has been used
cubes[k] = left[i]
# Move the iterator forward
i += 1
else:
right[j].location.x = k
cubes[k] = right[j]
j += 1
# Move to the next slot
k += 1
# For all the remaining values
while i < len(left):
left[i].location.x = k
cubes[k] = left[i]
i += 1
k += 1
while j < len(right):
right[j].location.x = k
cubes[k]=right[j]
j += 1
k += 1
#start recursion
mergeSort(cubes)

View File

@@ -0,0 +1,164 @@
from random import randint
import bpy
import random
from mathutils import Vector, Matrix
#variables
count = 50
cubes=[]
#delete every existing node_group
for grp in bpy.data.node_groups:
bpy.data.node_groups.remove(grp)
#delete every existing object
for ob in bpy.data.objects:
bpy.data.objects.remove(ob)
#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 and clear node_group
node_grp = bpy.data.node_groups[-1]
node_grp.nodes.clear()
#add nodes
stringToCurves = node_grp.nodes.new("GeometryNodeStringToCurves")
fillCurve = node_grp.nodes.new("GeometryNodeFillCurve")
transform = node_grp.nodes.new("GeometryNodeTransform")
joinStrings = node_grp.nodes.new("GeometryNodeStringJoin")
comparisonString = node_grp.nodes.new("FunctionNodeInputString")
comparisonCounter = node_grp.nodes.new("FunctionNodeValueToString")
arrayString = node_grp.nodes.new("FunctionNodeInputString")
arrayCounter = node_grp.nodes.new("FunctionNodeValueToString")
groupOutput = node_grp.nodes.new('NodeGroupOutput')
#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
joinStrings.inputs[0].default_value = " "
#connect nodes to eachother
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(comparisonCounter.outputs[0], joinStrings.inputs[1])
node_grp.links.new(comparisonString.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
ran = list(range(0,count))
#randomize array order
random.shuffle(ran)
#sets origin of cube to bottom of mesh
def origin_to_bottom(ob, matrix=Matrix()):
me = ob.data
mw = ob.matrix_world
local_verts = [matrix @ Vector(v[:]) for v in ob.bound_box]
o = sum(local_verts, Vector()) / 8
o.z = min(v.z for v in local_verts)
o = matrix.inverted() @ o
me.transform(Matrix.Translation(-o))
mw.translation = mw @ o
#create cubes with random location
for i in range(count):
bpy.ops.mesh.primitive_cube_add(location=(ran[i], 0, 0), scale=(0.25, 0.25, 0.25))
#shuffle array
random.shuffle(ran)
#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 = ran[i]+1
cubes.append(ob)
i += 1
#sort array based on location.x
cubes.sort(key = lambda obj: obj.location.x)
iframe=0
# function to find the partition position
def partition(array, low, high):
global iframe
#add 1 to array counter
arrayCounter.inputs[0].default_value += 1
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
# choose the rightmost element as pivot
pivot = array[(high + low) // 2]
# pointer for greater element
i = low
j = high
while True:
while array[i].scale.z < pivot.scale.z:
#add 1 to comparison counter
comparisonCounter.inputs[0].default_value += 1
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
i += 1
while array[j].scale.z > pivot.scale.z:
#add 1 to comparison counter
comparisonCounter.inputs[0].default_value += 1
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
j -= 1
if i >= j:
return j
else:
iframe += 1
for cube in cubes:
cube.keyframe_insert(data_path="location", frame=iframe)
array[i].location.x = j
array[j].location.x = i
array[i].keyframe_insert(data_path="location", frame=iframe)
array[j].keyframe_insert(data_path="location", frame=iframe)
#add 4 to array counter
arrayCounter.inputs[0].default_value += 4
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
# swapping element at i with element at j
array[i], array[j] = array[j], array[i]
# function to perform quicksort
def quickSort(array, low, high):
if low < high:
# find pivot element such that
# element smaller than pivot are on the left
# element greater than pivot are on the right
pi = partition(array, low, high)
# recursive call on the left of pivot
quickSort(array, low, pi)
# recursive call on the right of pivot
quickSort(array, pi + 1, high)
quickSort(cubes, 0, len(cubes) - 1)

View File

@@ -0,0 +1,121 @@
from random import randint
import bpy
import random
from mathutils import Vector, Matrix
#variables
count = 50
cubes=[]
#delete every existing node_group
for grp in bpy.data.node_groups:
bpy.data.node_groups.remove(grp)
#delete every existing object
for ob in bpy.data.objects:
bpy.data.objects.remove(ob)
#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 and clear node_group
node_grp = bpy.data.node_groups[-1]
node_grp.nodes.clear()
#add nodes
stringToCurves = node_grp.nodes.new("GeometryNodeStringToCurves")
fillCurve = node_grp.nodes.new("GeometryNodeFillCurve")
transform = node_grp.nodes.new("GeometryNodeTransform")
joinStrings = node_grp.nodes.new("GeometryNodeStringJoin")
comparisonString = node_grp.nodes.new("FunctionNodeInputString")
comparisonCounter = node_grp.nodes.new("FunctionNodeValueToString")
arrayString = node_grp.nodes.new("FunctionNodeInputString")
arrayCounter = node_grp.nodes.new("FunctionNodeValueToString")
groupOutput = node_grp.nodes.new('NodeGroupOutput')
#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
joinStrings.inputs[0].default_value = " "
#connect nodes to eachother
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(comparisonCounter.outputs[0], joinStrings.inputs[1])
node_grp.links.new(comparisonString.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
ran = list(range(0,count))
#randomize array order
random.shuffle(ran)
#sets origin of cube to bottom of mesh
def origin_to_bottom(ob, matrix=Matrix()):
me = ob.data
mw = ob.matrix_world
local_verts = [matrix @ Vector(v[:]) for v in ob.bound_box]
o = sum(local_verts, Vector()) / 8
o.z = min(v.z for v in local_verts)
o = matrix.inverted() @ o
me.transform(Matrix.Translation(-o))
mw.translation = mw @ o
#create cubes with random location
for i in range(count):
bpy.ops.mesh.primitive_cube_add(location=(ran[i], 0, 0), scale=(0.25, 0.25, 0.25))
#shuffle array
random.shuffle(ran)
#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 = ran[i]+1
cubes.append(ob)
i += 1
#sort array based on location.x
cubes.sort(key = lambda obj: obj.location.x)
iframe = 0
#Selection Sort ALgorithm
def selection_sort(cubes):
global iframe
for i in range(0, len(cubes)):
min_idx = i
for cube in cubes:
cube.keyframe_insert(data_path="location", frame= iframe)
for j in range(i , len(cubes)):
if cubes[min_idx].scale.z > cubes[j].scale.z:
min_idx = j
cubes[i].location.x = min_idx
cubes[min_idx].location.x = i
cubes[i].keyframe_insert(data_path="location", frame= iframe)
cubes[min_idx].keyframe_insert(data_path="location", frame= iframe)
iframe +=1
cubes[i], cubes[min_idx] = cubes[min_idx], cubes[i]
#Call Function
selection_sort(cubes)

View File

@@ -0,0 +1,150 @@
from random import randint
import bpy
import random
from mathutils import Vector, Matrix
#variables
count = 50
cubes=[]
#delete every existing node_group
for grp in bpy.data.node_groups:
bpy.data.node_groups.remove(grp)
#delete every existing object
for ob in bpy.data.objects:
bpy.data.objects.remove(ob)
#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 and clear node_group
node_grp = bpy.data.node_groups[-1]
node_grp.nodes.clear()
#add nodes
stringToCurves = node_grp.nodes.new("GeometryNodeStringToCurves")
fillCurve = node_grp.nodes.new("GeometryNodeFillCurve")
transform = node_grp.nodes.new("GeometryNodeTransform")
joinStrings = node_grp.nodes.new("GeometryNodeStringJoin")
comparisonString = node_grp.nodes.new("FunctionNodeInputString")
comparisonCounter = node_grp.nodes.new("FunctionNodeValueToString")
arrayString = node_grp.nodes.new("FunctionNodeInputString")
arrayCounter = node_grp.nodes.new("FunctionNodeValueToString")
groupOutput = node_grp.nodes.new('NodeGroupOutput')
#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
joinStrings.inputs[0].default_value = " "
#connect nodes to eachother
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(comparisonCounter.outputs[0], joinStrings.inputs[1])
node_grp.links.new(comparisonString.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
ran = list(range(0,count))
#randomize array order
random.shuffle(ran)
#sets origin of cube to bottom of mesh
def origin_to_bottom(ob, matrix=Matrix()):
me = ob.data
mw = ob.matrix_world
local_verts = [matrix @ Vector(v[:]) for v in ob.bound_box]
o = sum(local_verts, Vector()) / 8
o.z = min(v.z for v in local_verts)
o = matrix.inverted() @ o
me.transform(Matrix.Translation(-o))
mw.translation = mw @ o
#create cubes with random location
for i in range(count):
bpy.ops.mesh.primitive_cube_add(location=(ran[i], 0, 0), scale=(0.25, 0.25, 0.25))
#shuffle array
random.shuffle(ran)
#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 = ran[i]+1
cubes.append(ob)
i += 1
#sort array based on location.x
cubes.sort(key = lambda obj: obj.location.x)
iframe=0
#Shell Sort Algorithm
def shellSort(arr, n):
global iframe
gap=n//2
while gap>0:
j=gap
# Check the array in from left to right
# Till the last possible index of j
while j<n:
i=j-gap # This will keep help in maintain gap value
while i>=0:
for cube in arr:
cube.keyframe_insert(data_path="location", frame= iframe)
#add 1 to comparison counter
comparisonCounter.inputs[0].default_value += 1
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
#add 2 to array counter
arrayCounter.inputs[0].default_value += 2
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
# If value on right side is already greater than left side value
# We don't do swap else we swap
if arr[i+gap].scale.z > arr[i].scale.z:
break
else:
arr[i+gap].location.x = i
arr[i].location.x = i + gap
arr[i+gap].keyframe_insert(data_path="location", frame= iframe)
arr[i].keyframe_insert(data_path="location", frame= iframe)
iframe += 1
arr[i+gap],arr[i] = arr[i],arr[i+gap]
#add 4 to array counter
arrayCounter.inputs[0].default_value += 4
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
i=i-gap # To check left side also
# If the element present is greater than current element
j+=1
gap=gap//2
shellSort(cubes, count)