cntrl/tools/blender/build_blackout_props.py

211 lines
8.5 KiB
Python

# tools/blender/build_blackout_props.py
# FBC: NIGHT SHIFT — Blackout ritual props.
#
# Run inside Blender 5.x:
# exec(open("/home/dandovlatov/Documents/cntrl/tools/blender/build_blackout_props.py").read())
#
# Three sculpted set pieces for the projector-screen blackout ritual:
# phone_table - a desk with a ringing red hotline (the sequence nodes)
# case_frame - the opaque plinth, corner posts and yellow Board roof
# case_glass - the four translucent panes (drawn with alpha in the game)
# Each exports as one GLB with one merged mesh / painted vertex colours.
import math
import os
import sys
import bpy
from mathutils import 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({"plastic": "#2a2d33", "glass": "#9fc4cf",
"concrete_lt": "#9d9c96", "brass": "#b89348"})
# The OoP container is authored at half its shipped size: the frame and glass
# GLBs are uniformly doubled here, so both the game and the concept scene get
# one single, reproducible size change (no second scale in the presenter).
CASE_SCALE = 2.0
# Hotline desks ship 1.5x their authored size so a ringing table reads as real
# office furniture next to the ~1.9 m agent instead of doll-house dressing.
PHONE_TABLE_SCALE = 1.5
def rect(rx, ry):
return [(-rx, -ry), (rx, -ry), (rx, ry), (-rx, ry)]
def build_phone_table():
r = K.new_actor("PhoneTable")
K.plate(r, "top", rect(0.62, 0.40), 0.79, 0.85, 1.0, P["plastic"])
K.plate(r, "top_edge", rect(0.64, 0.42), 0.77, 0.80, 1.0, P["steel_dk"])
for sx in (-1, 1):
for sy in (-1, 1):
K.tube(r, "leg_%d_%d" % (sx, sy),
[(sx * 0.50, sy * 0.30, 0.02),
(sx * 0.52, sy * 0.31, 0.79)],
[0.045, 0.038], P["steel_dk"], sides=6)
K.plate(r, "drawer", rect(0.42, 0.02), 0.64, 0.76, 1.0,
P["redaction"], shift=(0, -0.40))
K.tube(r, "drawer_pull", [(-0.12, -0.43, 0.70), (0.12, -0.43, 0.70)],
[0.016, 0.016], P["brass"], sides=6)
K.blob(r, "phone_base", (0, 0, 0.92), (0.27, 0.20, 0.075),
P["hiss"], subdiv=3, seed=11, ao=0.05)
K.tube(r, "handset",
[(-0.28, -0.02, 1.07), (-0.15, -0.13, 1.10),
(0.15, -0.13, 1.10), (0.28, -0.02, 1.07)],
[0.050, 0.044, 0.044, 0.050], P["hiss_hot"], sides=10)
K.ring(r, "dial", (0, -0.09, 0.985), 0.095, 0.014,
P["redaction"], segments=10)
for i in range(6):
a = 2.0 * math.pi * i / 6
K.crystal(r, "dial_hole_%d" % i,
(math.cos(a) * 0.072, -0.09 + math.sin(a) * 0.072, 0.99),
0.016, P["hiss_hot"], sides=4, seed=20 + i)
K.crystal(r, "indicator", (0.20, -0.16, 0.995), 0.040,
P["hiss_hot"], sides=5, seed=3)
K.tube(r, "cord",
[(0.28, -0.02, 1.07), (0.38, -0.20, 0.94),
(0.22, -0.36, 0.88), (0.04, -0.30, 0.93)],
[0.012, 0.011, 0.011, 0.011], P["redaction"], sides=5)
return r
def build_case_frame():
r = K.new_actor("CaseFrame")
K.plate(r, "plinth", rect(0.88, 0.66), 0.0, 0.13, 1.0,
P["concrete_lt"], ao=0.10)
K.plate(r, "plinth_trim", rect(0.92, 0.70), 0.10, 0.15, 1.0,
P["brass"], ao=0.04)
for sx in (-1, 1):
for sy in (-1, 1):
K.tube(r, "post_%d_%d" % (sx, sy),
[(sx * 0.83, sy * 0.61, 0.15),
(sx * 0.83, sy * 0.61, 1.93)],
[0.042, 0.042], P["steel_dk"], sides=6)
K.plate(r, "top_rim", rect(0.90, 0.68), 1.90, 1.97, 1.0,
P["brass"], ao=0.03)
K.plate(r, "yellow_roof", rect(0.98, 0.75), 1.97, 2.12, 0.80,
P["board"], ao=0.02)
K.plate(r, "roof_seal", rect(0.22, 0.045), 2.13, 2.145, 1.0,
P["redaction"], shift=(0, -0.48), ao=0.0)
return r
def build_case_glass():
r = K.new_actor("CaseGlass")
K.plate(r, "pane_front", rect(0.81, 0.012), 0.16, 1.90, 1.0,
P["glass"], shift=(0, -0.60), ao=0.0)
K.plate(r, "pane_back", rect(0.81, 0.012), 0.16, 1.90, 1.0,
P["glass"], shift=(0, 0.60), ao=0.0)
K.plate(r, "pane_left", rect(0.012, 0.60), 0.16, 1.90, 1.0,
P["glass"], shift=(-0.83, 0), ao=0.0)
K.plate(r, "pane_right", rect(0.012, 0.60), 0.16, 1.90, 1.0,
P["glass"], shift=(0.83, 0), ao=0.0)
return r
BUILDERS = [
("phone_table", build_phone_table, 1400),
("case_frame", build_case_frame, 700),
("case_glass", build_case_glass, 200),
]
exported = []
roots = {}
for name, builder, cap in BUILDERS:
root = builder()
K.merge_actor(root, name)
# export_actor uses export_apply=True, so this object scale is baked into
# the GLB; the .blend keeps it editable on the merged mesh.
art_scale = CASE_SCALE if name.startswith("case_") else (
PHONE_TABLE_SCALE if name == "phone_table" else 1.0)
if art_scale != 1.0:
for child in root.children_recursive:
if child.type == "MESH":
child.scale = (art_scale, art_scale, art_scale)
tris = K.triangle_count(root)
print("[budget] %-12s %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)
roots[name] = root
exported.append((name, tris, os.path.getsize(out)))
# Concept-only: make the panes see-through in the render (the game handles
# transparency with its own uAlpha path; the exported GLB is unchanged).
glass_concept = bpy.data.materials.new("glass_concept")
glass_concept.use_nodes = True
glass_concept.surface_render_method = "BLENDED"
concept_bsdf = next(n for n in glass_concept.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
concept_bsdf.inputs["Alpha"].default_value = 0.16
for pane in roots["case_glass"].children_recursive:
if pane.type == "MESH":
pane.data.materials.clear()
pane.data.materials.append(glass_concept)
# ---- concept lineup -------------------------------------------------------
# Assemble the case around the existing lightswitch for the composition shot.
bpy.ops.object.select_all(action="DESELECT")
bpy.ops.import_scene.gltf(filepath=os.path.join(MODEL_DIR, "switch.glb"))
switch_root = bpy.context.object
for name, x in (("phone_table", -3.0), ("case_frame", 0.6),
("case_glass", 0.6)):
roots[name].location.x = x
switch_root.location = (0.6, 0, -0.16 * CASE_SCALE)
switch_root.scale = (1.7 * CASE_SCALE, 1.7 * CASE_SCALE, 1.7 * CASE_SCALE)
world = bpy.data.worlds.new("BlackoutWorld")
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("#08090c"), 1.0)
bg.inputs["Strength"].default_value = 0.6
for name, loc, power, col in (
("Key", (-3, -7, 7), 1800, "#eef2ff"),
("PhoneRed", (-4, -1, 3), 900, "#ff3b46"),
("BoardYellow", (3, -2, 6), 1400, "#f5c518"),
):
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, 1)) - Vector(loc)).to_track_quat("-Z", "Y").to_euler()
cam_data = bpy.data.cameras.new("BlackoutCam")
cam = bpy.data.objects.new("BlackoutCam", cam_data)
bpy.context.scene.collection.objects.link(cam)
cam.location = (0.4, -14.0, 5.8)
cam.rotation_euler = (Vector((0.6, 0, 2.2)) - 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 = 560
scene.render.image_settings.file_format = "PNG"
scene.render.filepath = os.path.join(CONCEPT_DIR, "blackout_props_lineup.png")
bpy.ops.render.render(write_still=True)
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(BLEND_DIR, "fbc_blackout_props.blend"))
print("[done] blackout props:", ", ".join("%s(%d tris)" % (n, t)
for n, t, _ in exported))