cntrl/tools/blender/build_litter_props.py

185 lines
6.8 KiB
Python

# tools/blender/build_litter_props.py
# FBC: NIGHT SHIFT — office litter kit: wastebasket, document stack, loose sheet.
# Run inside Blender 5.x:
# exec(open("/home/dandovlatov/Documents/cntrl/tools/blender/build_litter_props.py").read())
import math
import os
import sys
import bpy
from mathutils import Euler, Vector
REPO = "/home/dandovlatov/Documents/cntrl"
MODEL_DIR = os.path.join(REPO, "assets", "models", "props")
BLEND_DIR = os.path.join(REPO, "assets", "blender")
CONCEPT_DIR = os.path.join(REPO, "docs", "concept")
for d in (MODEL_DIR, BLEND_DIR, CONCEPT_DIR):
os.makedirs(d, exist_ok=True)
sys.path.insert(0, os.path.join(REPO, "tools", "blender"))
import importlib
import actor_kit as K
importlib.reload(K)
K.clear_scene()
P = K.PALETTE
P.update({"bin": "#3a3e45", "bin_lt": "#525861", "bin_in": "#15171b"})
def place(obj, rot=(0.0, 0.0, 0.0), offset=(0.0, 0.0, 0.0)):
mat = Euler(rot).to_matrix()
shift = Vector(offset)
for v in obj.data.vertices:
v.co = shift + mat @ v.co
obj.data.update()
return obj
def sheet(r, name, w, h, z, value, rot=(0.0, 0.0, 0.0), shift=(0.0, 0.0)):
outline = [(-w / 2, -h / 2), (w / 2, -h / 2),
(w / 2, h / 2), (-w / 2, h / 2)]
obj = K.plate(r, name, outline, z, z + 0.008, 1.0, value,
shift=shift, ao=0.0)
return place(obj, rot=rot, offset=(shift[0], shift[1], 0.0))
def bar(r, name, w, x, y, z, value=P["redaction"]):
outline = [(-w / 2, -0.012), (w / 2, -0.012),
(w / 2, 0.012), (-w / 2, 0.012)]
return K.plate(r, name, outline, z, z + 0.004, 1.0, value,
shift=(x, y), ao=0.0)
def build_bin():
r = K.new_actor("GarbageCan")
K.loft(r, "shell", [
{"z": 0.0, "r": (0.155, 0.155), "n": 14},
{"z": 0.06, "r": (0.165, 0.165), "n": 14},
{"z": 0.68, "r": (0.215, 0.215), "n": 14},
{"z": 0.72, "r": (0.225, 0.225), "n": 14, "cap": False},
], P["bin"], ao=0.10)
K.loft(r, "lining", [
{"z": 0.10, "r": (0.142, 0.142), "n": 14},
{"z": 0.68, "r": (0.195, 0.195), "n": 14},
{"z": 0.715, "r": (0.205, 0.205), "n": 14, "cap": False},
], P["bin_in"], ao=0.0)
K.ring(r, "rim", (0.0, 0.0, 0.722), 0.218, 0.014, P["bin_lt"],
segments=14)
for i in range(8):
a = 2.0 * math.pi * i / 8
x, y = math.cos(a) * 0.188, math.sin(a) * 0.188
K.tube(r, "rib_%d" % i,
[(x * 0.88, y * 0.88, 0.08), (x, y, 0.70)],
[0.011, 0.011], P["bin_lt"], sides=4, ao=0.0)
label = K.plate(r, "label", [(-0.10, -0.045), (0.10, -0.045),
(0.10, 0.045), (-0.10, 0.045)],
0.0, 0.004, 1.0, P["redaction"], ao=0.0)
place(label, rot=(math.pi / 2, 0.0, 0.0), offset=(0.0, -0.207, 0.44))
K.blob(r, "crumpled_a", (0.04, -0.03, 0.20), (0.095, 0.08, 0.055),
P["paper"], subdiv=1, seed=11, ao=0.0)
K.blob(r, "crumpled_b", (-0.05, 0.045, 0.15), (0.075, 0.07, 0.05),
P["paper"], subdiv=1, seed=12, ao=0.0)
K.blob(r, "crumpled_c", (0.02, 0.06, 0.13), (0.055, 0.06, 0.035),
P["board"], subdiv=1, seed=13, ao=0.0)
return r
def build_stack():
r = K.new_actor("DocumentStack")
specs = [
(0.0, 0.42, 0.30, 0.0), (0.012, 0.40, 0.29, 0.05),
(0.024, 0.43, 0.31, -0.07), (0.036, 0.41, 0.30, 0.03),
(0.048, 0.43, 0.30, -0.02),
]
for i, (z, w, h, yaw) in enumerate(specs):
sx = (i % 3 - 1) * 0.012
sy = (i % 2 - 0.5) * 0.010
sheet(r, "page_%d" % i, w, h, z,
P["board"] if i == 3 else P["paper"],
rot=(0.0, 0.0, yaw), shift=(sx, sy))
bar(r, "redact_0", 0.24, 0.0, 0.075, 0.057)
bar(r, "redact_1", 0.32, -0.02, 0.015, 0.057)
bar(r, "redact_2", 0.18, 0.06, -0.045, 0.057)
bar(r, "stamp", 0.09, -0.14, -0.10, 0.057, P["hiss"])
for x in (-0.03, 0.03):
K.tube(r, "staple_%g" % x,
[(x, -0.135, 0.044), (x, -0.135, 0.060)],
[0.004, 0.004], P["steel"], sides=4, ao=0.0)
return r
def build_leaf():
r = K.new_actor("LooseSheet")
sheet(r, "page", 0.40, 0.30, 0.0, P["paper"],
rot=(0.0, 0.0, -0.14))
bar(r, "redact_0", 0.24, -0.015, 0.075, 0.009)
bar(r, "redact_1", 0.30, 0.0, 0.005, 0.009)
bar(r, "redact_2", 0.16, -0.05, -0.065, 0.009)
curl = K.plate(r, "curl", [(-0.09, 0.0), (0.09, 0.0),
(0.09, 0.05), (-0.09, 0.05)],
0.0, 0.008, 1.0, P["paper"], ao=0.0)
place(curl, rot=(-0.55, 0.0, -0.14), offset=(0.12, 0.14, 0.0))
return r
BUILDERS = [
("garbage_can", build_bin, 900),
("document_stack", build_stack, 400),
("loose_sheet", build_leaf, 150),
]
exported = []
for name, builder, cap in BUILDERS:
root = builder()
K.merge_actor(root, name)
tris = K.triangle_count(root)
print("[budget] %-15s %5d tris (cap %d) %s"
% (name, tris, cap, "OK" if tris <= cap else "OVER"))
out = os.path.join(MODEL_DIR, name + ".glb")
K.export_actor(root, name, out)
exported.append((name, tris, os.path.getsize(out)))
for obj, x in zip(K.ROOTS, [-2.4, 0.0, 2.4]):
obj.location.x = x
world = bpy.data.worlds.new("LitterWorld")
bpy.context.scene.world = world
world.use_nodes = True
bg = next(n for n in world.node_tree.nodes if n.type == "BACKGROUND")
bg.inputs["Color"].default_value = (*K.rgb("#0d0f12"), 1.0)
bg.inputs["Strength"].default_value = 0.9
for name, loc, power, col in (
("Key", (-4, -8, 7), 2600, "#eef2ff"),
("Fill", (6, 2, 4), 1100, "#ff3b46"),
("Rim", (0, 7, 5), 1300, "#4ec9b0"),
):
data = bpy.data.lights.new(name, "AREA")
data.energy = power
data.shape = "DISK"
data.size = 7
data.color = K.rgb(col)
obj = bpy.data.objects.new(name, data)
bpy.context.scene.collection.objects.link(obj)
obj.location = loc
obj.rotation_euler = (Vector((0, 0, 0.4)) - Vector(loc)).to_track_quat("-Z", "Y").to_euler()
cam_data = bpy.data.cameras.new("LitterCam")
cam = bpy.data.objects.new("LitterCam", cam_data)
bpy.context.scene.collection.objects.link(cam)
cam.location = (0.0, -6.2, 3.0)
cam.rotation_euler = (Vector((0, 0, 0.35)) - cam.location).to_track_quat("-Z", "Y").to_euler()
cam_data.lens = 42
scene = bpy.context.scene
scene.camera = cam
engines = [i.identifier for i in bpy.types.RenderSettings.bl_rna.properties["engine"].enum_items]
scene.render.engine = "BLENDER_EEVEE_NEXT" if "BLENDER_EEVEE_NEXT" in engines else engines[0]
scene.render.resolution_x = 1200
scene.render.resolution_y = 620
scene.render.image_settings.file_format = "PNG"
scene.render.filepath = os.path.join(CONCEPT_DIR, "litter_props_lineup.png")
bpy.ops.render.render(write_still=True)
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(BLEND_DIR, "fbc_litter_props.blend"))
print("[done] litter props:", ", ".join("%s(%d tris)" % (n, t) for n, t, _ in exported))