From 2b17375c1fd288e4c41731c028256872e3e98d2b Mon Sep 17 00:00:00 2001 From: CJSatnarine Date: Thu, 30 Mar 2023 20:27:17 -0400 Subject: [PATCH] Ccreated file for testing recursion --- Trial.py | 18 +++++++--- TrialRecursive.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 TrialRecursive.py diff --git a/Trial.py b/Trial.py index fdb82a2..8291247 100644 --- a/Trial.py +++ b/Trial.py @@ -7,6 +7,15 @@ import random; size = 1; # Setting the x, y, and z positions. 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. def cleanScene(): @@ -27,14 +36,14 @@ def cleanScene(): # Function to spawn the "ground" by creating several cubes using a nested for loop. def spawnGround(): # Iterate over each grid "cell" we want a cube at. - for x in range(20): - for y in range(20): - for z in range(1): + for x in range(xNum): + for y in range(yNum): + for z in range(zNum): # Set the location. location = (x, y, z); # 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. activeObject = bpy.context.active_object; # Add a new material slot. @@ -54,6 +63,7 @@ def spawnGround(): cleanScene(); spawnGround(); + # Notes: # - For loop for the grid: # - x is for creating cubes in the x axis. diff --git a/TrialRecursive.py b/TrialRecursive.py new file mode 100644 index 0000000..ccdebd8 --- /dev/null +++ b/TrialRecursive.py @@ -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. \ No newline at end of file