Files
Ornament-Generator-RNG/voxel_toys/voxel_toys.py
T
2026-11-16 23:50:40 +03:00

526 lines
25 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Deluxe voxel toy set — procedural Blender build (Blender 4.2+/5.x, Eevee).
# Run inside Blender (Text Editor > Run Script) or via the Blender MCP:
# exec(open(r"C:\roblox\Ornament Generator RNG\voxel_toys\voxel_toys.py", encoding="utf-8").read())
#
# Tweak colours in PALETTE and glow strengths in GLOW at the top; re-run to rebuild everything.
import bpy, bmesh, math, os
from mathutils import Matrix, Vector
OUT_DIR = r"C:\roblox\Ornament Generator RNG\voxel_toys"
SCENE_NAME = "Voxel_Toys"
SPACING = 0.26 # metres between toys in the row
BEVEL = 0.0010 # bevel width (m) — small, toy-like edges
RIM = 0.35 # global fresnel rim-glow multiplier
PALETTE = { # linear RGB
"wood": (0.42, 0.18, 0.06),
"wood_light": (0.62, 0.32, 0.11),
"iron": (0.34, 0.37, 0.44),
"gold": (1.00, 0.66, 0.16),
"creature": (0.10, 0.62, 0.38),
"creature_dk": (0.04, 0.36, 0.22),
"belly": (0.72, 0.95, 0.55),
"ivory": (1.00, 0.90, 0.68),
"spike": (1.00, 0.38, 0.06),
"blush": (1.00, 0.35, 0.50),
"ink": (0.02, 0.02, 0.03),
"stone": (0.30, 0.31, 0.38),
"stone_light": (0.52, 0.54, 0.62),
"steel": (0.70, 0.76, 0.86),
"steel_dark": (0.38, 0.43, 0.55),
"steel_edge": (0.92, 0.95, 1.00),
"leather": (0.32, 0.12, 0.05),
"leather_dk": (0.12, 0.05, 0.03),
"dirt": (0.34, 0.17, 0.07),
"grass": (0.20, 0.62, 0.10),
"planks": (0.80, 0.52, 0.24),
"log": (0.30, 0.15, 0.06),
"roof": (0.80, 0.14, 0.10),
"roof_dk": (0.52, 0.07, 0.06),
"brick": (0.55, 0.20, 0.12),
"leaf": (0.10, 0.48, 0.12),
}
GLOW = { # (colour, emission strength 5–20)
"lock": ((1.00, 0.55, 0.08), 12.0),
"eyes": ((0.25, 1.00, 0.85), 10.0),
"crystal": ((0.85, 0.20, 1.00), 15.0),
"runes": ((0.15, 0.80, 1.00), 6.0),
"ruby": ((1.00, 0.08, 0.22), 12.0),
"window": ((1.00, 0.62, 0.20), 8.0),
"lantern": ((1.00, 0.80, 0.35), 14.0),
}
# ====================================================================== scene
def setup_scene():
sc = bpy.data.scenes.get(SCENE_NAME) or bpy.data.scenes.new(SCENE_NAME)
def purge(c):
for ch in list(c.children):
purge(ch)
for o in list(c.objects):
bpy.data.objects.remove(o, do_unlink=True)
bpy.data.collections.remove(c)
for o in list(sc.collection.objects):
bpy.data.objects.remove(o, do_unlink=True)
for c in list(sc.collection.children):
purge(c)
for w in bpy.context.window_manager.windows:
w.scene = sc
toys = bpy.data.collections.new("Toys")
studio = bpy.data.collections.new("Studio")
sc.collection.children.link(toys)
sc.collection.children.link(studio)
return sc, toys, studio
# ====================================================================== materials
_mat_cache = {}
def _fresh(name):
m = bpy.data.materials.get(name)
if m:
bpy.data.materials.remove(m)
m = bpy.data.materials.new(name)
try:
m.use_nodes = True
except Exception:
pass
m.node_tree.nodes.clear()
return m
def toy_material(key, rough=0.42, metal=0.0, coat=0.35, rim_col=(1.0, 0.95, 0.9)):
"""Painted plastic / wood toy shader: noise-varied roughness, micro bump,
soft coat specular, and a Layer-Weight fresnel rim added as emission."""
name = "Toy_" + key
if name in _mat_cache:
return _mat_cache[name]
col = PALETTE[key]
m = _fresh(name)
nt, N, L = m.node_tree, m.node_tree.nodes, m.node_tree.links
out = N.new("ShaderNodeOutputMaterial"); out.location = (900, 0)
bs = N.new("ShaderNodeBsdfPrincipled"); bs.location = (300, 100)
bs.inputs["Base Color"].default_value = (*col, 1)
bs.inputs["Metallic"].default_value = metal
if "Specular IOR Level" in bs.inputs:
bs.inputs["Specular IOR Level"].default_value = 0.55
if "Coat Weight" in bs.inputs:
bs.inputs["Coat Weight"].default_value = coat
bs.inputs["Coat Roughness"].default_value = 0.12
# roughness variation + subtle surface bump from object-space noise
tc = N.new("ShaderNodeTexCoord"); tc.location = (-700, 0)
nz = N.new("ShaderNodeTexNoise"); nz.location = (-500, 0)
nz.inputs["Scale"].default_value = 140.0
nz.inputs["Detail"].default_value = 3.0
L.new(tc.outputs["Object"], nz.inputs["Vector"])
mr = N.new("ShaderNodeMapRange"); mr.location = (-250, 120)
mr.inputs["To Min"].default_value = max(0.0, rough - 0.08)
mr.inputs["To Max"].default_value = min(1.0, rough + 0.08)
L.new(nz.outputs["Fac"], mr.inputs["Value"])
L.new(mr.outputs["Result"], bs.inputs["Roughness"])
bump = N.new("ShaderNodeBump"); bump.location = (-250, -150)
bump.inputs["Strength"].default_value = 0.08
bump.inputs["Distance"].default_value = 0.0004
L.new(nz.outputs["Fac"], bump.inputs["Height"])
L.new(bump.outputs["Normal"], bs.inputs["Normal"])
# fresnel rim glow: LayerWeight.Facing ^ 2.5 * RIM -> Emission strength
lw = N.new("ShaderNodeLayerWeight"); lw.location = (-250, -400)
lw.inputs["Blend"].default_value = 0.35
pw = N.new("ShaderNodeMath"); pw.operation = "POWER"; pw.location = (-50, -400)
pw.inputs[1].default_value = 2.5
L.new(lw.outputs["Facing"], pw.inputs[0])
mul = N.new("ShaderNodeMath"); mul.operation = "MULTIPLY"; mul.location = (130, -400)
mul.inputs[1].default_value = RIM
L.new(pw.outputs["Value"], mul.inputs[0])
em = N.new("ShaderNodeEmission"); em.location = (330, -350)
em.inputs["Color"].default_value = (*[0.5 * a + 0.5 * b for a, b in zip(col, rim_col)], 1)
L.new(mul.outputs["Value"], em.inputs["Strength"])
add = N.new("ShaderNodeAddShader"); add.location = (650, 0)
L.new(bs.outputs["BSDF"], add.inputs[0])
L.new(em.outputs["Emission"], add.inputs[1])
L.new(add.outputs["Shader"], out.inputs["Surface"])
m.diffuse_color = (*col, 1)
_mat_cache[name] = m
return m
def glow_material(key):
"""Emissive accent: coloured emission (strength from GLOW) + glossy body."""
name = "Glow_" + key
if name in _mat_cache:
return _mat_cache[name]
col, strength = GLOW[key]
m = _fresh(name)
N, L = m.node_tree.nodes, m.node_tree.links
out = N.new("ShaderNodeOutputMaterial"); out.location = (500, 0)
bs = N.new("ShaderNodeBsdfPrincipled")
bs.inputs["Base Color"].default_value = (*col, 1)
bs.inputs["Roughness"].default_value = 0.1
bs.inputs["Emission Color"].default_value = (*col, 1)
bs.inputs["Emission Strength"].default_value = strength
L.new(bs.outputs["BSDF"], out.inputs["Surface"])
m.diffuse_color = (*col, 1)
_mat_cache[name] = m
return m
# ====================================================================== mesh builder
class ToyBuilder:
"""Collects axis-aligned (optionally Z-rotated) boxes into one mesh with material slots."""
def __init__(self, name):
self.name = name
self.bm = bmesh.new()
self.mats = []
def _slot(self, mat):
if mat not in self.mats:
self.mats.append(mat)
return self.mats.index(mat)
def box(self, mat, center, size, rz=0.0):
M = (Matrix.Translation(center) @ Matrix.Rotation(math.radians(rz), 4, "Z")
@ Matrix.Diagonal((size[0], size[1], size[2], 1)))
ret = bmesh.ops.create_cube(self.bm, size=1.0, matrix=M)
idx = self._slot(mat)
for f in {f for v in ret["verts"] for f in v.link_faces}:
f.material_index = idx
def build(self, coll, location):
me = bpy.data.meshes.new(self.name)
self.bm.to_mesh(me)
self.bm.free()
for m in self.mats:
me.materials.append(m)
for p in me.polygons:
p.use_smooth = True
ob = bpy.data.objects.new(self.name, me)
ob.location = location
coll.objects.link(ob)
bv = ob.modifiers.new("Bevel", "BEVEL")
bv.width = BEVEL
bv.segments = 2
bv.limit_method = "ANGLE"
bv.angle_limit = math.radians(30)
bv.harden_normals = True # clean hard-surface shading on smooth-shaded boxes
bv.use_clamp_overlap = True
return ob
def corner_shell(t, mat_core, mat_post, cx, cy, z0, w, d, h, post):
"""Box w x d x h with square corner posts, built from non-overlapping pieces."""
zc = z0 + h / 2
wi, di = w - 2 * post, d - 2 * post
t.box(mat_core, (cx, cy, zc), (wi, d, h))
for sx in (-1, 1):
t.box(mat_core, (cx + sx * (wi / 2 + post / 2), cy, zc), (post, di, h))
for sy in (-1, 1):
t.box(mat_post, (cx + sx * (wi / 2 + post / 2), cy + sy * (di / 2 + post / 2), zc), (post, post, h))
# ====================================================================== models
def make_chest(coll, loc):
t = ToyBuilder("Toy_Chest")
wood, light, iron, gold = toy_material("wood", 0.55), toy_material("wood_light", 0.5), \
toy_material("iron", 0.35, metal=0.85, coat=0.0), toy_material("gold", 0.25, metal=1.0, coat=0.0)
W, D, P = 0.15, 0.10, 0.012
HB, HL = 0.075, 0.042
corner_shell(t, wood, iron, 0, 0, 0.0, W, D, HB, P) # body
corner_shell(t, light, iron, 0, 0, HB, W, D, HL, P) # lid
t.box(wood, (0, 0, HB + HL + 0.005), (W - 0.04, D - 0.04, 0.010)) # lid top step
t.box(gold, (0, 0, HB + HL + 0.0115), (W - 0.07, D - 0.07, 0.003)) # lid emblem plate
yf = -D / 2
for i, zc in enumerate((0.0135, 0.0375, 0.0615)): # front planks (raised panels)
t.box(light if i % 2 else wood, (0, yf - 0.0015, zc), (W - 2 * P - 0.006, 0.003, 0.021))
for y in (yf - 0.0015, D / 2 + 0.0015): # iron band at the lid seam
t.box(iron, (0, y, HB + 0.005), (W - 2 * P, 0.003, 0.010))
for sx in (-1, 1): # studs on the corner posts
for zc in (0.015, 0.045, 0.098):
t.box(gold, (sx * (W / 2 - P / 2), yf - 0.0025, zc), (0.006, 0.005, 0.006))
t.box(gold, (sx * (W / 2 - P / 2), -(D / 2 - P / 2), HB + HL + 0.003), (0.008, 0.008, 0.006))
t.box(gold, (sx * (W / 2 - P / 2), (D / 2 - P / 2), HB + HL + 0.003), (0.008, 0.008, 0.006))
t.box(iron, (sx * 0.035, D / 2 + 0.0045, HB), (0.016, 0.003, 0.022)) # back hinges
t.box(iron, (0, yf - 0.005, HB), (0.032, 0.004, 0.040)) # lock plate
t.box(glow_material("lock"), (0, yf - 0.0085, HB - 0.003), (0.012, 0.003, 0.016)) # glowing lock
t.box(gold, (0, yf - 0.0085, HB + 0.012), (0.016, 0.003, 0.005)) # lock shackle bar
return t.build(coll, loc)
def make_creature(coll, loc):
t = ToyBuilder("Toy_Creature")
body, dark, belly = toy_material("creature", 0.45), toy_material("creature_dk", 0.5), toy_material("belly", 0.45)
ivory, spike, ink, blush = toy_material("ivory", 0.4), toy_material("spike", 0.4), \
toy_material("ink", 0.3), toy_material("blush", 0.5)
eyes = glow_material("eyes")
LEG = 0.025
for sx in (-1, 1):
for sy in (-1, 1):
t.box(dark, (sx * 0.032, sy * 0.025, LEG / 2), (0.028, 0.028, LEG))
t.box(ivory, (sx * 0.032, sy * 0.025 - 0.016, 0.004), (0.020, 0.004, 0.006)) # toe claws
BW, BD, BH = 0.11, 0.09, 0.09
zb = LEG + BH / 2
t.box(body, (0, 0, zb), (BW, BD, BH))
yf = -BD / 2
t.box(belly, (0, yf - 0.0015, 0.046), (0.060, 0.003, 0.032))
for sx in (-1, 1):
t.box(eyes, (sx * 0.025, yf - 0.0015, 0.085), (0.020, 0.003, 0.024))
t.box(ink, (sx * 0.021, yf - 0.0035, 0.088), (0.008, 0.001, 0.010)) # pupils (inner/upper)
t.box(blush, (sx * 0.045, yf - 0.001, 0.072), (0.012, 0.002, 0.008))
# stepped horns
t.box(ivory, (sx * 0.032, 0, LEG + BH + 0.009), (0.018, 0.018, 0.018))
t.box(ivory, (sx * 0.036, 0, LEG + BH + 0.024), (0.012, 0.012, 0.012))
t.box(ivory, (sx * 0.039, 0, LEG + BH + 0.0335), (0.007, 0.007, 0.007))
t.box(dark, (sx * (BW / 2 + 0.004), 0, 0.06), (0.008, 0.022, 0.026)) # little arms
t.box(ink, (0, yf - 0.001, 0.068), (0.030, 0.002, 0.006)) # mouth
for sx in (-1, 1):
t.box(ivory, (sx * 0.008, yf - 0.0025, 0.0685), (0.005, 0.001, 0.004)) # teeth
for y, s in ((-0.008, 0.012), (0.014, 0.016), (0.034, 0.012)): # back spikes
t.box(spike, (0, y, LEG + BH + s / 2), (s, s * 0.8, s))
t.box(spike, (0, y, LEG + BH + s + 0.004), (s * 0.5, s * 0.4, 0.008))
t.box(dark, (0, BD / 2 + 0.008, 0.045), (0.016, 0.016, 0.016)) # tail
t.box(spike, (0, BD / 2 + 0.019, 0.045), (0.008, 0.006, 0.008))
return t.build(coll, loc)
def make_crystal_lamp(coll, loc):
t = ToyBuilder("Toy_CrystalLamp")
stone, slight, gold = toy_material("stone", 0.6), toy_material("stone_light", 0.55), \
toy_material("gold", 0.25, metal=1.0, coat=0.0)
crystal, runes = glow_material("crystal"), glow_material("runes")
t.box(stone, (0, 0, 0.009), (0.12, 0.12, 0.018))
t.box(gold, (0, 0, 0.025), (0.10, 0.10, 0.014))
t.box(slight, (0, 0, 0.037), (0.09, 0.09, 0.010))
for ang in range(4): # rune glyphs on the four base faces
a = math.radians(ang * 90)
for k, (u, dz, w, h) in enumerate(((-0.025, 0, 0.004, 0.010), (-0.012, 0.002, 0.010, 0.003),
(0.004, 0, 0.003, 0.010), (0.016, -0.002, 0.008, 0.003),
(0.028, 0.001, 0.004, 0.008))):
n = Vector((math.sin(a), -math.cos(a), 0))
tang = Vector((math.cos(a), math.sin(a), 0))
c = n * 0.061 + tang * u + Vector((0, 0, 0.009 + dz))
t.box(runes, c, (w, 0.002, h), rz=ang * 90)
for sx in (-1, 1): # corner pillars with gold feet
for sy in (-1, 1):
t.box(slight, (sx * 0.038, sy * 0.038, 0.097), (0.014, 0.014, 0.106))
t.box(gold, (sx * 0.038, sy * 0.038, 0.045), (0.018, 0.018, 0.006))
t.box(stone, (0, 0, 0.156), (0.11, 0.11, 0.012))
t.box(gold, (0, 0, 0.167), (0.075, 0.075, 0.010))
t.box(slight, (0, 0, 0.176), (0.045, 0.045, 0.008))
t.box(crystal, (0, 0, 0.187), (0.012, 0.012, 0.014), rz=45) # finial gem
t.box(crystal, (0, 0, 0.077), (0.034, 0.034, 0.070), rz=45) # emissive core
t.box(crystal, (0, 0, 0.123), (0.022, 0.022, 0.022), rz=45)
t.box(crystal, (0, 0, 0.139), (0.010, 0.010, 0.010), rz=45)
for (x, y) in ((0.03, 0), (-0.03, 0), (0, 0.03), (0, -0.03)): # satellite crystals
t.box(crystal, (x, y, 0.042 + 0.015), (0.012, 0.012, 0.030))
ob = t.build(coll, loc)
lamp = bpy.data.objects.new("CrystalLamp_Glow", bpy.data.lights.new("CrystalLamp_Glow", "POINT"))
lamp.data.energy = 1.5
lamp.data.color = GLOW["crystal"][0]
lamp.data.shadow_soft_size = 0.01
lamp.location = Vector(loc) + Vector((0, 0, 0.09))
coll.objects.link(lamp)
return ob
def make_sword(coll, loc):
t = ToyBuilder("Toy_Sword")
stone, gold = toy_material("stone", 0.6), toy_material("gold", 0.25, metal=1.0, coat=0.0)
steel = toy_material("steel", 0.22, metal=0.9, coat=0.0)
sdark = toy_material("steel_dark", 0.3, metal=0.9, coat=0.0)
sedge = toy_material("steel_edge", 0.15, metal=0.9, coat=0.0)
leather, ldark, ruby = toy_material("leather", 0.6), toy_material("leather_dk", 0.6), glow_material("ruby")
t.box(stone, (0, 0, 0.007), (0.07, 0.045, 0.014)) # display stand
t.box(gold, (0, 0, 0.016), (0.05, 0.035, 0.004))
t.box(gold, (0, 0, 0.030), (0.026, 0.026, 0.024)) # pommel
for y in (-0.014, 0.014):
t.box(ruby, (0, y, 0.030), (0.012, 0.002, 0.012)) # pommel gems
for i in range(6): # wrapped grip
z0 = 0.042 + i * 0.010
t.box(leather, (0, 0, z0 + 0.004), (0.016, 0.016, 0.008))
t.box(ldark, (0, 0, z0 + 0.009), (0.019, 0.019, 0.002))
t.box(gold, (0, 0, 0.110), (0.085, 0.018, 0.016)) # crossguard
for sx in (-1, 1):
t.box(gold, (sx * 0.0485, 0, 0.110), (0.012, 0.022, 0.022))
for y in (-0.010, 0.010):
t.box(ruby, (0, y, 0.110), (0.012, 0.002, 0.010)) # guard gems
zc, h = 0.193, 0.150 # blade: fuller, flats, edges
t.box(sdark, (0, 0, zc), (0.008, 0.008, h))
for sx in (-1, 1):
t.box(steel, (sx * 0.009, 0, zc), (0.010, 0.006, h))
t.box(sedge, (sx * 0.016, 0, zc), (0.004, 0.004, h))
for z, w, d, hh in ((0.274, 0.024, 0.006, 0.012), (0.285, 0.014, 0.005, 0.010), (0.294, 0.006, 0.004, 0.008)):
t.box(steel, (0, 0, z), (w, d, hh)) # stepped tip
return t.build(coll, loc)
def make_house(coll, loc):
t = ToyBuilder("Toy_House")
dirt, grass, planks, log = toy_material("dirt", 0.7), toy_material("grass", 0.55), \
toy_material("planks", 0.55), toy_material("log", 0.6)
roof, roofd, brick, leaf = toy_material("roof", 0.45), toy_material("roof_dk", 0.5), \
toy_material("brick", 0.65), toy_material("leaf", 0.55)
stone, gold, ink = toy_material("stone_light", 0.6), toy_material("gold", 0.25, metal=1.0, coat=0.0), \
toy_material("ink", 0.4)
win, lantern = glow_material("window"), glow_material("lantern")
S = 0.17
t.box(dirt, (0, 0, 0.010), (S, S, 0.020))
t.box(grass, (0, 0, 0.024), (S, S, 0.008))
for x in (-0.05, 0.02, 0.06): # grass drips
t.box(grass, (x, -S / 2 - 0.0015, 0.0165), (0.012, 0.003, 0.007))
G = 0.028 # ground level
HX, HY, HW, HD, HH, P = -0.015, 0.015, 0.10, 0.08, 0.065, 0.012
corner_shell(t, planks, log, HX, HY, G, HW, HD, HH, P)
yf = HY - HD / 2
t.box(log, (HX, yf - 0.0015, G + 0.020), (0.026, 0.003, 0.040)) # door frame
t.box(ink, (HX, yf - 0.0035, G + 0.019), (0.020, 0.001, 0.036)) # door recess
t.box(gold, (HX + 0.006, yf - 0.005, G + 0.018), (0.003, 0.002, 0.003)) # knob
def window(c, rz):
n = Vector((math.sin(math.radians(rz)), -math.cos(math.radians(rz)), 0))
tg = Vector((math.cos(math.radians(rz)), math.sin(math.radians(rz)), 0))
c = Vector(c)
t.box(win, c + n * 0.0015, (0.020, 0.003, 0.020), rz=rz)
t.box(log, c + n * 0.0035, (0.020, 0.001, 0.002), rz=rz) # mullion (h)
for s in (-1, 1):
t.box(log, c + n * 0.0035 + Vector((0, 0, s * 0.0055)), (0.002, 0.001, 0.009), rz=rz)
t.box(log, c + n * 0.003 + Vector((0, 0, -0.0115)), (0.026, 0.006, 0.003), rz=rz) # sill
for dx in (-0.030, 0.030):
window((HX + dx, yf, G + 0.040), 0)
window((HX + HW / 2, HY, G + 0.040), 90)
window((HX - HW / 2, HY, G + 0.040), -90)
zr = G + HH
for i, depth in enumerate((0.100, 0.076, 0.052, 0.028)): # stepped gable roof
t.box(roof if i % 2 == 0 else roofd, (HX, HY, zr + 0.005 + i * 0.010), (0.118, depth, 0.010))
t.box(roofd, (HX, HY, zr + 0.043), (0.122, 0.012, 0.006)) # ridge cap
t.box(brick, (HX + 0.035, HY + 0.032, zr + 0.025), (0.014, 0.012, 0.030)) # chimney on 2nd step
t.box(ink, (HX + 0.035, HY + 0.032, zr + 0.041), (0.010, 0.008, 0.002))
for k, y in enumerate((-0.035, -0.050, -0.065)): # stepping-stone path
t.box(stone, (HX + (0.004 if k % 2 else -0.004), y, G + 0.001), (0.018, 0.012, 0.002))
t.box(log, (0.065, -0.060, G + 0.025), (0.006, 0.006, 0.050)) # lantern post
t.box(lantern, (0.065, -0.060, G + 0.056), (0.012, 0.012, 0.012))
t.box(ink, (0.065, -0.060, G + 0.064), (0.016, 0.016, 0.004))
t.box(leaf, (-0.065, 0.060, G + 0.015), (0.030, 0.030, 0.030)) # bush cluster
t.box(leaf, (-0.065, 0.060, G + 0.039), (0.020, 0.020, 0.018))
t.box(leaf, (-0.042, 0.064, G + 0.009), (0.016, 0.016, 0.018))
ob = t.build(coll, loc)
for name, off, col, e in (("House_WindowGlow", (HX, yf - 0.02, G + 0.04), GLOW["window"][0], 0.6),
("House_LanternGlow", (0.065, -0.075, G + 0.056), GLOW["lantern"][0], 0.4)):
L = bpy.data.objects.new(name, bpy.data.lights.new(name, "POINT"))
L.data.energy = e
L.data.color = col
L.data.shadow_soft_size = 0.01
L.location = Vector(loc) + Vector(off)
coll.objects.link(L)
return ob
# ====================================================================== studio
def setup_studio(sc, coll):
world = bpy.data.worlds.get("Toy_Studio_World") or bpy.data.worlds.new("Toy_Studio_World")
try:
world.use_nodes = True
except Exception:
pass
bg = next((n for n in world.node_tree.nodes if n.type == "BACKGROUND"), None)
if bg is None:
world.node_tree.nodes.clear()
bg = world.node_tree.nodes.new("ShaderNodeBackground")
o = world.node_tree.nodes.new("ShaderNodeOutputWorld")
world.node_tree.links.new(bg.outputs["Background"], o.inputs["Surface"])
bg.inputs["Color"].default_value = (0.045, 0.047, 0.055, 1) # neutral dark grey
bg.inputs["Strength"].default_value = 1.0
sc.world = world
fm = _fresh("Studio_Floor")
b = fm.node_tree.nodes.new("ShaderNodeBsdfPrincipled")
b.inputs["Base Color"].default_value = (0.06, 0.062, 0.07, 1)
b.inputs["Roughness"].default_value = 0.55
o = fm.node_tree.nodes.new("ShaderNodeOutputMaterial")
fm.node_tree.links.new(b.outputs["BSDF"], o.inputs["Surface"])
bm = bmesh.new()
bmesh.ops.create_grid(bm, x_segments=1, y_segments=1, size=4.0)
me = bpy.data.meshes.new("Studio_Floor")
bm.to_mesh(me)
bm.free()
me.materials.append(fm)
floor = bpy.data.objects.new("Studio_Floor", me)
coll.objects.link(floor)
target = Vector((0, 0, 0.09))
def area(name, loc, energy, size, col):
L = bpy.data.objects.new(name, bpy.data.lights.new(name, "AREA"))
L.data.energy, L.data.size, L.data.color = energy, size, col
L.location = loc
L.rotation_euler = (target - Vector(loc)).to_track_quat("-Z", "Y").to_euler()
coll.objects.link(L)
area("Key_Soft", (-1.3, -1.6, 1.7), 45, 1.4, (1.0, 0.96, 0.9))
area("Fill_Cool", (1.7, -1.3, 0.7), 14, 2.0, (0.8, 0.88, 1.0))
area("Rim_Back", (0.4, 1.6, 1.1), 40, 1.2, (1.0, 1.0, 1.0))
return target
def setup_camera_and_render(sc, coll, models):
xs = [m.location.x for m in models]
span = (max(xs) - min(xs)) + 0.22
cam = bpy.data.objects.new("Toy_Camera", bpy.data.cameras.new("Toy_Camera"))
cam.data.lens = 50
fov = 2 * math.atan(18 / cam.data.lens) # horizontal fov (36 mm sensor, 16:9)
dist = (span / 2) / math.tan(fov / 2) * 1.04
target = Vector((0, 0, 0.10))
elev = math.radians(17)
cam.location = target + Vector((0, -dist * math.cos(elev), dist * math.sin(elev)))
cam.rotation_euler = (target - cam.location).to_track_quat("-Z", "Y").to_euler()
coll.objects.link(cam)
sc.camera = cam
for eng in ("BLENDER_EEVEE", "BLENDER_EEVEE_NEXT"):
try:
sc.render.engine = eng
break
except Exception:
pass
try:
sc.eevee.use_raytracing = True
except Exception:
pass
sc.render.resolution_x, sc.render.resolution_y = 1920, 1080
try:
sc.view_settings.view_transform = "AgX"
sc.view_settings.look = "AgX - Medium High Contrast"
except Exception:
pass
bloom = "off"
try: # compositor bloom (Blender 5.x node-group compositor)
tree = bpy.data.node_groups.new("Toy_Compositor", "CompositorNodeTree")
sc.compositing_node_group = tree
rl, gl, go = tree.nodes.new("CompositorNodeRLayers"), tree.nodes.new("CompositorNodeGlare"), \
tree.nodes.new("NodeGroupOutput")
tree.interface.new_socket("Image", in_out="OUTPUT", socket_type="NodeSocketColor")
try:
gl.glare_type = "BLOOM"
except Exception:
if "Type" in gl.inputs:
gl.inputs["Type"].default_value = "Bloom"
for k, v in (("Threshold", 1.0), ("Strength", 0.5), ("Size", 0.5)):
if k in gl.inputs:
gl.inputs[k].default_value = v
tree.links.new(rl.outputs["Image"], gl.inputs["Image"])
tree.links.new(gl.outputs["Image"], go.inputs[0])
bloom = "on"
except Exception as ex:
bloom = "unavailable: %s" % ex
return bloom
# ====================================================================== main
def main():
os.makedirs(OUT_DIR, exist_ok=True)
_mat_cache.clear()
sc, toys, studio = setup_scene()
builders = (make_chest, make_creature, make_crystal_lamp, make_sword, make_house)
models = []
for i, fn in enumerate(builders):
x = (i - (len(builders) - 1) / 2) * SPACING
models.append(fn(toys, (x, 0, 0)))
setup_studio(sc, studio)
bloom = setup_camera_and_render(sc, studio, models)
vl = sc.view_layers[0]
sc.render.filepath = os.path.join(OUT_DIR, "voxel_toys_preview.png")
with bpy.context.temp_override(scene=sc, view_layer=vl):
bpy.ops.render.render(write_still=True, scene=sc.name)
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(OUT_DIR, "voxel_toys.blend"))
return {
"models": {m.name: [round(v, 3) for v in m.dimensions] for m in models},
"faces": sum(len(m.data.polygons) for m in models),
"bloom": bloom,
"preview": sc.render.filepath,
}
result = main()