From e4735d048f699cae0f4b67937cb7d522d208319b Mon Sep 17 00:00:00 2001 From: CJSatnarine Date: Mon, 27 Mar 2023 11:48:56 -0400 Subject: [PATCH] Made the code have functions. --- Trial.py | 66 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/Trial.py b/Trial.py index 4e4f1c0..74c4a85 100644 --- a/Trial.py +++ b/Trial.py @@ -2,6 +2,13 @@ import bpy; import random; +# Variables: + +size = 1; # Size of each cube. + +x = y = z = size / 2; # Setting the x, y, and z positions. + + #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. @@ -18,39 +25,34 @@ def cleanScene(): bpy.ops.object.select_all(action = "SELECT"); bpy.ops.object.delete(); -# Calling the clean scene method. +# Iterate over each grid 'cell' we want a cube at. +def spawnGround(): + for x in range(20): + for y in range(20): + for z in range(1): + # Set the location. + location = (x, y, z); + + # Add the cubes. + bpy.ops.mesh.primitive_cube_add(size = size, location=location, scale=(1, 1, 1)); + # Set the newly created cube as the active object. + activeObject = bpy.context.active_object; + # Add a new material slot. + # bpy.ops.object.material_slot_add(); + # Creating a new material and assigning it to the active cube. + material = bpy.data.materials.new("Material"); + material.use_nodes = True; + materialNodes = material.node_tree.nodes; + materialLinks = material.node_tree.links; + + activeObject.data.materials.append(material); + + # Change the base colour. + materialNodes['Principled BSDF'].inputs['Base Color'].default_value = (1.0, 0.47, 1.0, 1.0); + +# Calls the functions: cleanScene(); - -# Size of each cube. -size = 1; - -# Setting the x, y, and z positions. -x = y = z = size / 2; - -# 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): - # Set the location. - location = (x, y, z); - - # Add the cubes. - bpy.ops.mesh.primitive_cube_add(size = size, location=location, scale=(1, 1, 1)); - # Set the newly created cube as the active object. - activeObject = bpy.context.active_object; - # Add a new material slot. - # bpy.ops.object.material_slot_add(); - # Creating a new material and assigning it to the active cube. - material = bpy.data.materials.new("Material"); - material.use_nodes = True; - materialNodes = material.node_tree.nodes; - materialLinks = material.node_tree.links; - - activeObject.data.materials.append(material); - - # Change the base colour. - materialNodes['Principled BSDF'].inputs['Base Color'].default_value = (1.0, 0.47, 1.0, 1.0) - +spawnGround(); # Notes: # - For loop for the grid: