organizing into functions
This commit is contained in:
parent
253a3a0da4
commit
83c2acd0cc
@ -2,102 +2,18 @@ import bpy
|
|||||||
import random
|
import random
|
||||||
from mathutils import Vector, Matrix
|
from mathutils import Vector, Matrix
|
||||||
|
|
||||||
#variables
|
############################################################
|
||||||
count = 50
|
# Bubble Sort Algorithm
|
||||||
cubes=[]
|
############################################################
|
||||||
|
|
||||||
#delete every existing node_group
|
def bubble_sort(cubes, arrayCounter, comparisonCounter):
|
||||||
for grp in bpy.data.node_groups:
|
for i in range(len(cubes)-1):
|
||||||
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
|
#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(count - i -1):
|
for j in range(len(cubes) - i -1):
|
||||||
|
|
||||||
#add 1 to comparison counter
|
#add 1 to comparison counter
|
||||||
comparisonCounter.inputs[0].default_value += 1
|
comparisonCounter.inputs[0].default_value += 1
|
||||||
@ -125,8 +41,106 @@ for i in range(count):
|
|||||||
if already_sorted:
|
if already_sorted:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
############################################################
|
||||||
|
# Setup Random Cubes + Array to be sorted
|
||||||
|
############################################################
|
||||||
|
|
||||||
|
def setup_array(count):
|
||||||
|
|
||||||
|
#initialize array
|
||||||
|
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 = setup_array(20)
|
||||||
|
|
||||||
|
bubble_sort(cubes, arrayCounter, comparisonCounter)
|
||||||
@ -6,7 +6,7 @@ from mathutils import Vector, Matrix
|
|||||||
# Insertion Sort Algorithm
|
# Insertion Sort Algorithm
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
def insertionSort(cubes, arrayCounter, comparisonCounter):
|
def insertion_sort(cubes, arrayCounter, comparisonCounter):
|
||||||
#start at frame 0
|
#start at frame 0
|
||||||
iframe=0
|
iframe=0
|
||||||
originFrame = 0
|
originFrame = 0
|
||||||
@ -18,8 +18,8 @@ def insertionSort(cubes, arrayCounter, comparisonCounter):
|
|||||||
|
|
||||||
j = i - 1
|
j = i - 1
|
||||||
|
|
||||||
#add 2 to array counter
|
#add 1 to array counter
|
||||||
arrayCounter.inputs[0].default_value += 2
|
arrayCounter.inputs[0].default_value += 1
|
||||||
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
while j >= 0 and cubes[j].scale.z > key_item.scale.z:
|
while j >= 0 and cubes[j].scale.z > key_item.scale.z:
|
||||||
@ -36,8 +36,8 @@ def insertionSort(cubes, arrayCounter, comparisonCounter):
|
|||||||
for cube in cubes:
|
for cube in cubes:
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
|
||||||
#add 2 to array counter
|
#add 3 to array counter
|
||||||
arrayCounter.inputs[0].default_value += 2
|
arrayCounter.inputs[0].default_value += 3
|
||||||
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
#add 1 to comparison counter
|
#add 1 to comparison counter
|
||||||
@ -73,10 +73,9 @@ def insertionSort(cubes, arrayCounter, comparisonCounter):
|
|||||||
# Setup Random Cubes + Array to be sorted
|
# Setup Random Cubes + Array to be sorted
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
def setUpCubeArray():
|
def setup_array(count):
|
||||||
|
|
||||||
#variables
|
#initialize array
|
||||||
count = 50
|
|
||||||
cubes=[]
|
cubes=[]
|
||||||
|
|
||||||
#delete every existing node_group
|
#delete every existing node_group
|
||||||
@ -170,12 +169,6 @@ def setUpCubeArray():
|
|||||||
# Call Functions
|
# Call Functions
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
cubes, arrayCounter, comparisonCounter = setUpCubeArray()
|
cubes, arrayCounter, comparisonCounter = setup_array(12)
|
||||||
|
|
||||||
insertionSort(cubes, arrayCounter, comparisonCounter)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
insertion_sort(cubes, arrayCounter, comparisonCounter)
|
||||||
|
|||||||
@ -1,137 +1,77 @@
|
|||||||
from random import randint
|
|
||||||
import bpy
|
import bpy
|
||||||
import random
|
import random
|
||||||
from mathutils import Vector, Matrix
|
from mathutils import Vector, Matrix
|
||||||
|
|
||||||
#variables
|
############################################################
|
||||||
count = 150
|
# Merge Sort Algorithm
|
||||||
cubes=[]
|
############################################################
|
||||||
|
|
||||||
#delete every existing node_group
|
def merge(arr, l, m, r, arrayCounter, comparisonCounter):
|
||||||
for grp in bpy.data.node_groups:
|
|
||||||
bpy.data.node_groups.remove(grp)
|
|
||||||
|
|
||||||
#delete every existing object
|
global cubes
|
||||||
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)
|
|
||||||
|
|
||||||
def merge(arr, l, m, r):
|
|
||||||
|
|
||||||
global array
|
|
||||||
global iframe
|
global iframe
|
||||||
|
|
||||||
|
|
||||||
n1 = m - l + 1
|
n1 = m - l + 1
|
||||||
n2 = r - m
|
n2 = r - m
|
||||||
|
|
||||||
# create temp arrays
|
#create temp arrays
|
||||||
L = [0] * (n1)
|
L = [0] * (n1)
|
||||||
R = [0] * (n2)
|
R = [0] * (n2)
|
||||||
|
|
||||||
# Copy data to temp arrays L[] and R[]
|
#add 2 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
|
#copy data to temp arrays L[] and R[]
|
||||||
for i in range(0, n1):
|
for i in range(0, n1):
|
||||||
|
|
||||||
|
#add 2 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
L[i] = arr[l + i]
|
L[i] = arr[l + i]
|
||||||
|
|
||||||
for j in range(0, n2):
|
for j in range(0, n2):
|
||||||
|
#add 2 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
R[j] = arr[m + 1 + j]
|
R[j] = arr[m + 1 + j]
|
||||||
|
|
||||||
# Merge the temp arrays back into arr[l..r]
|
#merge the temp arrays back into arr[l..r]
|
||||||
i = 0 # Initial index of first subarray
|
i = 0 #initial index of first subarray
|
||||||
j = 0 # Initial index of second subarray
|
j = 0 #initial index of second subarray
|
||||||
k = l # Initial index of merged subarray
|
k = l #initial index of merged subarray
|
||||||
|
|
||||||
while i < n1 and j < n2:
|
while i < n1 and j < n2:
|
||||||
|
#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)
|
||||||
if L[i].scale.z <= R[j].scale.z:
|
if L[i].scale.z <= R[j].scale.z:
|
||||||
arr[k] = L[i]
|
arr[k] = L[i]
|
||||||
|
|
||||||
L[i].location.x = k
|
L[i].location.x = k
|
||||||
|
|
||||||
|
#add 2 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
else:
|
else:
|
||||||
arr[k] = R[j]
|
arr[k] = R[j]
|
||||||
|
|
||||||
R[j].location.x = k
|
R[j].location.x = k
|
||||||
|
|
||||||
|
#add 3 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
j += 1
|
j += 1
|
||||||
k += 1
|
k += 1
|
||||||
|
|
||||||
for cube in array:
|
for cube in cubes:
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
for cube in L:
|
for cube in L:
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
@ -140,14 +80,18 @@ def merge(arr, l, m, r):
|
|||||||
|
|
||||||
iframe += 1
|
iframe += 1
|
||||||
|
|
||||||
# Copy the remaining elements of L[], if there
|
#copy the remaining elements of L[], if there are any
|
||||||
# are any
|
|
||||||
while i < n1:
|
while i < n1:
|
||||||
arr[k] = L[i]
|
arr[k] = L[i]
|
||||||
|
|
||||||
L[i].location.x = k
|
L[i].location.x = k
|
||||||
|
|
||||||
|
#add 2 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
x=0
|
x=0
|
||||||
for cube in array:
|
for cube in cubes:
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
for cube in L:
|
for cube in L:
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
@ -158,13 +102,17 @@ def merge(arr, l, m, r):
|
|||||||
i += 1
|
i += 1
|
||||||
k += 1
|
k += 1
|
||||||
|
|
||||||
# Copy the remaining elements of R[], if there
|
#copy the remaining elements of R[], if there are any
|
||||||
# are any
|
|
||||||
while j < n2:
|
while j < n2:
|
||||||
arr[k] = R[j]
|
arr[k] = R[j]
|
||||||
|
|
||||||
R[j].location.x = k
|
R[j].location.x = k
|
||||||
for cube in array:
|
|
||||||
|
#add 2 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
|
for cube in cubes:
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
for cube in L:
|
for cube in L:
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
@ -174,20 +122,120 @@ def merge(arr, l, m, r):
|
|||||||
|
|
||||||
j += 1
|
j += 1
|
||||||
k += 1
|
k += 1
|
||||||
# l is for left index and r is right index of the
|
|
||||||
# sub-array of arr to be sorted
|
|
||||||
|
|
||||||
def mergeSort(arr, l, r):
|
#l is for left index and r is right index of the sub-array of arr to be sorted
|
||||||
|
def merge_sort(arr, l, r, arrayCounter, comparisonCounter):
|
||||||
if l < r:
|
if l < r:
|
||||||
|
|
||||||
# Same as (l+r)//2, but avoids overflow for
|
#same as (l+r)//2, but avoids overflow for large l and h
|
||||||
# large l and h
|
|
||||||
m = l+(r-l)//2
|
m = l+(r-l)//2
|
||||||
# Sort first and second halves
|
|
||||||
mergeSort(arr, l, m)
|
#sort first and second halves
|
||||||
mergeSort(arr, m+1, r)
|
merge_sort(arr, l, m, arrayCounter, comparisonCounter)
|
||||||
merge(arr, l, m, r)
|
merge_sort(arr, m+1, r, arrayCounter, comparisonCounter)
|
||||||
|
merge(arr, l, m, r, arrayCounter, comparisonCounter)
|
||||||
|
|
||||||
|
############################################################
|
||||||
|
# Setup Random Cubes + Array to be sorted
|
||||||
|
############################################################
|
||||||
|
|
||||||
|
def setup_array(count):
|
||||||
|
|
||||||
|
#initialize array
|
||||||
|
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 = setup_array(20)
|
||||||
|
|
||||||
iframe = 0
|
iframe = 0
|
||||||
array = cubes
|
merge_sort(cubes, 0, len(cubes)-1, arrayCounter, comparisonCounter)
|
||||||
mergeSort(cubes, 0, count-1)
|
|
||||||
@ -1,101 +1,13 @@
|
|||||||
from random import randint
|
|
||||||
|
|
||||||
import bpy
|
import bpy
|
||||||
import random
|
import random
|
||||||
from mathutils import Vector, Matrix
|
from mathutils import Vector, Matrix
|
||||||
|
|
||||||
#variables
|
############################################################
|
||||||
count = 50
|
# Quick Sort Algorithm
|
||||||
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
|
# function to find the partition position
|
||||||
def partition(array, low, high):
|
def partition(array, low, high, arrayCounter, comparisonCounter):
|
||||||
|
|
||||||
global iframe
|
global iframe
|
||||||
|
|
||||||
@ -147,18 +59,120 @@ def partition(array, low, high):
|
|||||||
array[i], array[j] = array[j], array[i]
|
array[i], array[j] = array[j], array[i]
|
||||||
|
|
||||||
# function to perform quicksort
|
# function to perform quicksort
|
||||||
def quickSort(array, low, high):
|
def quick_sort(array, low, high, arrayCounter, comparisonCounter):
|
||||||
if low < high:
|
if low < high:
|
||||||
# find pivot element such that
|
# find pivot element such that
|
||||||
# element smaller than pivot are on the left
|
# element smaller than pivot are on the left
|
||||||
# element greater than pivot are on the right
|
# element greater than pivot are on the right
|
||||||
pi = partition(array, low, high)
|
pi = partition(array, low, high, arrayCounter, comparisonCounter)
|
||||||
|
|
||||||
# recursive call on the left of pivot
|
# recursive call on the left of pivot
|
||||||
quickSort(array, low, pi)
|
quick_sort(array, low, pi, arrayCounter, comparisonCounter)
|
||||||
|
|
||||||
# recursive call on the right of pivot
|
# recursive call on the right of pivot
|
||||||
quickSort(array, pi + 1, high)
|
quick_sort(array, pi + 1, high, arrayCounter, comparisonCounter)
|
||||||
|
|
||||||
|
############################################################
|
||||||
|
# Setup Random Cubes + Array to be sorted
|
||||||
|
############################################################
|
||||||
|
|
||||||
quickSort(cubes, 0, len(cubes) - 1)
|
def setup_array(count):
|
||||||
|
|
||||||
|
#initialize array
|
||||||
|
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 = setup_array(20)
|
||||||
|
|
||||||
|
iframe = 0
|
||||||
|
quick_sort(cubes, 0, len(cubes) - 1, arrayCounter, comparisonCounter)
|
||||||
@ -1,100 +1,12 @@
|
|||||||
from random import randint
|
|
||||||
|
|
||||||
import bpy
|
import bpy
|
||||||
import random
|
import random
|
||||||
from mathutils import Vector, Matrix
|
from mathutils import Vector, Matrix
|
||||||
|
|
||||||
#variables
|
############################################################
|
||||||
count = 50
|
# Selection Sort Algorithm
|
||||||
cubes=[]
|
############################################################
|
||||||
|
|
||||||
#delete every existing node_group
|
def selection_sort(cubes, arrayCounter, comparisonCounter):
|
||||||
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
|
global iframe
|
||||||
|
|
||||||
@ -105,6 +17,15 @@ def selection_sort(cubes):
|
|||||||
cube.keyframe_insert(data_path="location", frame= iframe)
|
cube.keyframe_insert(data_path="location", frame= iframe)
|
||||||
|
|
||||||
for j in range(i , len(cubes)):
|
for j in range(i , len(cubes)):
|
||||||
|
|
||||||
|
#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)
|
||||||
|
|
||||||
if cubes[min_idx].scale.z > cubes[j].scale.z:
|
if cubes[min_idx].scale.z > cubes[j].scale.z:
|
||||||
min_idx = j
|
min_idx = j
|
||||||
|
|
||||||
@ -115,7 +36,112 @@ def selection_sort(cubes):
|
|||||||
cubes[min_idx].keyframe_insert(data_path="location", frame= iframe)
|
cubes[min_idx].keyframe_insert(data_path="location", frame= iframe)
|
||||||
iframe +=1
|
iframe +=1
|
||||||
|
|
||||||
|
#add 4 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 4
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
cubes[i], cubes[min_idx] = cubes[min_idx], cubes[i]
|
cubes[i], cubes[min_idx] = cubes[min_idx], cubes[i]
|
||||||
|
|
||||||
#Call Function
|
############################################################
|
||||||
selection_sort(cubes)
|
# Setup Random Cubes + Array to be sorted
|
||||||
|
############################################################
|
||||||
|
|
||||||
|
def setup_array(count):
|
||||||
|
|
||||||
|
#initialize array
|
||||||
|
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 = setup_array(20)
|
||||||
|
|
||||||
|
iframe = 0
|
||||||
|
selection_sort(cubes, arrayCounter, comparisonCounter)
|
||||||
@ -1,101 +1,12 @@
|
|||||||
from random import randint
|
|
||||||
|
|
||||||
import bpy
|
import bpy
|
||||||
import random
|
import random
|
||||||
from mathutils import Vector, Matrix
|
from mathutils import Vector, Matrix
|
||||||
|
|
||||||
#variables
|
############################################################
|
||||||
count = 50
|
# Shell Sort Algorithm
|
||||||
cubes=[]
|
############################################################
|
||||||
|
|
||||||
#delete every existing node_group
|
def shellSort(arr, n, arrayCounter, comparisonCounter):
|
||||||
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
|
global iframe
|
||||||
|
|
||||||
@ -147,4 +58,107 @@ def shellSort(arr, n):
|
|||||||
j+=1
|
j+=1
|
||||||
gap=gap//2
|
gap=gap//2
|
||||||
|
|
||||||
shellSort(cubes, count)
|
############################################################
|
||||||
|
# Setup Random Cubes + Array to be sorted
|
||||||
|
############################################################
|
||||||
|
|
||||||
|
def setup_array(count):
|
||||||
|
|
||||||
|
#initialize array
|
||||||
|
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 = setup_array(20)
|
||||||
|
|
||||||
|
iframe = 0
|
||||||
|
shellSort(cubes, len(cubes), arrayCounter, comparisonCounter)
|
||||||
Loading…
x
Reference in New Issue
Block a user