KAZIK/tools/generators/build_studio_v6.py

355 lines
18 KiB
Python

"""Build the corridor, dorm room and camera timing for the new studio ident."""
import argparse
import math
import sys
from pathlib import Path
import bpy
from mathutils import Quaternion, Vector
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
from build_cinematic_v4 import area, cube, cylinder, mat, noise_surface, tube
from workshop_props import lettering, printed
from workshop_props_v6 import CAN_RADIUS, detailed_props, fit_beverage
ROOT = HERE.parents[1]
SOURCE = ROOT / "assets/cinematics/blender"
WORK = ROOT / "build/cinematic-v6"
END = 288
def material(name, color, roughness=.8, bump=.002):
result = mat(name, color, 0, roughness)
noise_surface(result, 95, bump)
return result
def two_tone(name, location, size, upper, lower, trim, height=.99):
x, y, z = location
width, depth, tall = size
bottom = z - tall / 2
cube(name + " / oil paint", (x, y, (bottom + height)/2), (width, depth, height-bottom), lower)
cube(name + " / limewash", (x, y, (height+z+tall/2)/2), (width, depth, z+tall/2-height), upper)
cube(name + " / baseboard", (x, y, bottom+.04), (width+.01, depth+.01, .08), trim, .003)
def radiator(loc, ivory, steel):
x, y, z = loc
for i in range(11):
xx = x + .061*i
cube("Cast-iron radiator / section", (xx, y, z+.19), (.053, .14, .37), ivory, .019)
for dz in (.04, .34):
piece = cylinder("Radiator / section union", (xx, y, z+dz), .027, .065, ivory, 20)
piece.rotation_euler.y = math.pi/2
tube("Exposed riser", [(x-.09,y,z-.08), (x-.09,y,z+.35), (x,y,z+.35)], .015, steel)
tube("Radiator return", [(x+.61,y,z+.04), (x+.74,y,z+.04), (x+.74,y,z-.12)], .014, steel)
def threshold_and_room():
scene = bpy.context.scene
mint = material("Dorm / worn teal oil paint", (.043,.15,.125), .48)
plaster = material("Dorm / uneven pale limewash", (.32,.29,.215), .9, .005)
wood = material("Dorm / worn amber veneer", (.20,.095,.038), .55)
metal = material("Dorm / old enamel and wire", (.13,.14,.128), .54)
pale = material("Dorm / cream painted pipes", (.53,.48,.34), .53)
ink = mat("Dorm / stencil", (.037,.046,.036), 0, .82)
cement = material("Dorm / pitted corridor cement", (.16,.155,.145), .83, .011)
print_dir = SOURCE / "printed-v6"
wallpaper = printed("Dorm / faded repeated wallpaper", print_dir / "wallpaper.png")
bsdf = wallpaper.node_tree.nodes.get("Principled BSDF")
bsdf.inputs["Metallic"].default_value = 0
bsdf.inputs["Roughness"].default_value = .88
noise_surface(wallpaper, 100, .003)
# Keep the existing desk/TV as the studio's anchors. Rebuild the visible shell.
for obj in list(scene.objects):
if obj.name.startswith(("Room / ", "Window / ", "Workshop back wall",
"Wall / crooked overlapping", "Crooked studio note backing",
"Studio / printed all-night", "Ceiling / soft white tube")):
for part in list(obj.children_recursive):
bpy.data.objects.remove(part, do_unlink=True)
bpy.data.objects.remove(obj, do_unlink=True)
two_tone("Room left side", (-1.94,-.74,1.31), (.13,2.76,2.62), plaster, mint, wood)
two_tone("Room right side", (1.91,-.74,1.31), (.13,2.76,2.62), plaster, mint, wood)
cube("Room ceiling", (0,-.73,2.65), (3.85,2.78,.1), plaster)
cube("Room backing", (0,.72,1.31), (3.85,.15,2.62), plaster)
for i in range(5):
bpy.ops.mesh.primitive_plane_add(size=1, location=(-1.55+.775*i,.632,1.35),
rotation=(math.pi/2,0,0))
obj = bpy.context.object
obj.name = "Faded wallpaper / hung strip"
obj.scale = (.779,2.63,1)
obj.data.materials.append(wallpaper)
cube("Wallpaper / pulled loose at a corner", (1.38,.62,1.09), (.27,.02,.46), plaster, .005)
# A timber window and a low cast-iron radiator replace the horizontal blinds.
glass = mat("Dorm / cold pane", (.10,.19,.27), .15, .17, emission=.9)
cube("Window / cold pane", (1.838,-.30,1.71), (.016,1.02,.79), glass, .008)
for y in (-.84,-.30,.24):
cube("Window / cream timber mullion", (1.813,y,1.71), (.061,.043,.88), pale, .006)
for z in (1.29,1.73,2.14):
cube("Window / cream timber crossbar", (1.813,-.30,z), (.061,1.12,.047), pale, .005)
cube("Window / deep sill", (1.76,-.3,1.27), (.19,1.17,.036), pale, .004)
radiator((1.28,-.22,.065), pale, metal)
# Narrow dresser for the kettle and glass jars of hardware on the sill.
cube("Room / battered veneer cupboard", (-1.60,-1.04,.66), (.49,.52,1.32), wood, .015)
for i in range(4):
z = .18+i*.29
cube("Room / crooked drawer front", (-1.346,-1.04,z), (.036,.475,.257), wood, .008)
tube("Room / wire drawer handle", [(-1.321,-1.096,z),(-1.299,-1.07,z),(-1.299,-1.01,z),(-1.321,-.984,z)], .005, metal)
glass_jar = mat("Sill / dusty glass", (.24,.30,.22), .18,.22, transmission=.45)
for i in range(4):
y = .04-i*.16
cylinder("Sill / glass jar of bolts", (1.735,y,1.33), .042,.085,glass_jar,28)
cylinder("Sill / jar enamel lid", (1.735,y,1.375), .043,.009,pale,28)
rug = printed("Room / striped runner", print_dir / "runner.png")
rug.node_tree.nodes.get("Principled BSDF").inputs["Roughness"].default_value = .95
bpy.ops.mesh.primitive_plane_add(size=1, location=(.12,-1.28,.003))
bpy.context.object.name = "Room / worn woven runner"
bpy.context.object.scale = (1.11,1.53,1)
bpy.context.object.rotation_euler.z = .13
bpy.context.object.data.materials.append(rug)
# The open doorway is real geometry. Camera passes the gap on the right.
for center, width in ((-.79,2.12),(1.57,.53)):
two_tone("Room corridor partition", (center,-2.13,1.31), (width,.17,2.62), plaster,mint,wood)
cube("Door / lintel", (.79,-2.13,2.355), (1.06,.18,.53), plaster)
for x in (.27,1.30):
cube("Door / chipped jamb", (x,-2.13,1.04), (.071,.22,2.08), wood,.009)
cube("Door / chipped header", (.79,-2.13,2.09), (1.1,.22,.076),wood,.008)
cube("Door / rubbed threshold", (.79,-2.14,.018), (1.06,.15,.036),wood,.004)
cube("Door / open veneer leaf", (1.263,-2.64,1.02), (.055,1.02,2.02),wood,.01)
for z in (.48,1.4):
cube("Door / sunken veneer panel", (1.23,-2.64,z), (.012,.71,.66),wood,.006)
cylinder("Door / handle mount", (1.225,-2.96,1.05), .029,.022,metal).rotation_euler.y = math.pi/2
tube("Door / chipped handle", [(1.225,-2.96,1.05),(1.18,-2.96,1.05),(1.18,-2.82,1.05)], .012,metal)
cube("Door / old number plate", (.03,-2.23,1.58), (.39,.019,.22), metal,.008)
lettering("Door / room number", "17", (.03,-2.244,1.605), .097,pale,(math.pi/2,0,0))
lettering("Door / studio label", "ЕБЛАН ГЕЙМС", (.03,-2.244,1.53), .039,pale,(math.pi/2,0,0))
# Tight common corridor with brown doors and exposed conduit.
cube("Corridor / concrete floor", (.69,-4.01,-.06), (2.45,4.03,.13),cement)
cube("Corridor / ceiling", (.69,-4.01,2.6), (2.45,4.03,.08),plaster)
for x in (-.53,1.9):
two_tone("Corridor side wall", (x,-4.05,1.29), (.14,3.83,2.58), plaster,mint,wood)
for y in (-3.45,-4.72):
cube("Corridor / old door frame", (-.445,y,1.035), (.06,.94,2.07),wood,.01)
cube("Corridor / painted door", (-.4,y,1.014), (.039,.84,2.02),mint,.004)
cube("Corridor / chipped door inset", (-.378,y,1.41), (.008,.67,.64),wood,.005)
tube("Corridor / door handle", [(-.365,y+.28,1.02),(-.326,y+.28,1.02),(-.326,y+.17,1.02)],.009,metal)
tube("Corridor / external conduit", [(-.45,-5.8,2.31),(-.45,-2.261,2.31),(.14,-2.261,2.31),(.14,-2.261,1.91)], .013,metal)
cube("Corridor / electricity meter", (.14,-2.245,1.92), (.16,.055,.20),metal,.005)
cube("Corridor / meter inspection glass", (.14,-2.277,1.946), (.10,.009,.062),ink,.004)
aged = printed("Corridor / patchy chalk", print_dir / "limewash.png")
aged.node_tree.nodes.get("Principled BSDF").inputs["Roughness"].default_value = .9
for x, rotation in ((-.455,math.pi/2),(1.825,-math.pi/2)):
bpy.ops.mesh.primitive_plane_add(size=1, location=(x,-4.1,1.77), rotation=(math.pi/2,0,rotation))
wall = bpy.context.object
wall.name = "Corridor / mottled wall wash"
wall.scale = (3.73,1.53,1)
wall.data.materials.append(aged)
bpy.ops.mesh.primitive_plane_add(size=1, location=(-.255,-2.225,1.39),
rotation=(math.pi/2,-.09,0))
notice = bpy.context.object
notice.name = "Corridor / taped handwritten rules"
notice.scale = (.24,.315,1)
notice.data.materials.append(printed("Corridor / house rules", print_dir / "notice.png"))
for x, z in ((-.33,1.53),(-.18,1.25)):
latch = cube("Corridor / worn tape", (x,-2.227,z), (.066,.004,.026), pale,.002)
latch.rotation_euler.y = .24
bpy.ops.mesh.primitive_plane_add(size=1, location=(.77,-2.72,.006))
bpy.context.object.name = "Corridor / threadbare doormat"
bpy.context.object.scale = (.79,.58,1)
bpy.context.object.data.materials.append(rug)
for y in (-3.93,-2.54):
glass = mat("Corridor / opal ceiling shade", (.64,.50,.3), 0,.46,emission=3)
cylinder("Corridor / ceiling cover", (.64,y,2.529), .112,.033,glass,40)
area("Corridor / ceiling lamp", (.64,y,2.484), (.60,y,1.1), 46, (1,.76,.46), .48)
area("Corridor / wall bounce", (.65,-4.74,1.41), (.3,-2.16,1.4), 17,(1,.75,.5),1)
# Dusty room light comes from the window, desk shade and hallway doorway.
for name, power, color in (
("Practical / desk bulb", 52, (1,.72,.46)),
("Practical / frosted window", 83, (.38,.61,1)),
("Practical / tired ceiling tube", 22, (.74,1,.83)),
("Soft room bounce", 24, (.78,.73,.61)),
("Low reflected window fill", 7, (.49,.67,.87)),
):
light = bpy.data.objects[name]
light.data.animation_data_clear()
light.data.energy = power
light.data.color = color
area("Room / warm doorway bounce", (.87,-1.99,1.61), (.15,-.56,.8), 26,(1,.61,.36),.72)
area("Room / faint plum return", (-1.4,-.42,1.52),(-.4,.1,.93), 8,(.73,.45,.57),.53)
scene.view_settings.look = "AgX - Medium High Contrast"
scene.view_settings.exposure = .25
def film_camera(name, keys, aperture):
"""Bank the camera around its view axis with timed world-space poses."""
scene = bpy.context.scene
data = bpy.data.cameras.new(name)
camera = bpy.data.objects.new(name, data)
scene.collection.objects.link(camera)
camera.rotation_mode = "QUATERNION"
data.dof.use_dof = True
data.dof.aperture_fstop = aperture
previous = None
for frame, position, target, roll, lens in keys:
direction = Vector(target) - Vector(position)
rotation = direction.to_track_quat("-Z", "Y") @ Quaternion((0,0,1), math.radians(roll))
if previous is not None and previous.dot(rotation) < 0:
rotation.negate()
previous = rotation
camera.location = position
camera.rotation_quaternion = rotation
data.lens = lens
data.dof.focus_distance = direction.length
camera.keyframe_insert("location", frame=frame)
camera.keyframe_insert("rotation_quaternion", frame=frame)
data.keyframe_insert("lens", frame=frame)
data.keyframe_insert("dof.focus_distance", frame=frame)
marker = scene.timeline_markers.new(name, frame=keys[0][0])
marker.camera = camera
return camera
def cameras():
scene = bpy.context.scene
for mark in list(scene.timeline_markers):
scene.timeline_markers.remove(mark)
for obj in list(scene.objects):
if obj.type == "CAMERA" or obj.name.endswith(" / focal plane"):
bpy.data.objects.remove(obj, do_unlink=True)
film_camera("01 / Corridor and doorway", [
(1, (.34,-5.45,1.23), (.68,-2.8,1.35), -8, 25),
(13,(.58,-4.15,1.34), (.85,-2.55,1.36), 3, 24),
(27,(.81,-2.93,1.36), (.54,-1.27,1.17), 9, 27),
(38,(.83,-2.59,1.43), (.26,-2.06,1.49), 10, 28),
(48,(.83,-2.48,1.44), (.31,-1.89,1.40), 8, 27),
(58,(.89,-1.91,1.4), (-.33,-.04,1.11), -7, 24),
(68,(1.04,-1.07,1.29),(-.16,.04,1.06), -13, 23),
], 7.1)
film_camera("02 / Falling energy can", [
(69,(1.26,-.85,1.015), (.838,-.322,.847), -7, 39),
(76,(1.19,-.76,.958), (.84,-.388,.828), 4, 42),
(94,(1.17,-.74,.927), (.842,-.432,.814), 7, 42),
(107,(1.19,-.75,.79), (.842,-.523,.59), 0, 38),
(116,(1.24,-.78,.60),(.84,-.575,.17), -8, 33),
], 5.6)
film_camera("03 / Across the workbench", [
(117,(-1.22,-.73,1.48),(-.64,-.19,.81),-14,30),
(128,(-.97,-.74,1.22),(-.39,-.15,.82),-5,29),
(139,(-.71,-.69,1.12),(-.12,-.17,.87),8,34),
(154,(-.55,-.70,1.11),(-.07,-.13,.97),5,36),
(172,(-.29,-.76,1.21),(-.084,-.09,1.06),0,39),
], 6.3)
film_camera("04 / Studio logo on CRT", [
(173,(.17,-1.39,1.25),(-.08,-.10,1.07),2,48),
(186,(.11,-1.28,1.22),(-.09,-.10,1.075),-2,49),
(234,(.071,-1.16,1.19),(-.085,-.10,1.075),0,51),
(288,(.065,-1.14,1.19),(-.085,-.10,1.075),0,51),
], 6.3)
def action():
scene = bpy.context.scene
for obj in list(scene.objects):
# Retire the earlier version's can trajectory.
if obj.name.startswith("Energy / complete") and obj.animation_data:
obj.animation_data_clear()
obj.location = (-.72,-.30,.824)
obj.rotation_mode = "XYZ"
obj.rotation_euler = (math.pi/2,0,1.5)
for shape in bpy.data.shape_keys:
if shape.animation_data:
shape.animation_data_clear()
meshes = detailed_props()
can = bpy.data.objects.new("Energy / rolling can", None)
scene.collection.objects.link(can)
fit_beverage(can, 0, meshes, centered=True)
can.rotation_mode = "QUATERNION"
lying_sideways = Quaternion((0,1,0), math.pi/2)
poses = [(1,-.31,.823,0), (69,-.31,.823,0), (77,-.391,.823,0),
(94,-.439,.823,0), (101,-.475,.746,16), (108,-.52,.551,47),
(115,-.558,.12,71), (117,-.57,.068,18), (124,-.597,.036,0),
(133,-.62,.033,0)]
previous = None
for frame, y, z, tilt in poses:
# The long axis lies along X. Moving along -Y rotates around X by
# distance over radius until the can leaves the edge of the desk.
roll = (-.31-y)/CAN_RADIUS
rotation = Quaternion((0,1,0),math.radians(tilt)) @ lying_sideways @ Quaternion((0,0,1),roll)
if previous is not None and previous.dot(rotation)<0:
rotation.negate()
can.location = (.84,y,z)
can.rotation_quaternion = rotation
can.keyframe_insert("location", frame=frame)
can.keyframe_insert("rotation_quaternion", frame=frame)
previous = rotation
lamp = bpy.data.objects["Practical / tired ceiling tube"]
for frame, power in ((1,22),(126,22),(129,13),(132,24),(136,22),(288,22)):
lamp.data.energy = power
lamp.data.keyframe_insert("energy",frame=frame)
crt = bpy.data.objects["Practical / CRT light spill"]
crt.data.animation_data_clear()
for frame, power in ((1,0),(149,0),(152,4.6),(154,1.2),(159,2.6),(210,3.6),(288,3.6)):
crt.data.energy = power
crt.data.keyframe_insert("energy",frame=frame)
shader = bpy.data.materials["Original terminal / real curved phosphor glass"]
tex = next(n for n in shader.node_tree.nodes if n.type == "TEX_IMAGE")
tex.image = bpy.data.images.load(str(SOURCE / "screen-v6/screen_0001.png"), check_existing=True)
tex.image.filepath = "//screen-v6/screen_0001.png"
tex.image.source = "SEQUENCE"
tex.image_user.frame_duration = END
tex.image_user.frame_start = 1
tex.image_user.use_auto_refresh = True
shader.node_tree.nodes.get("Principled BSDF").inputs["Emission Strength"].default_value = 2.05
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--build", action="store_true")
parser.add_argument("--proof", nargs="*", type=int)
parser.add_argument("--animation", action="store_true")
args = parser.parse_args(sys.argv[sys.argv.index("--")+1:] if "--" in sys.argv else [])
WORK.mkdir(parents=True, exist_ok=True)
path = SOURCE / "eblan-workshop-v6.blend"
bpy.ops.wm.open_mainfile(filepath=str(SOURCE / "eblan-workshop-v5.blend" if args.build else path))
scene = bpy.context.scene
if args.build:
threshold_and_room()
cameras()
action()
scene.frame_start, scene.frame_end = 1, END
scene.render.resolution_x, scene.render.resolution_y = 1920,1080
scene.render.resolution_percentage = 100
scene.render.fps = 24
scene.frame_set(67)
prefs = bpy.context.preferences.addons["cycles"].preferences
prefs.compute_device_type = "OPTIX"
prefs.get_devices()
for device in prefs.devices:
device.use = device.type == "OPTIX"
scene.cycles.device = "GPU"
scene.cycles.denoising_use_gpu = True
scene.cycles.samples = 96
scene.cycles.adaptive_min_samples = 28
scene.cycles.adaptive_threshold = .028
scene.render.use_motion_blur = True
scene.render.motion_blur_shutter = .38
if args.build:
bpy.ops.file.pack_all()
bpy.ops.wm.save_as_mainfile(filepath=str(path))
if args.proof:
scene.render.resolution_percentage = 60
scene.cycles.samples = 40
for frame in args.proof:
scene.frame_set(frame)
scene.render.filepath = str(WORK / f"proof-{frame:04}.png")
bpy.ops.render.render(write_still=True)
if args.animation:
(WORK / "frames").mkdir(exist_ok=True)
scene.render.filepath = str(WORK / "frames/frame_")
bpy.ops.render.render(animation=True)
if __name__ == "__main__":
main()