Skip to content

Instantly share code, notes, and snippets.

@zmnv
Created April 20, 2021 20:54
Show Gist options
  • Save zmnv/d3cb10d25f3a89487c2f25743ae8fa01 to your computer and use it in GitHub Desktop.
Save zmnv/d3cb10d25f3a89487c2f25743ae8fa01 to your computer and use it in GitHub Desktop.
Blender generator
import bpy
from random import random as rnd
# Clean scene:
objs = bpy.data.objects
for e in objs:
objs.remove(e, do_unlink=True)
spacing = 34
offset = 160
center = (2 * offset * -1, 2 * offset * -1, 2 * offset * -1)
for x in range(20):
for y in range(20):
# Randomize:
lX = x * spacing
lY = y * spacing
lZ = rnd() * 480
location = (lX + center[0], lY + center[1], lZ - 210)
size = rnd() * 2
scale = (size, size, rnd() * 20)
# Draw object:
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=location, scale=scale)
# Randomize material for current object:
item = bpy.context.object
if rnd() < 0.33:
item.data.materials.append(bpy.data.materials["Material.002"])
elif rnd() > 0.33 and rnd() < 0.66:
item.data.materials.append(bpy.data.materials["Material.003"])
else:
# Default material:
item.data.materials.append(bpy.data.materials["Material.001"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment