Ccreated file for testing recursion
This commit is contained in:
18
Trial.py
18
Trial.py
@@ -7,6 +7,15 @@ import random;
|
|||||||
size = 1;
|
size = 1;
|
||||||
# Setting the x, y, and z positions.
|
# Setting the x, y, and z positions.
|
||||||
x = y = z = size / 2;
|
x = y = z = size / 2;
|
||||||
|
# Setting the number of cubes in each coordinate.
|
||||||
|
xNum = 20;
|
||||||
|
yNum = 20;
|
||||||
|
zNum = 1;
|
||||||
|
# Setting the initial value for the number of cubes in each recursive call.
|
||||||
|
cubeCount = 0;
|
||||||
|
# Setting the maximum amount of cubes that is needed to be created.
|
||||||
|
numOfCubes = xNum * yNum * zNum;
|
||||||
|
print(numOfCubes);
|
||||||
|
|
||||||
#Function to clean the scene. This removes all of the objects, collections, materials, particles, textures, images, curves, meshes, actions, nodes, and worlds from the scene.
|
#Function to clean the scene. This removes all of the objects, collections, materials, particles, textures, images, curves, meshes, actions, nodes, and worlds from the scene.
|
||||||
def cleanScene():
|
def cleanScene():
|
||||||
@@ -27,14 +36,14 @@ def cleanScene():
|
|||||||
# Function to spawn the "ground" by creating several cubes using a nested for loop.
|
# Function to spawn the "ground" by creating several cubes using a nested for loop.
|
||||||
def spawnGround():
|
def spawnGround():
|
||||||
# Iterate over each grid "cell" we want a cube at.
|
# Iterate over each grid "cell" we want a cube at.
|
||||||
for x in range(20):
|
for x in range(xNum):
|
||||||
for y in range(20):
|
for y in range(yNum):
|
||||||
for z in range(1):
|
for z in range(zNum):
|
||||||
# Set the location.
|
# Set the location.
|
||||||
location = (x, y, z);
|
location = (x, y, z);
|
||||||
|
|
||||||
# Add the cubes.
|
# Add the cubes.
|
||||||
bpy.ops.mesh.primitive_cube_add(size = size, location = location, scale = (1, 1, 1));
|
bpy.ops.mesh.primitive_cube_add(size = size, location = location, scale = (size, size, size));
|
||||||
# Set the newly created cube as the active object.
|
# Set the newly created cube as the active object.
|
||||||
activeObject = bpy.context.active_object;
|
activeObject = bpy.context.active_object;
|
||||||
# Add a new material slot.
|
# Add a new material slot.
|
||||||
@@ -54,6 +63,7 @@ def spawnGround():
|
|||||||
cleanScene();
|
cleanScene();
|
||||||
spawnGround();
|
spawnGround();
|
||||||
|
|
||||||
|
|
||||||
# Notes:
|
# Notes:
|
||||||
# - For loop for the grid:
|
# - For loop for the grid:
|
||||||
# - x is for creating cubes in the x axis.
|
# - x is for creating cubes in the x axis.
|
||||||
|
84
TrialRecursive.py
Normal file
84
TrialRecursive.py
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import bpy;
|
||||||
|
|
||||||
|
# Variables:
|
||||||
|
# Size of each cube.
|
||||||
|
size = 1;
|
||||||
|
# Setting the x, y, and z positions.
|
||||||
|
x = y = z = size / 2;
|
||||||
|
# Setting the number of cubes in each coordinate.
|
||||||
|
xNum = 1;
|
||||||
|
yNum = 3;
|
||||||
|
zNum = 1;
|
||||||
|
# Setting the initial value for the number of cubes in each recursive call.
|
||||||
|
cubeCount = 0;
|
||||||
|
# Setting the maximum amount of cubes that is needed to be created.
|
||||||
|
numOfCubes = xNum * yNum * zNum;
|
||||||
|
|
||||||
|
#Function to clean the scene. This removes all of the objects, collections, materials, particles, textures, images, curves, meshes, actions, nodes, and worlds from the scene.
|
||||||
|
def cleanScene():
|
||||||
|
# Changes the mode to object mode if it is in Edit mode.
|
||||||
|
if (bpy.context.active_object and bpy.context.active_object.mode == "EDIT"):
|
||||||
|
bpy.ops.object.editmode_toggle();
|
||||||
|
|
||||||
|
# Checks for hidden stuff and unhides them.
|
||||||
|
for obj in bpy.data.objects:
|
||||||
|
obj.hide_set(False);
|
||||||
|
obj.hide_select = False;
|
||||||
|
obj.hide_viewport = False;
|
||||||
|
|
||||||
|
# Selects all the objects and then deletes.
|
||||||
|
bpy.ops.object.select_all(action = "SELECT");
|
||||||
|
bpy.ops.object.delete();
|
||||||
|
|
||||||
|
# Function to recursively spawn the cubes to create the ground.
|
||||||
|
def spawnGroundRecursively(count):
|
||||||
|
if count < numOfCubes:
|
||||||
|
location = (x, y, z);
|
||||||
|
bpy.ops.mesh.primitive_cube_add(size = size, location = location, scale = (size, size, size));
|
||||||
|
activeObjectRecursive = bpy.context.active_object;
|
||||||
|
# location += 1;
|
||||||
|
else:
|
||||||
|
print(count);
|
||||||
|
# x += 1;
|
||||||
|
# y += 1;
|
||||||
|
# z += 1;
|
||||||
|
location = (x + 1, y + 1, x + 1);
|
||||||
|
spawnGroundRecursively(count + 1);
|
||||||
|
|
||||||
|
# ChatGPT's method:
|
||||||
|
def addCube(x, y, z, size, material):
|
||||||
|
bpy.ops.mesh.primitive_cube_add(size=size, location=(x, y, z));
|
||||||
|
cube = bpy.context.active_object;
|
||||||
|
cube.data.materials.append(material);
|
||||||
|
|
||||||
|
# Set up material.
|
||||||
|
material = bpy.data.materials.new("Material")
|
||||||
|
material.use_nodes = True
|
||||||
|
materialNodes = material.node_tree.nodes
|
||||||
|
materialLinks = material.node_tree.links
|
||||||
|
materialNodes['Principled BSDF'].inputs['Base Color'].default_value = (1.0, 0.47, 1.0, 1.0)
|
||||||
|
|
||||||
|
# Generate the cubes recursively.
|
||||||
|
def addCubesRecursively(x, y, z, size, material, num_cubes):
|
||||||
|
if num_cubes == 0:
|
||||||
|
return
|
||||||
|
addCube(x, y, z, size, material);
|
||||||
|
for i in range(1, num_cubes):
|
||||||
|
addCubesRecursively(x + i*size, y, z, size, material, num_cubes - 1);
|
||||||
|
addCubesRecursively(x, y + i*size, z, size, material, num_cubes - 1);
|
||||||
|
addCubesRecursively(x, y, z + i*size, size, material, num_cubes - 1);
|
||||||
|
for j in range(1, num_cubes):
|
||||||
|
addCubesRecursively(x + i*size, y + j*size, z, size, material, num_cubes - 1);
|
||||||
|
addCubesRecursively(x + i*size, y, z + j*size, size, material, num_cubes - 1);
|
||||||
|
addCubesRecursively(x, y + i*size, z + j*size, size, material, num_cubes - 1);
|
||||||
|
for k in range(1, num_cubes):
|
||||||
|
addCubesRecursively(x + i*size, y + j*size, z + k*size, size, material, num_cubes - 1);
|
||||||
|
|
||||||
|
# Call the methods.
|
||||||
|
cleanScene();
|
||||||
|
# spawnGroundRecursively(cubeCount);
|
||||||
|
addCubesRecursively(x, y, z, size, material, max(xNum, yNum, zNum));
|
||||||
|
print('works lmao');
|
||||||
|
|
||||||
|
# Notes:
|
||||||
|
# - Not very fast. I need to impliment recursion in a better way.
|
Reference in New Issue
Block a user