cntrl/tools/blender/build_body_pile.py

222 lines
10 KiB
Python

# tools/blender/build_body_pile.py
# FBC: NIGHT SHIFT — floor dressing: a heap of fallen bodies.
# Stylised, faceless mannequin-like figures in muted office/workwear, tangled
# together on the floor. One merged mesh, one material, COLOR_0 vertex colours.
#
# Run inside Blender 5.x:
# exec(open("/home/dandovlatov/Documents/cntrl/tools/blender/build_body_pile.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
DRIED = "#5a1418"
DRIED_DARK = "#3a0f12"
# offset (x, y, z) in metres; tilt (rx, ry, rz) in radians; body palette.
# Bodies 1-2 form the base layer, 3-4 are draped across them, 5 crests the
# heap, 6 sprawls out from the side.
BODIES = [
((-0.10, -0.45, 0.02), (0.02, 0.00, 0.10), "navy_dk", "navy_dk", "navy", 11),
(( 0.35, 0.30, 0.02), (-0.03, 0.00, 2.55), "tan", "tan", "navy_dk", 12),
((-0.25, 0.15, 0.30), (0.30, -0.18, 1.05), "navy", "navy_lt", "navy", 13),
(( 0.10, -0.15, 0.34), (-0.22, 0.25, -0.80), "concrete", "concrete", "navy_dk", 14),
(( 0.00, 0.05, 0.55), (0.35, -0.30, 0.45), "board", "board", "navy_dk", 15),
((-0.55, 0.55, 0.06), (0.05, 0.00, 1.95), "steel_dk", "steel_dk", "navy_dk", 16),
(( 0.62, -0.52, 0.04), (0.02, 0.00, -1.35), "tan", "tan", "navy", 17),
((-0.48, -0.55, 0.14), (0.20, -0.10, 0.80), "navy_lt", "navy_lt", "navy_dk", 18),
]
def place(obj, rot, offset):
"""Bake a rigid rotation + translation into the mesh (parts start in a
canonical pose lying along +X with the head at +X and the face up)."""
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 stain(r, name, center, size, value, seed):
return K.blob(r, name, center, size, value, subdiv=0, warp=0.22,
seed=seed, ao=0.0)
def build_body(r, tag, cloth, sleeve, pants, seed):
"""Return one body's parts, lying flat in canonical local space."""
parts = []
# Torso / shoulders / hips, laid out along +X.
parts.append(K.blob(r, tag + "_torso", (0.00, 0.00, 0.27),
(0.40, 0.25, 0.17), cloth, subdiv=1, seed=seed + 1))
parts.append(K.blob(r, tag + "_chest", (0.24, 0.00, 0.29),
(0.18, 0.24, 0.16), cloth, subdiv=1, seed=seed + 2))
parts.append(K.blob(r, tag + "_pelvis", (-0.39, 0.00, 0.24),
(0.20, 0.22, 0.15), pants, subdiv=1, seed=seed + 3))
parts.append(K.blob(r, tag + "_belt", (-0.20, 0.00, 0.415),
(0.045, 0.235, 0.018), P["redaction"], subdiv=0,
seed=seed + 16, ao=0.0))
parts.append(K.blob(r, tag + "_collar", (0.39, 0.00, 0.405),
(0.055, 0.150, 0.020), P["steel_dk"], subdiv=0,
seed=seed + 17, ao=0.0))
# Neck and head: jaw, nose and closed eyes keep the figure recognisably
# human without turning it into a detailed portrait.
parts.append(K.tube(r, tag + "_neck", [(0.36, 0.00, 0.30), (0.49, 0.00, 0.27)],
[0.075, 0.065], P["skin"], sides=6, ao=0.06))
parts.append(K.blob(r, tag + "_head", (0.60, 0.00, 0.26),
(0.160, 0.140, 0.130), P["skin"], subdiv=1,
seed=seed + 4, ao=0.06))
parts.append(K.blob(r, tag + "_jaw", (0.69, 0.00, 0.205),
(0.075, 0.085, 0.055), P["skin"], subdiv=0,
seed=seed + 18, ao=0.0))
parts.append(K.blob(r, tag + "_nose", (0.75, 0.00, 0.285),
(0.030, 0.028, 0.025), P["skin"], subdiv=0,
seed=seed + 19, ao=0.0))
parts.append(K.blob(r, tag + "_hair", (0.52, 0.00, 0.30),
(0.120, 0.115, 0.075), P["redaction"], subdiv=0,
seed=seed + 5, ao=0.0))
for side, sy in (("l", 0.050), ("r", -0.050)):
parts.append(K.blob(r, tag + "_eye_" + side, (0.66, sy, 0.375),
(0.022, 0.018, 0.010), P["redaction"], subdiv=0,
seed=seed + (20 if side == "l" else 21), ao=0.0))
# Arms flung outward at different angles.
parts.append(K.tube(r, tag + "_arm_l",
[(0.22, 0.17, 0.29), (0.02, 0.37, 0.20), (-0.20, 0.32, 0.13)],
[0.090, 0.075, 0.060], sleeve, sides=6, ao=0.08))
parts.append(K.tube(r, tag + "_arm_r",
[(0.22, -0.17, 0.29), (0.12, -0.40, 0.20), (-0.08, -0.47, 0.13)],
[0.090, 0.075, 0.060], sleeve, sides=6, ao=0.08))
parts.append(K.blob(r, tag + "_elbow_l", (0.02, 0.37, 0.20),
(0.085, 0.080, 0.075), sleeve, subdiv=0, seed=seed + 12, ao=0.0))
parts.append(K.blob(r, tag + "_elbow_r", (0.12, -0.40, 0.20),
(0.085, 0.080, 0.075), sleeve, subdiv=0, seed=seed + 13, ao=0.0))
parts.append(K.blob(r, tag + "_cuff_l", (-0.17, 0.31, 0.14),
(0.060, 0.060, 0.055), P["steel_dk"], subdiv=0,
seed=seed + 22, ao=0.0))
parts.append(K.blob(r, tag + "_cuff_r", (-0.05, -0.44, 0.14),
(0.060, 0.060, 0.055), P["steel_dk"], subdiv=0,
seed=seed + 23, ao=0.0))
parts.append(K.blob(r, tag + "_hand_l", (-0.24, 0.30, 0.12),
(0.075, 0.060, 0.045), P["skin"], subdiv=0, seed=seed + 6, ao=0.0))
parts.append(K.blob(r, tag + "_hand_r", (-0.12, -0.45, 0.12),
(0.075, 0.060, 0.045), P["skin"], subdiv=0, seed=seed + 7, ao=0.0))
# Legs trailing behind the torso, one bent.
parts.append(K.tube(r, tag + "_leg_l",
[(-0.46, 0.11, 0.24), (-0.82, 0.17, 0.17), (-1.10, 0.12, 0.11)],
[0.125, 0.105, 0.085], pants, sides=6, ao=0.08))
parts.append(K.tube(r, tag + "_leg_r",
[(-0.46, -0.11, 0.24), (-0.78, -0.22, 0.15), (-1.06, -0.28, 0.11)],
[0.125, 0.105, 0.085], pants, sides=6, ao=0.08))
parts.append(K.blob(r, tag + "_knee_l", (-0.82, 0.17, 0.17),
(0.115, 0.105, 0.100), pants, subdiv=0, seed=seed + 14, ao=0.0))
parts.append(K.blob(r, tag + "_knee_r", (-0.78, -0.22, 0.15),
(0.115, 0.105, 0.100), pants, subdiv=0, seed=seed + 15, ao=0.0))
parts.append(K.blob(r, tag + "_shoe_l", (-1.15, 0.12, 0.09),
(0.130, 0.090, 0.070), P["redaction"], subdiv=0, seed=seed + 8, ao=0.0))
parts.append(K.blob(r, tag + "_shoe_r", (-1.11, -0.29, 0.09),
(0.130, 0.090, 0.070), P["redaction"], subdiv=0, seed=seed + 9, ao=0.0))
# Muted, dried staining on the cloth — no graphic detail.
parts.append(stain(r, tag + "_stain_a", (0.02, 0.06, 0.435),
(0.13, 0.09, 0.012), DRIED, seed + 10))
parts.append(stain(r, tag + "_stain_b", (-0.24, -0.05, 0.38),
(0.10, 0.08, 0.012), DRIED_DARK, seed + 11))
return parts
root = K.new_actor("BodyPile")
# Dark pooled smears on the floor under the heap.
for i, (x, y, z, sx, sy, seed) in enumerate([
(0.00, 0.00, 0.012, 0.70, 0.50, 21),
(-0.30, 0.30, 0.014, 0.40, 0.32, 22),
(0.25, -0.20, 0.014, 0.45, 0.30, 23),
]):
stain(root, "pool_%d" % i, (x, y, z), (sx, sy, 0.010),
DRIED_DARK, seed)
for i, (offset, tilt, cloth, sleeve, pants, seed) in enumerate(BODIES):
parts = build_body(root, "body_%d" % i, P[cloth], P[sleeve], P[pants], seed)
for obj in parts:
place(obj, tilt, offset)
# Nothing should poke through the floor.
for obj in root.children_recursive:
if obj.type == "MESH":
for v in obj.data.vertices:
if v.co.z < 0.008:
v.co.z = 0.008
obj.data.update()
K.merge_actor(root, "body_pile")
tris = K.triangle_count(root)
print("[budget] body_pile %5d tris (cap 5600) %s" %
(tris, "OK" if tris <= 5600 else "OVER"))
out = os.path.join(MODEL_DIR, "body_pile.glb")
K.export_actor(root, "body_pile", out)
print("[export] %s (%d bytes)" % (out, os.path.getsize(out)))
# Small lineup render for concept review.
world = bpy.data.worlds.new("BodyPileWorld")
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, -6, 6), 2200, "#eef2ff"),
("Fill", (5, 3, 4), 900, "#ff3b46"),
("Rim", (0, 6, 4), 1200, "#4ec9b0"),
):
data = bpy.data.lights.new(name, "AREA")
data.energy = power
data.shape = "DISK"
data.size = 6
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.3)) - Vector(loc)).to_track_quat("-Z", "Y").to_euler()
cam_data = bpy.data.cameras.new("BodyPileCam")
cam = bpy.data.objects.new("BodyPileCam", cam_data)
bpy.context.scene.collection.objects.link(cam)
cam.location = (2.8, -3.4, 2.6)
cam.rotation_euler = (Vector((0, 0, 0.35)) - cam.location).to_track_quat("-Z", "Y").to_euler()
cam_data.lens = 45
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 = 1100
scene.render.resolution_y = 700
scene.render.image_settings.file_format = "PNG"
scene.render.filepath = os.path.join(CONCEPT_DIR, "body_pile_lineup.png")
bpy.ops.render.render(write_still=True)
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(BLEND_DIR, "fbc_body_pile.blend"))
print("[done] body pile")