
    Hj	.                    x   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlZdZdZdZd	Zd!d
Zd!dZd!dZdZdZ	 d"	 	 	 	 	 d#dZddddiddiddidg dddddidddidddddiddiddiddiddidg ddddddiddddg dddZd$dZd%dZ	 	 	 	 d&	 	 	 	 	 	 	 	 	 d'dZ	 	 d(	 	 	 	 	 	 	 d)dZd*d Zy)+uJ  실내/실외 공용 체인 단계 — 임시샷 → (VLM 분석|i2i 윤곽) → 배경 재생성 → 최종샷.

경로 정의 (fractional A/B — Codex 의견 수용):
  A: 직행 — 환경배경 + 배치도(control) + passport → 최종샷
  B: 임시샷 → VLM 구도 텍스트 분석 → 배경 재생성 → 최종샷
  C: 임시샷 → i2i 윤곽(contour) → 배경 재생성 → 최종샷 (사용자 지정 주경로)
ref 라벨 = 단계별 SOT 위계 명시(이미지 참조끼리 싸우지 않게).
시나리오 중립: 모든 구체 내용은 데이터 문자열 주입.
    )annotationsN)Path)AnyDictListOptionalTupleu   ENVIRONMENT BACKGROUND reference — the empty environment: its space, architecture, materials and lighting are the ground truth. It contains NO people.u   CAMERA AND PLACEMENT DIAGRAM (top-down control image) — use it ONLY to understand where the camera stands, what it looks at, and where each person is. NEVER copy its drawing style, lines, circles, letters or any marks into the output.u   COMPOSITION GUIDE (line contour) — the framing, perspective and layout ground truth. Follow its composition exactly; do not copy its line style.z7IDENTITY REFERENCE photo of the person referred to as: c                l   | j                  d      xs i }g }|j                  d      r|j                  d|d           |j                  d      r|j                  d|d           |j                  d      r|j                  d|d           |j                  d      xs g D ]+  }|j                  d	t        j                  |d
      z          - |j                  d      xs i }|j                  d      r,|j                  dt        j                  |d   d
      z          dj	                  |      S )Nstagingcamera_directionzCAMERA: framing_scalez	FRAMING: lighting_moodz
LIGHTING: character_angleszPERSON: Fensure_asciiframe_spatial_contractconstraintszSPATIAL CONSTRAINTS: 
)getappendjsondumpsjoin)sstpartscafscs        forest_chain.pystaging_linesr       s   	
y		RBE	vv !x#5 6789	vvoyO!4 567	vvoz"_"5!678vv()/R/Z$**Re"DDE 0
&&)
*
0bC
ww},zz#m"45IJ 	K99U    c                    d| j                  d       g}| j                  d      r&|j                  ddj                  | d                 dj                  |      S )NzSHOT CONTENT: description
characterszPEOPLE IN SHOT: , r   )r   r   r   )r   liness     r   shot_content_linesr'   1   sX    aeeM2345Euu\'		!L/(B'CDE99Ur!   c                    | j                  d      xs g D cg c]  }t        |t              s| }}|r!dt        |       ddj	                  |       dS yc c}w )u`   인원 계약 — 데이터(characters)로만 결정. 1.5라운드 수정(군중 발명 차단).r$   zPEOPLE COUNT CONTRACT: exactly u    person(s) in frame — r%   zd. Absolutely no other people, bystanders or figures anywhere in the frame, foreground or background.u   PEOPLE COUNT CONTRACT: this shot contains NO people at all — the frame must be completely unpopulated, with no figures anywhere.)r   
isinstancestrlenr   )r   nnamess      r   people_contractr.   8   sr    |,22J21z!S7IQ2EJ1#e* >		%() *OO 	PO Ks
   AAu   Any circled letters, numbers or legend text on the control diagram are SITE MARKERS only — they are never people and must not become people or objects in the output.u	  CAMERA HEIGHT CONTRACT: the camera stands ON the ground INSIDE the scene at human eye height (about 1.6 m). Sky or horizon must be visible where the view allows it. Do NOT render an overhead, elevated, aerial or drone view — this is a ground-level cinematic shot.c                    dj                  dddddt        |      t        |      dg|rt        |      dgz   ng ddd	d
      }t	        j
                  | |t        |ft        |fg|      S )u`   1차 임시샷 — 구도/표현 발견 전용. passport 미첨부(정체성 무관), 실인간.r   zHCreate a photorealistic cinematic still that stages this shot inside thezGgiven environment. This is a COMPOSITION DRAFT: the people are ordinaryuI   real humans whose identity does not matter — focus on a strong, naturalz:composition that matches the camera and placement exactly. zDKeep the environment's architecture, materials and lighting from thezGenvironment reference. Place the camera and every person exactly as thezGcontrol diagram indicates. Absolutely no text, letters, circles, arrowszor diagram marks in the output.out_path)r   r'   r    listFimg_nb2LBL_ENVLBL_CONTROL)tagr   env_pngcontrol_pngr2   	contractsprompts          r   	temp_shotr=   N   s     YYRQSD
