Skip to content

Instantly share code, notes, and snippets.

@manoloide
Created April 8, 2019 18:55
Show Gist options
  • Save manoloide/752f74adc1e8566ff809e9e65993a8b5 to your computer and use it in GitHub Desktop.
Save manoloide/752f74adc1e8566ff809e9e65993a8b5 to your computer and use it in GitHub Desktop.
import bpy
import random
import math
# random seed
seed = random.randint(0, 99999);
def main():
# set seed
random.seed(seed)
# clear all
bpy.ops.object.select_all(action = "SELECT")
bpy.ops.object.delete(use_global = True);
camera = bpy.ops.object.camera_add(location=(0.0, 0.0, 6.2), rotation=(0, 0, 0))
rotX = 0 # random.uniform(-math.pi*0.1, +math.pi*0.1)
rotY = random.uniform(-math.pi*0.3, -math.pi*0.2)
bpy.ops.object.light_add(type='SUN', view_align=False, location=(0, 0, 0) ,rotation=(rotX, rotY, 0))
sun = bpy.context.active_object
sun.data.node_tree.nodes['Emission'].inputs['Strength'].default_value = 9
bpy.ops.object.light_add(type='SUN', view_align=False, location=(0, 0, 0))
sun2 = bpy.context.active_object
sun2.data.node_tree.nodes['Emission'].inputs['Strength'].default_value = 0.8
# sun.nodes.inputs[1].default_value = 5.0 # strength
bpy.ops.mesh.primitive_plane_add(location=(0,0,0))
bpy.ops.transform.resize(value=(8,8,8))
mat4 = bpy.data.materials.get("Mat4")
ob = bpy.context.active_object
if ob.data.materials:
ob.data.materials[0] = mat4
else:
ob.data.materials.append(mat4)
size = 2
cubes = []
depth = size*0.7
cubes.append(Cube(0, 0, depth, size, size, depth))
division = 90
for i in range(0, division):
ind = random.randint(0, int(len(cubes)*random.uniform(0.0, 0.7)))
c = cubes[ind] # random.choice(cubes);
w1 = random.uniform(c.w*0.2, c.w*0.8)
w2 = c.w-w1
h1 = random.uniform(c.h*0.2, c.h*0.8)
h2 = c.h-h1
d1 = random.uniform(c.d*0.2, c.d*0.8)
d2 = c.d-d1
cubes.append(Cube(c.x-c.w+w1, c.y-c.h+h1, c.z-c.d+d1, w1, h1, d1))
cubes.append(Cube(c.x+c.w-w2, c.y-c.h+h1, c.z-c.d+d1, w2, h1, d1))
cubes.append(Cube(c.x+c.w-w2, c.y+c.h-h2, c.z-c.d+d1, w2, h2, d1))
cubes.append(Cube(c.x-c.w+w1, c.y+c.h-h2, c.z-c.d+d1, w1, h2, d1))
cubes.remove(c)
mat1 = bpy.data.materials.get("Mat1")
mat2 = bpy.data.materials.get("Mat2") #bpy.ops.material.new()
mat3 = bpy.data.materials.get("Mat3") #bpy.ops.material.new()
for i in range(0, len(cubes)):
if random.uniform(0, 1) < 0.15:
continue
cube = cubes[i]
x = cube.x
y = cube.y
z = cube.z
w = cube.w-0.001
h = cube.h-0.001
d = cube.d-0.001
amp = 1
if random.uniform(0, 1) < 0.3:
amp = random.uniform(0, 0.5)
w *= amp
h *= amp
min = w
if h < min :
min = h
if d < min :
min = d
min *= 0.2
# mat1.inputs[0].default_value = (0,1,0,1)
cube1 = bpy.ops.mesh.primitive_cube_add(location=(x, y, z))
bpy.ops.transform.resize(value=(w, h, d))
mat = mat1
if random.uniform(0, 1) < 0.5 :
mat = mat2
if random.uniform(0, 1) < 0.7 :
mat = mat3
ob = bpy.context.active_object
if ob.data.materials:
ob.data.materials[0] = mat
else:
ob.data.materials.append(mat)
cube2 = bpy.ops.mesh.primitive_cube_add(location=(x, y, z+d+min))
bpy.ops.transform.resize(value=(min, min, min))
mat = mat1
if random.uniform(0, 1) < 0.5 :
mat = mat2
if random.uniform(0, 1) < 0.2:
mat = mat3
ob = bpy.context.active_object
if ob.data.materials:
ob.data.materials[0] = mat
else:
ob.data.materials.append(mat)
class Cube:
def __init__(self, x, y, z, w, h, d):
self.x = x
self.y = y
self.z = z
self.w = w
self.h = h
self.d = d
main()
@nbogie
Copy link

nbogie commented Apr 28, 2020

Thank you!

Here are some tiny tweaks to the light creation for blender 2.8
(because view_align is gone and setting strength is different)

    bpy.ops.object.light_add(type='SUN', location=(0, 0, 0) ,rotation=(rotX, rotY, 0))

    sun = bpy.context.active_object
    sun.data.angle = math.radians(10) #softer
    sun.data.energy = 9

    bpy.ops.object.light_add(type='SUN',  location=(0, 0, 0))
    sun2 = bpy.context.active_object
    sun2.data.energy = 0.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment