
    [iq                       U 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	 ddl
ZddlmZ ddlmZ dd	lmZ  ee                                          j        d
         dz  dz  dz  ZdZg dZdaded<   d%dZeedfd&d Zeedfd'd$ZdS )(u  Expressive compile: blend user-authored L1/L3/L5 presets within emotion family.

Philosophy: when a turn has an emotion label, the user's authored presets ARE
the ground truth. VAD determines intensity level (L1 mild, L5 extreme) via RBF
distance weighting on anchor VAD — NOT by blending with parametric rules or
other emotion families.

Result: full expressivity of authored L5 poses at extreme VAD, faithful fall-off
to L3/L1 at milder VAD. Emotion label is dominant.
    )annotationsN)Path)DictListOptional   )LIPSYNC_ONLY)parametric_layer)validate_vad   dataemotionzemotion_vad_anchors.jsongffffff?)                  +   ,   z*Optional[Dict[str, Dict[int, np.ndarray]]]_anchor_cachereturn Dict[str, Dict[int, np.ndarray]]c                     t           t           S t          j        t                              d                    d         } i }|                                 D ]\  }}d |D             ||<   |a |S )Nzutf-8)encodinganchorsc                    i | ]<}t          |d                    t          j        |d         t          j                  =S )levelvaddtype)intnpasarrayfloat32).0es     S/dataset/kemix-engine/package/face/animasync-face-v3/scripts/compiler/expressive.py
<dictcomp>z!_load_anchors.<locals>.<dictcomp>4   s:    ]]]PQC'
OORZ%
%K%K%K]]]    )r   jsonloads_ANCHOR_PATH	read_textitems)rawoutemoentriess       r(   _load_anchorsr4   -   sy     
*\++W+==
>
>y
IC,.C		 ^ ^W]]U\]]]CMJr*         ?Optional[str]r   
np.ndarraypresetsDict[str, dict]sigmafloatparametric_overlay_channelsOptional[List[int]]parametric_overlay_intensityc                B   t                    | | dk    rq|                    d          }|:t          j        t          j        |d         t          j                  dd          S t          j        dt          j                  S t                                          |           }| t          j        dt          j                  S g }g }	g }
d	D ]}|  d
| }||vs||vr|                    |           |	                    ||                    |
                    t          j        ||         d         t          j                             |
s t          j        dt          j                  S t          j	        fd|	D             t          j                  }t          j
        |dz   d|dz  z  z            }||                                z  }t          j        |
d          }|dddf         |z                      d          }|rVt                    }t          j        |t          j                  }t          j        ||         |||         z            ||<   t          j        |dd                              t          j                  S )u  Return 52-dim blendshape by blending authored L1/L3/L5 presets of emotion,
    with optional channel-masked parametric overlay (Option E).

    Args:
        emotion: emotion family name (e.g. 'crying'). None → neutral.
        vad: (3,) target VAD.
        presets: dict with keys like 'crying_L1', 'crying_L3', 'crying_L5'.
        sigma: RBF bandwidth for level blending.
        parametric_overlay_channels: list of channel indices on which to add
            the parametric layer's output via max() merge. Defaults to
            mouth/cheek "valence-coloring" channels. Pass None or [] to disable.
        parametric_overlay_intensity: scalar α applied to the parametric
            output before max-merge (1.0 = pure max, lower = milder coloring).

    Option E rationale: emotion + within-emotion RBF defines the structural
    pose (eyes, brows, jaw). Valence then continuously colors the mouth/cheek
    so happy-surprise (V+) and sad-surprise (V−) become visually distinct
    without re-authoring presets per V quadrant.
    Nneutral
neutral_L3bsr    r   r   4   )r         _Lc                T    g | ]$}t           j                            |z
            %S  )r#   linalgnorm)r&   avr   s     r(   
<listcomp>z&compile_expressive.<locals>.<listcomp>p   s+    EEE2binnS2X..EEEr*   r   g       @)axisg        r5   )r   getr#   clipr$   r%   zerosr4   appendarrayexpsumstackr
   int64maximumastype)r   r   r8   r:   r<   r>   r@   r   levelsanchor_vads
bs_vectorsLkeydistsweightsbs_stackr1   parammasks    `                 r(   compile_expressiverc   9   s   6 s

C'Y..++l++72:gdm2:FFF1MMMx"*----oo!!'**Gx"*---- F$&K#%J L LAg'!1!1a71:&&&"*WS\$%7rzJJJKKKK .x"*---- HEEEEEEERZXXXEfuz]cEQJ&6788Gw{{}}G x
+++H111d7h&
+
+
+
3
3C # V %%z5RXFFFJs4y*Ft*TUUD	73S!!((444r*   emotionsList[Optional[str]]vadsc           	         |j         d         }t          j        |dft          j                  }t	          |          D ]&}t          | |         ||         ||||          ||<   '|S )z-Batch version. vads: (N, 3). Returns (N, 52).r   rC   r    )r:   r<   r>   )shaper#   emptyr%   rangerc   )	rd   rf   r8   r:   r<   r>   Nr1   is	            r(   compile_expressive_batchrm      sw     	
1A
(Ar7"*
-
-
-C1XX 
 
#QKa'(C)E	
 
 
A Jr*   )r   r   )r   r6   r   r7   r8   r9   r:   r;   r<   r=   r>   r;   r   r7   )rd   re   rf   r7   r8   r9   r:   r;   r<   r=   r>   r;   r   r7   )__doc__
__future__r   r+   pathlibr   typingr   r   r   numpyr#   	constantsr	   
parametricr
   utilsr   __file__resolveparentsr-   EXPRESSIVE_SIGMAMOUTH_CHEEK_OVERLAY_CHANNELSr   __annotations__r4   rc   rm   rH   r*   r(   <module>r|      sn  	 	 	 # " " " " "        ' ' ' ' ' ' ' ' ' '     # # # # # # ( ( ( ( ( (      tH~~%%''/2V;iGJdd         =A @ @ @ @	 	 	 	  $7S*-H5 H5 H5 H5 H5^ $7S*-      r*   