1a
 %.$y/RD
 2 	O 	R 	R 	* F 99S&(;*DE&( (r!   objecttypestring)heightangle	lens_feelF)r?   
propertiesrequiredadditionalPropertiesarray)r?   items)labelscreen_zonedepth_planeposesupport_contactzUwhat the EMPTY background must contain/frame so the subjects can be re-inserted later)r?   r#   )camerahorizon_and_depthmajor_surfacessubjectsocclusion_orderempty_background_requirementsc                    dddt        j                  |      g}t        j                  dd|t        d      }t        j                  d|  |       |S )	Ntexta8  Analyse this film still's composition precisely. Describe the camera, horizon/depth, major visible surfaces, each subject's screen position / depth / pose / support contact, occlusion order, and what an EMPTY background plate (no people) must contain so the people could be re-inserted with the same composition.)r?   rU   forest_vlm_compz%You analyse cinematography precisely.gpt)modelcomp_)r4   png_data_urlllmVLM_COMP_SCHEMA	save_plan)r8   temp_pngusercomps       r   vlm_compositionra      sY    6	7 	
x D 55?%Le5DKK%ut$Kr!   c                :    d}t        j                  | ||g|      S )Nuh  Convert the attached photograph into a clean LINE-ART CONTOUR drawing: thin dark ink lines on a plain white background. Preserve the EXACT composition, framing, perspective and proportions — trace the outline of every structure, surface edge and human figure as it appears. No shading, no colour fills, no photo texture, no text, no added or removed objects.refsr2   )r4   img_gpt)r8   r^   r2   r<   s       r   contourrf      s&    	S  99S&zHEEr!   c                r   ddddt        |      t        |      dg}||dt        j                  |d      dgz  }||g dz  }|r|g d	z  }n|g d
z  }|r|t	        |      z  }|dgz  }g }	||	j                  |       |	j                  |       t        j                  | dj                  |      |	|      S )uf   최종 배경 재생성 — 빈 환경(±마네킹). comp(텍스트) 또는 contour(이미지) 기반.zCCreate the photorealistic EMPTY background plate for one film shot:zCthe environment exactly as the environment reference shows it (samez@architecture, materials, lighting), seen from the shot's camera.r0   z>The required composition, extracted from a draft of this shot:Fr   )u:   Follow the attached line contour's composition EXACTLY —z2same framing, perspective and structure placement.r0   )zDWhere each person would stand or sit, place a featureless matte-greyzCartist mannequin in that exact pose and scale (no face, no clothingu7   detail) — everything else stays an empty environment.)uD   The plate must contain NO people, no figures, and no silhouettes —z?just the environment, composed so the described people could bezinserted later.z>Absolutely no text, letters, circles, arrows or diagram marks.r   rc   )	r'   r    r   r   r3   r   r4   re   r   )
r8   r   r9   r2   r`   contour_png	mannequinr;   baserd   s
             r   bg_regenrk      s     	NMJ
q!=#3R	D QDu5r; 	; K 	K 
 	
 	 
 	

 YMNNDDK KK99S$))D/xHHr!   c                   t         dz   |fg}||j                  t        |f       |j                         D ](  \  }}	|j                  t        |z   t        |	      f       * dj                  ddt        |      t        |      dg|rt        |      dgz   ng ddddd	d
d      }
t        j                  | |
||      S )uN   최종샷 — 배경 + passport(정체성 SOT) + (경로 A만 control 첨부).zJ Use it as the shot's background, matching its space and lighting exactly.r   zBCreate the FINAL photorealistic cinematic film still of this shot.r0   uF   Stage the shot inside the attached environment background — keep itszDarchitecture, materials and lighting exactly. Every person is a REALzDhuman being, photorealistic, matching their identity reference photozD(face, hair, build). Natural film lighting and grain consistent withzthe environment.zCAbsolutely no text, letters, circles, arrows, diagram marks, and noz7mannequin-like or statue-like appearance on any person.r1   )r6   r   r7   rH   LBL_IDENTITYr   r   r'   r    r3   r4   r5   )r8   r   bg_png	passportsr2   r:   r;   rd   rI   pr<   s              r   
final_shotrq      s    &- 0Q &Q%+%- $.D [+./OO%q\E)4734 &YYL
q!=#3R %.$y/RD
 2 	Q	
 	O 	O 	O 	 	N 	B F 99S&$::r!   c                    | j                  d      xs i }i }|j                  d      xs g D ]C  }|j                  |      }|r|| d   v r| d   |   ||<   )t        j                  dd|d       E |S )u]   샷 등장 인물명 → passport 경로. 이름↔short_id 매핑은 DB 이름으로 조인.character_name_to_sidr$   ro   warnzno passport for character )kindmsg)r   r4   runlog)reconr   name_to_sidoutnamesids         r   passports_for_shotr}      s     ))34:K
C|$**ood#3%,,k*3/CIHHf/I$-RST + Jr!   )r   Dict[str, Any]returnr*   )N)r8   r*   r   r~   r9   r   r:   r   r2   r   r;   Optional[List[str]]r   r   )r8   r*   r^   r   r   r~   )r8   r*   r^   r   r2   r   r   r   )NNFN)r8   r*   r   r~   r9   r   r2   r   r`   zOptional[Dict[str, Any]]rh   Optional[Path]ri   boolr;   r   r   r   )NN)r8   r*   r   r~   rn   r   ro   Dict[str, str]r2   r   r:   r   r;   r   r   r   )rx   r~   r   r~   r   r   )__doc__
__future__r   r   pathlibr   typingr   r   r   r   r	   
forest_libr4   r6   r7   LBL_CONTOURrm   r    r'   r.   MARKER_NOT_PEOPLEEYE_LEVEL_CONTRACTr=   r\   ra   rf   rk   rq   r}    r!   r   <module>r      s   #   3 3 7! I$PI I  04((,(8<(2 #x(FH3E (+4- 9$)	+
 %h/#*fh5GH$h'"H-"H-X& &1Q3,$)0+ , #H-2:?*@%,E!5< F /3+/$.2	%I+%I(%I %I ,	%I 8<	%IR >B04;;,:;-;9=;6r!   