I'd like to be able to generate a bunch of primitive objects. All of them will be different sizes and have a material out of a random set.
What I'm having trouble with is the following:
- Creating 20,000 primitive objects quickly
- Creating a set of different materials where I can assign the primitive objects to
Here is my code so far:
import bpy, random
from random import randint
#Short hands for common calls
scene = bpy.context.scene
selected = bpy.context.selected_objects
mesh = bpy.ops.mesh
context = bpy.context
object = bpy.ops.object
selected_object = bpy.context.active_object
clouds = 500
def baseObject(x,y,z):
#Creates base cylinder
mesh.primitive_cylinder_add(location=(x,y,z))
return
def clearScene():
#Clears the current scene
for selectedObj in bpy.context.scene.objects:
selectedObj.select= True
object.delete()
return
def ObjectCreation(clouds):
for attempts in range(clouds):
x = round(random.uniform(-4.0, 4.0), 10)
y = round(random.uniform(-4.0, 4.0), 10)
z = round(random.uniform(-4.0, 4.0), 10)
baseObject(x,y,z)
context.object.scale[0] = x/10 #X Scale
context.object.scale[1] = y/10 #Y Scale
context.object.scale[2] = z/10 #Z Scale
materialCreation()
return
def materialCreation():
#for attempts in range(scale):
randomRed = random.uniform(0.0, 1.0) #Used for random color scale, need to adjust later for color mapping
randomGreen = random.uniform(0.0, 1.0)
randomBlue = random.uniform(0.0, 1.0)
material = bpy.data.materials.new("Material") #Material properties
material.alpha = round(random.uniform(0.1, 1.0), 10) #Opacity
material.use_transparency = True
context.object.show_transparent = True
context.object.data.materials.append(material) #Applies material properties
context.object.color = (randomRed, randomGreen, randomBlue, 1) #Changes objects color(RGB Opacity)
context.object.active_material.use_object_color = True
return
clearScene()
ObjectCreation(clouds)