"""Compile explicit camera travel into a visual cue without rewriting the scene.""" import re import unicodedata # Full-cue matches only. Negations, multiple actions and editing instructions go to the director. _TRAVEL = { "recule": "back", "se rapproche": "in", "avance": "in", "pulls back": "back", "moves backward": "back", "moves back": "back", "pushes in": "in", "moves closer": "in", "moves forward": "in", } _ANGLES = {"contre plongee": "low", "plongee": "high", "low angle": "low", "high angle": "high"} _CUE = re.compile( r"(?:(?:en |from a |at a )(?P" + "|".join(_ANGLES) + r"),? )?" r"(?:(?:la|the) )?camera (?P" + "|".join(_TRAVEL) + r")" r"(?:,? (?:en |from a |at a )?(?P" + "|".join(_ANGLES) + r")(?: view)?)?" ) _ANGLE_TEXT = {"low": " from a low viewpoint looking up at the subject", "high": " from a high viewpoint looking down at the subject", "": ""} _MOVES = { "back": ("pulls backward away from the subject", "The subject grows smaller in the frame as more of the surroundings come into view."), "in": ("tracks forward toward the subject", "The subject grows larger in the frame as the view tightens."), } # An exact, known first line lets a later camera cue replace the travel while retaining the angle. _SHOTS = { (move, angle): f"The camera {travel}{position}. {effect}" for move, (travel, effect) in _MOVES.items() for angle, position in _ANGLE_TEXT.items() } _PREVIOUS = {text: key for key, text in _SHOTS.items()} _CAMERA_CONTEXT = re.compile( r"\b(camera|lens|shot|viewpoint|zoom|pan|pans|tilt|dolly|tracking|framing|foreground|" r"close[- ]up|wide[- ]angle|low[- ]angle|high[- ]angle|from above|from below|bird.s.eye)\b", re.I ) def camera_direction(instruction, old, selected): scene = old["prompt"].strip() if not scene: return None plain = unicodedata.normalize("NFKD", instruction.casefold()) plain = "".join(c for c in plain if not unicodedata.combining(c)) plain = " ".join(re.sub(r"[-‐‑–]", " ", plain).strip().rstrip(".!").split()) match = _CUE.fullmatch(plain) if not match or (match["start"] and match["end"]): return None previous, split, description = scene.partition("\n") old_angle = "" if split and previous in _PREVIOUS: _, old_angle = _PREVIOUS[previous] scene = description elif "camera" in old["focus"].split("+") or _CAMERA_CONTEXT.search(scene): # The model can reconcile a nonstandard prior composition with the new direction. return None move = _TRAVEL[match["move"]] angle = _ANGLES.get(match["start"] or match["end"], old_angle) summary = ("Camera pulls back" if move == "back" else "Camera moves in") if angle: summary += f" at a {angle} angle" return selected or "append", "camera", summary, _SHOTS[move, angle] + "\n" + scene