"""依頼228:⑧「車いすに道を譲る」の台本 closeup228_script.json を作る。 175 の台本(closeup175_script.json)から、廊下の部屋・色・光・看護師の値・画面の値を写し、場面は新しい "yield" 1 つだけにする。 ・大城さん:175 の手づくりの歩き(Walk・walk_steps は make_script217.py から写した)を、押す速さ(0.8 m/秒)と歩幅(周期 0.5 秒)に合わせた。 両手は車いすの握り(WC_Parts の黒い筒・左右 0.45 m)に置く(的は看護師の根から。車いすは看護師の根に固く付いて動くので、握りは看護師の根から動かない) ・車いす:根は看護師の根の前 wc_ahead_m(Unity で看護師の根から置く)。茂さんは車いすの子 ・ハコブくん:台本の robot_rule の決まりで Unity の中で動く(Closeup228.cs)。止まる時刻は times.json(1 回めの走り)から ・声:L4 ハコブ「お先にどうぞ」・L5 大城「ありがとう」。口は 217・218 と同じ作り方 使い方:python3 make_script228.py <175 の台本> <出力の台本> <声の wav のフォルダ> [times.json] """ import json, math, os, sys, wave import numpy as np def E(k, d): v = os.environ.get(k) return type(d)(v) if v is not None else d src, dst, wavdir = sys.argv[1], sys.argv[2], sys.argv[3] times = json.load(open(sys.argv[4])) if len(sys.argv) > 4 and os.path.exists(sys.argv[4]) else {} S = json.load(open(src)) FPS = 30; DT = 1.0 / FPS O = list(S["corridor"]["origin"]) LANE_C = E("LANE_C", 0.42) # 車いすの中心の横の位置(廊下の中心から。左側通行=+x の側) LANE_R = E("LANE_R", -0.33) # ハコブくんのふだんの横の位置(左寄りの通り道) V_C = E("V_C", 0.8) # 押す速さ V_PASS = E("V_PASS", 0.65) # 横を通りすぎる時の速さ STEP_T = E("STEP_T", 0.5) # 1 歩の時間(押す歩き。歩幅 0.4 m) GRIP_Z = 0.3336 # 握りの中心:車いすの根から後ろ(Unity で測った) GRIP_X, GRIP_Y = 0.225, 0.910 GRIP_R = 0.0155 D_PUSH = E("D_PUSH", 0.26) # 握りの中心と看護師の根の前後の距離 WC_AHEAD = GRIP_Z + D_PUSH # 車いすの根:看護師の根の前 WC_FRONT = 0.64 # 足のせの前の端:車いすの根から前 ROBOT_FRONT, ROBOT_REAR = 0.382, 0.380 NURSE_BACK = 0.16 # 看護師の体の後ろの端:根から後ろ(めやす) RZ0 = E("RZ0", -1.6) # ハコブくんの始めの z(中心) NZ0 = E("NZ0", 20.1) # 看護師の根の始めの z TV0 = E("TV0", 1.0) # 動画の始まり(台本の秒) GAP = E("GAP", 0.55) # セリフの間 SPINE, CHEST = E("SPINE", 4.0), E("SPINE_C", 3.0) # 上体を前へ(合わせて 7°) END_AFTER = E("END_AFTER", 4.2) # 走り出しから動画の終わりまで(めやす。画面の外へ出たら終わり=times.json の exit_t があればそれ) def smooth(u): u = min(1, max(0, u)); return u * u * (3 - 2 * u) # ---------------------------------------------------------------- 声 LINES = [("L4", "robot", "L4_hakobu.wav", "お先にどうぞ"), ("L5", "nurse", "L5_oshiro.wav", "ありがとう")] SR = 48000 def load_wav(p): w = wave.open(p); x = np.frombuffer(w.readframes(w.getnframes()), np.int16).astype(np.float64) / 32768.0 assert w.getframerate() == SR and w.getnchannels() == 1, p return x def voiced(x, th_db=-45.0): hop = 480 e = np.array([np.sqrt(np.mean(x[i:i + hop] ** 2)) for i in range(0, len(x) - hop, hop)]) on = np.where(20 * np.log10(e + 1e-9) > th_db)[0] return on[0] * hop / SR, (on[-1] + 1) * hop / SR clips = {} for name, who, fn, text in LINES: x = load_wav(os.path.join(wavdir, fn)); v0, v1 = voiced(x) clips[name] = {"who": who, "file": fn, "text": text, "x": x, "v0": v0, "v1": v1, "dur": v1 - v0, "len": len(x) / SR} # ---------------------------------------------------------------- ハコブくんの決まり(Closeup228.cs と同じ値) RULE = { "about": "ハコブくんの決まり(依頼228。175 の考え方:近い→減速して寄る・すき間が取れない→止まる)。値は Closeup228.cs が読む", "cruise_mps": 0.6, "slow_mps": 0.5, "accel_max_mps2": 0.4, "decel_max_mps2": 0.4, "jerk_max_mps3": 1.0, "detect_range_m": 6.0, "react_s": 0.1, "margin_m": E("MARGIN", 2.4), "gap_slow_m": 0.40, "gap_stop_m": 0.15, "lane_x": LANE_R, "wall_clear_m": 0.053, "lateral_max_mps": 0.15, "lateral_angle_deg": 15.0, "lateral_accel_mps2": 0.25, "lateral_gain": 2.0, "lateral_done_m": 0.03, "resume_lateral_angle_deg": 10.0, "turn_rate_dps": 30.0, "corner_half_len_m": 0.38, "corner_slack_m": 0.006, "pass_behind_m": 1.0, } # ---------------------------------------------------------------- 大城さん・車いすの道(根の z) t_stop = times.get("stop_t", 13.5) # ハコブくんが止まった(1 回めの走り) z_rstop = times.get("robot_stop_z", 5.8) # 止まった中心の z x_rstop = times.get("robot_stop_x", -0.47) t_resume = times.get("resume_t", t_stop + 8.0) t_exit = times.get("exit_t", t_resume + END_AFTER) z_rfront, z_rrear = z_rstop + ROBOT_FRONT, z_rstop - ROBOT_REAR TW0, RAMP = 0.2, 1.0 NT = 60.0 def build_path(slow0_z, slow1_z, T_SL=0.8): """速さ:0.2〜1.2 秒で 0.8 へ。足のせの前の端が slow0_z を過ぎたら 0.65 へ(T_SL 秒で)、看護師の体の後ろの端が slow1_z を過ぎたら 0.8 へ""" z = NZ0; out = []; v = 0.0; ph = 0; ts = None for f in range(int(NT * FPS) + 1): t = f * DT if t < TW0: vt = 0.0 elif t < TW0 + RAMP: vt = V_C * smooth((t - TW0) / RAMP) else: front = z - WC_AHEAD - WC_FRONT; back = z + NURSE_BACK if ph == 0 and front <= slow0_z: ph = 1; ts = t if ph == 1 and back <= slow1_z: ph = 2; ts = t if ph == 0: vt = V_C elif ph == 1: vt = V_C + (V_PASS - V_C) * smooth((t - ts) / T_SL) else: vt = V_PASS + (V_C - V_PASS) * smooth((t - ts) / T_SL) if f > 0: z -= 0.5 * (v + vt) * DT v = vt out.append((t, z, v)) return out SLOW0 = z_rfront + E("SLOW_AHEAD", 1.0) path = build_path(SLOW0, z_rrear) def nz(t): return path[min(len(path) - 1, int(round(t * FPS)))][1] # セリフ(声の始まり) on = {} on["L4"] = t_stop + E("L4_AFTER", 0.3) on["L5"] = on["L4"] + clips["L4"]["dur"] + GAP end = {k: on[k] + clips[k]["dur"] for k in on} band_on = t_stop + E("BAND_AFTER", 0.2) happy_from = end["L5"]; happy_to = happy_from + 1.5 band_off = happy_to DUR = round(t_exit * FPS) / FPS + (0.0 if "exit_t" in times else 0.0) NF = int(round(DUR * FPS)) keys_x = [[0, LANE_C]]; keys_yaw = [[0, 180.0]] keys_z = [[round(t, 4), round(z, 5), "lin"] for t, z, v in path if t <= DUR + 1.0 + 1e-6] keys_z[0] = keys_z[0][:2] def walk_steps(t0, t1, first="R", period=STEP_T): st, t, foot = [], t0, first while t + period <= t1 + 1e-6: st.append([round(t, 4), round(t + period, 4), foot]); t += period; foot = "L" if foot == "R" else "R" return st steps = walk_steps(TW0, DUR + 0.6) # ---------------------------------------------------------------- 大城さんの目線(ハコブくんの画面へ)・口 SCREEN = (x_rstop, 0.96, z_rstop + ROBOT_FRONT - 0.01) EYE = (0.0, E("EYE_Y", 1.50), -0.09) # 看護師の根から(前=−z。上体を前へ傾けた分) BASE_NOD = E("BASE_NOD", 8.0) # 押す時は少し下を見る t_look0, t_look1 = on["L5"] - E("LOOK_LEAD", 0.6), on["L5"] - E("LOOK_LEAD", 0.6) + 0.35 t_back0 = end["L5"] + E("LOOK_HOLD", 0.35); t_back1 = t_back0 + 0.45 def look_w(t): if t < t_look0 or t > t_back1: return 0.0 if t < t_look1: return smooth((t - t_look0) / (t_look1 - t_look0)) if t > t_back0: return 1 - smooth((t - t_back0) / (t_back1 - t_back0)) return 1.0 hy_keys, hn_keys = [[0, 0.0]], [[0, BASE_NOD]] f0, f1 = int(t_look0 * FPS) - 1, int(t_back1 * FPS) + 2 for f in range(max(1, f0), f1 + 1): t = f * DT; w = look_w(t) ex, ez = LANE_C, nz(t) + EYE[2] dx, dz, dy = SCREEN[0] - ex, SCREEN[2] - ez, SCREEN[1] - EYE[1] want_yaw = math.degrees(math.atan2(dx, dz)); hy = ((want_yaw - 180.0 + 180) % 360) - 180 pitch = math.degrees(math.atan2(-dy, math.hypot(dx, dz))) hy_keys.append([round(t, 4), round(w * hy, 3), "lin"]) hn_keys.append([round(t, 4), round(BASE_NOD * (1 - w) + w * pitch, 3), "lin"]) hy_keys.append([round((f1 + 1) * DT, 4), 0.0]); hn_keys.append([round((f1 + 1) * DT, 4), BASE_NOD]) t_smile = t_look0 nurse_mouth_base = [[0, "flat"], [round(t_smile, 4), "smile"]] # ---------------------------------------------------------------- 茂さん:顔をハコブくんへ(20〜30°)・smile・うなずき 1 回 M_YAW = E("M_YAW", 25.0); M_NOD = E("M_NOD", 10.0) m_t0 = on["L5"] - E("M_LEAD", 0.45); m_t1 = m_t0 + 0.45 nod_t0 = on["L5"] + E("NOD_AFTER", 0.25); NOD_DOWN, NOD_HOLD, NOD_BACK = 0.28, 0.10, 0.32 m_back0 = end["L5"] + E("M_HOLD", 0.9); m_back1 = m_back0 + 0.6 m_yaw = [[0, 0.0], [round(m_t0, 4), 0.0], [round(m_t1, 4), M_YAW], [round(m_back0, 4), M_YAW], [round(m_back1, 4), 0.0]] m_nod = [[0, 0.0], [round(nod_t0, 4), 0.0], [round(nod_t0 + NOD_DOWN, 4), M_NOD], [round(nod_t0 + NOD_DOWN + NOD_HOLD, 4), M_NOD], [round(nod_t0 + NOD_DOWN + NOD_HOLD + NOD_BACK, 4), 0.0]] m_mouth = [[0, "flat"], [round(m_t0 + 0.15, 4), "smile"]] # ---------------------------------------------------------------- 声の並び・口(217 と同じ) tracks = {"nurse": np.zeros(int(DUR * SR) + SR), "robot": np.zeros(int(DUR * SR) + SR)} place = {} for k, c in clips.items(): s0 = on[k] - c["v0"]; i0 = int(round(s0 * SR)) tracks[c["who"]][i0:i0 + len(c["x"])] += c["x"] place[k] = {"who": c["who"], "file": c["file"], "text": c["text"], "clip_start_s": round(s0, 4), "voice_on_s": round(on[k], 4), "voice_off_s": round(end[k], 4), "voice_len_s": round(c["dur"], 4), "clip_len_s": round(c["len"], 4)} HOP = SR // FPS def frame_rms(x): return np.array([np.sqrt(np.mean(x[i * HOP:(i + 1) * HOP] ** 2)) if i * HOP < len(x) else 0 for i in range(NF)]) LADDER = ["closed", "small", "mid", "large"] def ladder(who, rest, speak): rms = frame_rms(tracks[who]) ref = np.percentile(rms[rms > 0.01], 90) if (rms > 0.01).any() else 1.0 lvl = np.convolve(rms / ref, np.ones(3) / 3, mode="same") target = np.where(speak, np.digitize(lvl, [0.15, 0.35, 0.65]), 0) for i in range(NF): if not speak[i]: target[i] = rest[i] for i in range(1, NF - 1): if target[i] != target[i - 1] and target[i] != target[i + 1]: target[i] = target[i - 1] step = np.zeros(NF, int) for i in range(NF): prev = step[i - 1] if i else rest[0] step[i] = prev + int(np.clip(target[i] - prev, -1, 1)) if i % 2 == 0 else prev return step def speak_mask(names): m = np.zeros(NF, bool) for k in names: m[int(math.floor(on[k] * FPS)):int(math.ceil(end[k] * FPS))] = True return m r_step = ladder("robot", np.full(NF, 2), speak_mask(["L4"])) # 休む口は「基本」(mid) robot_mouth = [LADDER[s] for s in r_step] n_step = ladder("nurse", np.zeros(NF, int), speak_mask(["L5"])) nurse_mouth = [int(s) for s in n_step] mix = (tracks["nurse"] + tracks["robot"])[: int(round(DUR * SR))] audio_out = os.environ.get("AUDIO_OUT", "") if audio_out: pk = np.max(np.abs(mix)); assert pk < 1.0, pk w = wave.open(audio_out, "w"); w.setnchannels(1); w.setsampwidth(2); w.setframerate(SR) w.writeframes((np.clip(mix, -1, 1) * 32767).astype(np.int16).tobytes()); w.close() # ---------------------------------------------------------------- カメラ(廊下の原点から) zc = z_rstop + E("CAM_C", 0.7) CAM = { "main": {"pos": [-0.62, 1.72, round(zc - E("CAM_BACK", 6.0), 4)], "look": [0.12, 0.8, round(zc + 0.9, 4)], "fov": 38}, "screen_close": {"follow": "robot", "pos": [x_rstop, 1.23, 1.15], "look": [x_rstop, 0.962, 0.29], "fov": 24}, "face": {"pos": [E("FACE_X", -0.70), E("FACE_Y", 1.30), round(z_rfront + E("FACE_DZ", 0.10), 4)], "look": [E("FACE_LX", 0.40), E("FACE_LY", 1.28), round(z_rfront + E("FACE_LDZ", 2.1), 4)], "fov": E("FACE_FOV", 30.0)}, "depart": {"pos": [0.62, 1.70, round(z_rstop + E("DEP_DZ", 3.6), 4)], "look": [-0.40, 0.55, round(z_rstop + 0.3, 4)], "fov": 40}, "top": {"pos": [0.0, 7.6, round(zc - 0.3, 4)], "look": [0.0, 0.0, round(zc - 0.3, 4)], "up": [-1, 0, 0], "fov": 27}, "chk_grip": {"follow": "nurse", "pos": [round(LANE_C - 0.62, 4), 1.22, 0.05], "look": [round(LANE_C - 0.225, 4), 0.93, -0.27], "fov": 22}, "chk_grip_l": {"follow": "nurse", "pos": [round(LANE_C - 0.3, 4), 1.75, 0.30], "look": [round(LANE_C - 0.225, 4), 0.93, -0.30], "fov": 22}, "chk_wc": {"follow": "nurse", "pos": [-0.75, 1.0, -1.3], "look": [LANE_C, 0.6, -0.6], "fov": 45}, } q = lambda t: round(round(t * FPS) / FPS, 4) CL0 = q(band_on - 0.1) F0 = q(end["L4"] + E("FACE_IN", 0.25)) F1 = q(max(nod_t0 + NOD_DOWN + NOD_HOLD + NOD_BACK, end["L5"]) + 0.25) cuts = [[0, "main"], [CL0, "screen_close"], [F0, "face"], [F1, "screen_close"], [q(happy_to + 0.1), "main"], [q(t_resume + E("DEP_IN", 0.6)), "depart"]] t_pass = times.get("pass_t", t_stop + 3.5) t_seen = times.get("seen_t", t_stop - 3.3) stills = [ [q(t_seen + 0.3), "main", "s1_coming_main"], [q(t_stop + 0.1), "main", "s2_stopped_main"], [q(on["L4"] + 0.4), "screen_close", "s3_osakini_close"], [q(nod_t0 + NOD_DOWN + 0.05), "face", "s4_arigatou_nod_face"], [q(t_pass), "main", "s5_pass_main"], [q(t_pass), "top", "s6_pass_top"], [q(t_resume + 1.2), "depart", "s7_depart"], [q(t_stop + 0.1), "top", "chk_top_stopped"], [q(t_resume), "top", "chk_top_resume"], [q(happy_from + 0.5), "screen_close", "chk_screen_happy"], [q(t_seen), "chk_grip", "chk_grip"], [2.0, "chk_grip", "chk_grip_2s"], [2.0, "chk_wc", "chk_wc_2s"], [2.0, "chk_grip_l", "chk_grip_l_2s"], [q(nod_t0 + NOD_DOWN + 0.05), "chk_wc", "chk_wc_nod"], [q(t_seen), "chk_wc", "chk_wc_walk"], ] shot = {"name": "yield", "about": "⑧ 車いすに道を譲る(依頼228)", "origin": O, "duration": DUR, "video": True, "box_start": {"on": "hidden"}, "robot": {"screen": [[0, "face"]]}, "box_events": [], "card_events": [], "presses": [], "card": False, "check_open": [], "check_close": [], "check_above": [], "transitions": [], "robot_start_z": RZ0, "swing_ramp": 0.15, "lift_pow": 0.5, "nurse": {"x": keys_x, "z": keys_z, "yaw": keys_yaw, "walk": [[0, 0], [TW0, 0], [TW0 + 0.4, 1]], "spine": [[0, SPINE]], "chest": [[0, CHEST]], "head_yaw": hy_keys, "head_nod": hn_keys, "mouth": nurse_mouth_base, "talk": [[0, 0]], "L_w": [[0, 1.0]], "R_w": [[0, 1.0]], "L_target": [[0, "grip_l"]], "R_target": [[0, "grip_r"]], "L_curl": [[0, E("CURL", 30.0)]], "R_curl": [[0, E("CURL", 30.0)]], "L_thumb": [[0, E("THUMB", 20.0)]], "R_thumb": [[0, E("THUMB", 20.0)]], "L_hint": [[0, "push_l"]], "R_hint": [[0, "push_r"]], "L_hint_w": [[0, 0.5]], "R_hint_w": [[0, 0.5]]}, "steps": steps, "look_windows": [[round(t_look1, 3), round(t_back0, 3)]], "cameras": CAM, "cuts": cuts, "top_camera": "top", "stills": stills, "c228": {"wc_ahead_m": round(WC_AHEAD, 4), "video_from": TV0, "video_to": round((NF - 1) * DT, 4), "band": "c228_yield_band", "band_on": round(band_on, 4), "band_off": round(band_off, 4), "happy_from": round(happy_from, 4), "happy_to": round(happy_to, 4), "robot_mouth": robot_mouth, "nurse_mouth": nurse_mouth, "nurse_mouth_scale": [1.0, 1.2, 1.8, 2.4], "morita": {"head_yaw": m_yaw, "head_nod": m_nod, "mouth": m_mouth, "neck_share": 0.4, "blink": {"every_min_s": 3.0, "every_max_s": 5.0, "close_s": 0.15, "seed": 228}, "eye_min_scale": 0.1}, "lines": place, "speak": {k: [round(on[k], 4), round(end[k], 4)] for k in on}, "grip": {"x": GRIP_X, "y": GRIP_Y, "z_behind_root": GRIP_Z, "r": GRIP_R}}, "times": {"t_stop_used": t_stop, "z_robot_stop": z_rstop, "x_robot_stop": x_rstop, "t_resume_used": t_resume, "t_exit_used": t_exit, "slow0_front_z": round(SLOW0, 4), "slow1_back_z": round(z_rrear, 4), "nurse_z0": NZ0, "robot_z0": RZ0, "nurse_look": [round(t_look0, 3), round(t_back1, 3)], "morita_turn": [round(m_t0, 3), round(m_back1, 3)], "nod_t0": round(nod_t0, 4)}} out = {k: S[k] for k in ("fps", "width", "height", "supersample", "colors", "light", "rooms", "box", "bag", "nurse", "targets", "card", "pocket", "screen_ui", "drawer_parts", "hints", "check_stand_parts", "corridor")} out["about"] = "依頼228:⑧ 車いすに道を譲る(場面 yield)。廊下(rooms)・色・光・看護師・画面の値は 175 の台本から写した(この台本の中で変える)" out["nurse"] = dict(out["nurse"]); out["nurse"]["walk"] = dict(out["nurse"]["walk"]) out["nurse"]["walk"].update({"amp_deg": E("AMP", 11.0), "knee_deg": 24, "bob_m": 0.008, "lift_m": 0.05}) out["nurse"]["blink"] = dict(out["nurse"]["blink"]) # 握りの的(看護師の根から。手のひらを下に向けて握りの上にのせる・指は前) gz = WC_AHEAD - GRIP_Z out["targets"] = dict(out["targets"]) out["targets"]["grip_l"] = {"ref": "nurse", "pos": [round(-GRIP_X, 4), round(GRIP_Y + GRIP_R + E("PALM_UP", 0.018), 4), round(gz - E("GRIP_BACK", 0.05), 4)], "finger_frame": "nurse", "finger": [-E("FING_X", 1.0), -E("FING_Y", 0.35), 1], "palm_frame": "nurse", "palm": [E("PALM_X", 0.3), -1, 0.0]} out["targets"]["grip_r"] = {"ref": "nurse", "pos": [round(GRIP_X, 4), round(GRIP_Y + GRIP_R + E("PALM_UP", 0.018), 4), round(gz - E("GRIP_BACK", 0.05), 4)], "finger_frame": "nurse", "finger": [E("FING_X", 1.0), -E("FING_Y", 0.35), 1], "palm_frame": "nurse", "palm": [-E("PALM_X", 0.3), -1, 0.0]} out["hints"] = dict(out["hints"]); out["hints"]["push_l"] = [-0.40, 1.0, -0.20]; out["hints"]["push_r"] = [0.40, 1.0, -0.20] out["robot_rule"] = RULE out["screen228"] = {"about": "帯「お先にどうぞ」(tools/screen228.py。219・223 の帯だけの絵と同じ作り方)", "band_text": "お先にどうぞ"} out["shots"] = [shot] json.dump(out, open(dst, "w"), ensure_ascii=False, indent=1) info = dict(shot["times"]); info.update({"duration": DUR, "video_len": round(DUR - TV0, 3), "speak": shot["c228"]["speak"], "gap_L4_L5": round(on["L5"] - end["L4"], 3), "band": [band_on, band_off], "happy": [happy_from, happy_to], "cuts": cuts}) print(json.dumps(info, ensure_ascii=False))