334 lines
16 KiB
Python
334 lines
16 KiB
Python
"""Room 17 at night. Author the six-shot intro and render it offline."""
|
||
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
|
||
from build_studio_v6 import film_camera
|
||
from workshop_props import lettering, printed
|
||
from workshop_room_v5 import paper_sheet
|
||
from workshop_props_v6 import CAN_RADIUS
|
||
from workshop_relay_v7 import build_relay
|
||
from workshop_set_v7 import curled_cable, dress_room, lighting, make_shell
|
||
|
||
|
||
ROOT = HERE.parents[1]
|
||
SOURCE = ROOT/"assets/cinematics/blender"
|
||
WORK = ROOT/"build/cinematic-v7"
|
||
END = 288
|
||
|
||
|
||
def cameras():
|
||
scene = bpy.context.scene
|
||
for marker in list(scene.timeline_markers):
|
||
scene.timeline_markers.remove(marker)
|
||
film_camera("01 / Door swings in", [
|
||
(1,(.57,-6.69,1.29),(.77,-3.62,1.34),0,25),
|
||
(17,(.62,-5.18,1.36),(.73,-3.60,1.32),-2,25),
|
||
(33,(.78,-3.63,1.42),(.13,-.40,1.18),-3,24),
|
||
(49,(.30,-2.48,1.48),(-.64,-.46,1.12),0,23),
|
||
],7)
|
||
film_camera("02 / A side of the demo tape", [
|
||
(50,(-1.95,-1.83,1.13),(-2.29,-1.43,.84),1,43),
|
||
(68,(-2.04,-1.76,1.09),(-2.28,-1.44,.837),1,46),
|
||
(92,(-2.16,-1.72,1.06),(-2.25,-1.435,.836),0,48),
|
||
(121,(-2.24,-1.69,1.04),(-2.24,-1.435,.836),0,48),
|
||
],7)
|
||
# Sample the same clock for framing and action. A small lead below the
|
||
# subject leaves the desk edge visible as the can loses support.
|
||
keys = []
|
||
for frame in range(122,160):
|
||
scene.frame_set(frame)
|
||
x,y,z = bpy.data.objects["Energy / rolling can"].location
|
||
u = (frame-122)/37
|
||
keys.append((frame,(1.113-.048*u,y-.365,z+.138+.035*u),
|
||
(x,y-.01,z-.008),1,45))
|
||
film_camera("03 / Over the lip", keys,7)
|
||
film_camera("04 / Impact on the switch", [
|
||
(160,(.535,-.958,.211),(.822,-.646,.131),-2,43),
|
||
(170,(.546,-.95,.194),(.826,-.642,.101),-1,43),
|
||
(172,(.546,-.95,.194),(.826,-.642,.100),-1,43),
|
||
],7)
|
||
final_camera = film_camera("06 / Eblan Games", [
|
||
(185,(.47,-1.27,1.13),(-.086,-.11,1.074),1,40),
|
||
(213,(.19,-1.14,1.18),(-.091,-.11,1.076),0,46),
|
||
(240,(.13,-1.105,1.19),(-.091,-.11,1.076),0,47),
|
||
(288,(.12,-1.095,1.19),(-.091,-.11,1.076),0,47.4),
|
||
],5.6)
|
||
next(marker for marker in scene.timeline_markers if marker.camera==final_camera).frame = 223
|
||
|
||
|
||
def playback_rate(frame):
|
||
def ease(start,end):
|
||
u = min(1,max(0,(frame-start)/(end-start)))
|
||
return u*u*(3-2*u)
|
||
return 1-.87*ease(124,137)+.87*ease(150,162)
|
||
|
||
|
||
def can_roll():
|
||
can = bpy.data.objects["Energy / rolling can"]
|
||
# Stand the lime can on clear desktop beside the tower, beyond the rolling lane.
|
||
lime = bpy.data.objects["Energy / complete lime can.001"]
|
||
lime.location = (.96,.061,.787)
|
||
lime.rotation_euler = (0,0,-.44)
|
||
can.animation_data_clear()
|
||
can.rotation_mode = "QUATERNION"
|
||
sideways = Quaternion((0,1,0),math.pi/2)
|
||
fall_duration = math.sqrt(2*(.820-.095)/9.81)
|
||
speed = (.643-.446)/fall_duration
|
||
# Age in physical seconds, measured from the instant the can leaves the desk.
|
||
age = fall_duration-sum(playback_rate(f+.5)/24 for f in range(1,162))
|
||
# At first contact the lower chime is 70 mm below the centre. The rim
|
||
# grazes the left side of the button with the mouth still in the air.
|
||
tilt_at_contact = math.radians(38)
|
||
rim_depth = .076*math.sin(tilt_at_contact)+.029*math.cos(tilt_at_contact)
|
||
contact_z = .095+rim_depth
|
||
hit_age = math.sqrt(2*(.820-contact_z)/9.81)
|
||
previous = None
|
||
times = {}
|
||
for frame in range(1,196):
|
||
u = min(1,max(0,(frame-156)/6))
|
||
times[frame] = age-(fall_duration-hit_age)*u*u*(3-2*u)
|
||
age += playback_rate(frame+.5)/24
|
||
times[162] = hit_age
|
||
for frame,age in sorted(times.items()):
|
||
t = max(age,-.40)
|
||
# A short nudge occurs during the cassette insert. Integrating a
|
||
# smooth acceleration keeps the resting can clear of the mug handle.
|
||
rolling_time = t
|
||
if t < -.14:
|
||
u = (t+.40)/.26
|
||
rolling_time = -.27+.26*(u**3-.5*u**4)
|
||
x,y = .84,-.446-speed*rolling_time
|
||
z = .820-4.905*max(t,0)**2
|
||
spin = speed*(rolling_time+.40)/CAN_RADIUS
|
||
tilt = tilt_at_contact*min(1,max(0,t/hit_age))
|
||
yaw = math.radians(14)*min(1,max(0,(t+.08)/.08))
|
||
if frame >= 162:
|
||
# Follow the lever for its compression stroke before rebounding.
|
||
after = max(0,(frame-163)/24)
|
||
compressed = contact_z-.009*min(1,frame-162)
|
||
flight = (.63+math.sqrt(.63**2+19.62*(compressed-CAN_RADIUS)))/9.81
|
||
arc = min(after,flight)
|
||
z = compressed+.63*arc-4.905*arc**2
|
||
landed = max(0,after-flight)
|
||
z += max(0,.18*landed-4.905*landed**2)
|
||
coast = min(after,flight)+(1-math.exp(-4*landed))/4
|
||
x,y = .84+.072*coast,-.446-speed*(hit_age+coast)
|
||
spin = speed*(hit_age+.40+coast)/CAN_RADIUS
|
||
tilt = tilt_at_contact*max(0,1-after/flight)**1.4
|
||
rotation = (Quaternion((0,0,1),yaw) @ Quaternion((0,1,0),-tilt)
|
||
@ sideways @ Quaternion((0,0,1),spin))
|
||
if previous and previous.dot(rotation)<0:
|
||
rotation.negate()
|
||
can.location = (x,y,z)
|
||
can.rotation_quaternion = rotation
|
||
can.keyframe_insert("location",frame=frame)
|
||
can.keyframe_insert("rotation_quaternion",frame=frame)
|
||
previous = rotation
|
||
|
||
|
||
def seat_rim_on_switch():
|
||
"""Keep a single arc of the bottom rim seated during the lever stroke."""
|
||
scene = bpy.context.scene
|
||
can = scene.objects["Energy / rolling can"]
|
||
foot = next(part for part in can.children if "concave foot" in part.name)
|
||
# Scan the underside of the actual rolled seam. The lever has rounded
|
||
# edges and crosses the can obliquely, so its centre height is insufficient.
|
||
underside = [v.co.copy() for v in foot.data.vertices if v.co.z < .005]
|
||
poses = []
|
||
for sample in range(9):
|
||
frame = 162+sample/8
|
||
scene.frame_set(int(frame),subframe=frame%1)
|
||
depsgraph = bpy.context.evaluated_depsgraph_get()
|
||
gaps = []
|
||
for name in ("Power switch / sprung lever","Power switch / ribbed actuator"):
|
||
part = scene.objects[name].evaluated_get(depsgraph)
|
||
inv = part.matrix_world.inverted()
|
||
down = inv.to_3x3() @ Vector((0,0,-1))
|
||
for vertex in underside:
|
||
world = foot.matrix_world @ vertex
|
||
origin = inv @ Vector((world.x,world.y,.25))
|
||
hit,at,_,_ = part.ray_cast(origin,down)
|
||
if hit:
|
||
gaps.append(world.z-(part.matrix_world @ at).z)
|
||
if not gaps:
|
||
raise ValueError(f"Rim misses the switch at frame {frame}")
|
||
poses.append((frame,can.location.z-min(gaps)))
|
||
for frame,height in poses:
|
||
can.location.z=height
|
||
can.keyframe_insert("location",index=2,frame=frame)
|
||
|
||
|
||
def socket_and_flash(steel, flash):
|
||
socket = mat("Switch / worn black Bakelite",(.027,.022,.016),0,.4)
|
||
brass = mat("Switch / oxidized contacts",(.32,.22,.08),.71,.34)
|
||
rubber = mat("Switch / rolled cable rubber",(.026,.033,.038),0,.76)
|
||
indicator = mat("Switch / vacuum neon indicator",(.9,.17,.029),0,.25,emission=.08)
|
||
noise_surface(socket,220,.0006)
|
||
base_x,base_y = .84,-.630
|
||
cube("Power switch / gasketed box",(base_x,base_y,.026),(.212,.145,.052),socket,.014)
|
||
cube("Power switch / aged steel plate",(base_x,base_y,.055),(.187,.125,.008),steel,.008)
|
||
for x in (-.072,.072):
|
||
for y in (-.044,.044):
|
||
cylinder("Power switch / panel screw",(base_x+x,base_y+y,.060),.0043,.002,brass,24)
|
||
lettering("Power switch / set identifier","СЕТЬ",(base_x+.029,base_y-.042,.060),.021,brass)
|
||
cylinder("Power switch / amber status lens",(base_x-.071,base_y-.016,.063),.011,.010,indicator,32)
|
||
hinge = bpy.data.objects.new("Power switch / lever pivot",None)
|
||
bpy.context.scene.collection.objects.link(hinge)
|
||
lever = cube("Power switch / sprung lever",(0,-.037,.013),(.092,.076,.012),steel,.012)
|
||
shoe = cube("Power switch / ribbed actuator",(0,-.063,.021),(.085,.029,.009),rubber,.006)
|
||
for part in (lever,shoe):
|
||
part.parent = hinge
|
||
hinge.location = (base_x,base_y+.023,.068)
|
||
for frame,angle in ((1,-13),(162,-13),(163,1),(165,4),(169,2),(288,2)):
|
||
hinge.rotation_euler.x = math.radians(angle)
|
||
hinge.keyframe_insert("rotation_euler",frame=frame)
|
||
shader = indicator.node_tree.nodes.get("Principled BSDF")
|
||
for frame,power in ((1,.08),(161,.08),(163,4.4),(165,2.5),(288,2.7)):
|
||
shader.inputs["Emission Strength"].default_value = power
|
||
shader.inputs["Emission Strength"].keyframe_insert("default_value",frame=frame)
|
||
for frame,power in ((1,0),(163,0),(166,8),(169,1),(172,0),(288,0)):
|
||
flash.data.energy = power
|
||
flash.data.keyframe_insert("energy",frame=frame)
|
||
curled_cable("Power switch / cable disappears behind desk",
|
||
[(.753,-.608,.032),(.652,-.59,.021),(.51,-.51,.009),(.47,-.30,.009),
|
||
(.408,.14,.022),(.39,.38,.61)],.007,rubber)
|
||
|
||
|
||
def demo_tape():
|
||
# An opened cassette on the repair bench. The unspooled tape leads the
|
||
# eye across the face label towards the loose pressure pad and screws.
|
||
x,y,z = -2.25,-1.435,.825
|
||
polymer = mat("Cassette / smoky moulded shell",(.051,.042,.029),.21,.32)
|
||
ivory = mat("Cassette / nylon reels",(.64,.57,.39),.06,.4)
|
||
tape = mat("Cassette / magnetic oxide",(.085,.028,.012),.49,.29)
|
||
label = mat("Cassette / faded paper label",(.60,.47,.25),0,.85)
|
||
ink = mat("Cassette / blue ballpoint",(.022,.039,.074),0,.72)
|
||
metal = mat("Cassette / nickel plated screw",(.42,.43,.38),.86,.29)
|
||
paper = printed("Repair slip / channel trace",SOURCE/"printed-v7/channel-test.png")
|
||
paper.node_tree.nodes["Principled BSDF"].inputs["Metallic"].default_value = 0
|
||
paper_sheet("Bench / marked signal dropout",(x+.032,y+.043,z),(.139,.081),-.14,paper,.003)
|
||
copper = mat("Bench / bare copper",(.43,.145,.049),.83,.28)
|
||
for i in range(2):
|
||
px,py = x-.072+i*.009,y+.012-i*.023
|
||
points = [(px+.006*math.cos(j*.23),py+j*.00017,z+.007+.006*math.sin(j*.23)) for j in range(115)]
|
||
curled_cable("Bench / wound pickup coil",points,.00053,copper)
|
||
cube("Demo cassette / tray",(x,y,z+.005),(.107,.068,.010),polymer,.003)
|
||
for offset,outer in ((-.023,.0205),(.023,.014)):
|
||
cx = x+offset
|
||
cylinder("Cassette / tape pack",(cx,y+.006,z+.009),outer,.006,tape,96)
|
||
cylinder("Cassette / hub",(cx,y+.006,z+.012),.008,.005,ivory,64)
|
||
cylinder("Cassette / spindle opening",(cx,y+.006,z+.0145),.0053,.001,ink,48)
|
||
for tooth in range(6):
|
||
a = tooth*math.tau/6
|
||
tab = cube("Cassette / spindle tooth",(cx+.0049*math.cos(a),y+.006+.0049*math.sin(a),z+.015),
|
||
(.0025,.0015,.0011),ivory,.0003)
|
||
tab.rotation_euler.z = a
|
||
cube("Cassette / paper strip",(x,y-.021,z+.011),(.096,.019,.001),label,.001)
|
||
lettering("Cassette / handwritten side label","ОБЩАГА // ДУБЛЬ 17",(x,y-.024,z+.0116),.0065,ink)
|
||
for i,(dx,dy) in enumerate(((-.047,.028),(.048,.028),(-.068,-.037),(-.062,-.044))):
|
||
cylinder("Cassette / cover screw",(x+dx,y+dy,z+(.012 if i<2 else .0018)),.0023,.0026,metal,32)
|
||
slot = cube("Cassette / screw slot",(x+dx,y+dy,z+(.0134 if i<2 else .0032)),(.0034,.00055,.0002),ink)
|
||
slot.rotation_euler.z = i*.81
|
||
# A thin, upright ribbon with an irregular loop on the wood.
|
||
path = [(x-.023,y+.026,z+.009),(x-.051,y+.020,z+.008),(x-.079,y-.003,z+.002),
|
||
(x-.090,y-.034,z+.002),(x-.068,y-.058,z+.002),(x-.022,y-.056,z+.002),
|
||
(x+.014,y-.047,z+.003),(x+.054,y-.022,z+.006),(x+.037,y+.007,z+.009)]
|
||
ribbon = curled_cable("Cassette / spilled magnetic tape",path,.00025,tape)
|
||
ribbon.data.bevel_depth = 0
|
||
ribbon.data.extrude = .0017
|
||
area("Workbench / light across tape and wood",(-2.49,-1.20,1.32),(x,y,z),5,(1,.77,.53),.32)
|
||
|
||
|
||
def crt_screen():
|
||
screen = bpy.data.materials["Original terminal / real curved phosphor glass"]
|
||
shader = screen.node_tree.nodes.get("Principled BSDF")
|
||
shader.inputs["Emission Strength"].default_value = 2.7
|
||
shader.inputs["Roughness"].default_value = .35
|
||
texture = next(node for node in screen.node_tree.nodes if node.type=="TEX_IMAGE")
|
||
texture.image = bpy.data.images.load(str(SOURCE/"screen-v7/screen_0001.png"),check_existing=True)
|
||
texture.image.source = "SEQUENCE"
|
||
texture.image.filepath = "//screen-v7/screen_0001.png"
|
||
texture.image_user.frame_duration = END
|
||
texture.image_user.frame_start = 1
|
||
texture.image_user.use_auto_refresh = True
|
||
|
||
|
||
def set_render(scene):
|
||
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
|
||
preferences = bpy.context.preferences.addons["cycles"].preferences
|
||
preferences.compute_device_type = "OPTIX"
|
||
preferences.get_devices()
|
||
for device in preferences.devices:
|
||
device.use = device.type=="OPTIX"
|
||
scene.cycles.device = "GPU"
|
||
scene.cycles.use_denoising = True
|
||
scene.cycles.denoising_use_gpu = True
|
||
scene.cycles.samples = 72
|
||
scene.cycles.adaptive_min_samples = 20
|
||
scene.cycles.adaptive_threshold = .037
|
||
scene.cycles.max_bounces = 7
|
||
scene.cycles.volume_bounces = 1
|
||
scene.render.use_motion_blur = True
|
||
scene.render.motion_blur_shutter = .41
|
||
scene.render.image_settings.file_format = "PNG"
|
||
scene.render.filepath = bpy.path.relpath(str(WORK/"frames/frame_"), start=str(SOURCE))
|
||
|
||
|
||
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")
|
||
parser.add_argument("--resume-at",type=int,default=1)
|
||
parser.add_argument("--through",type=int,default=END)
|
||
args = parser.parse_args(sys.argv[sys.argv.index("--")+1:] if "--" in sys.argv else [])
|
||
if not 1 <= args.resume_at <= args.through <= END:
|
||
parser.error(f"render range must be within frames 1–{END}")
|
||
WORK.mkdir(parents=True,exist_ok=True)
|
||
path = SOURCE/"eblan-workshop-v7.blend"
|
||
bpy.ops.wm.open_mainfile(filepath=str(SOURCE/"eblan-workshop-v6.blend" if args.build else path))
|
||
scene = bpy.context.scene
|
||
if args.build:
|
||
steel = make_shell()
|
||
dress_room(steel)
|
||
flash = lighting()
|
||
can_roll()
|
||
socket_and_flash(steel,flash)
|
||
seat_rim_on_switch()
|
||
cameras()
|
||
demo_tape()
|
||
build_relay()
|
||
crt_screen()
|
||
set_render(scene)
|
||
scene.frame_set(69)
|
||
if args.build:
|
||
bpy.ops.file.pack_all()
|
||
bpy.context.preferences.filepaths.save_version = 0
|
||
bpy.ops.wm.save_as_mainfile(filepath=str(path))
|
||
if args.proof:
|
||
scene.render.resolution_percentage = 62
|
||
scene.cycles.samples = 32
|
||
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:
|
||
scene.frame_start, scene.frame_end = args.resume_at, args.through
|
||
(WORK/"frames").mkdir(exist_ok=True)
|
||
scene.render.filepath = str(WORK/"frames/frame_")
|
||
bpy.ops.render.render(animation=True)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|