o
    [iq                     @  s   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 d d ZdZg dZdaded< d(ddZeedfd)d!d"Zeedfd*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                  C  sR   t d urt S ttjddd } i }|  D ]\}}dd |D ||< q|a |S )Nzutf-8)encodinganchorsc                 S  s*   i | ]}t |d  tj|d tjdqS )levelvaddtype)intnpasarrayfloat32).0e r%   S/dataset/kemix-engine/package/face/animasync-face-v3/scripts/compiler/expressive.py
<dictcomp>4   s   * z!_load_anchors.<locals>.<dictcomp>)r   jsonloads_ANCHOR_PATH	read_textitems)rawoutemoentriesr%   r%   r&   _load_anchors-   s   r1         ?Optional[str]r   
np.ndarraypresetsDict[str, dict]sigmafloatparametric_overlay_channelsOptional[List[int]]parametric_overlay_intensityc                   s  t   | du s| dkr,|d}|dur$ttj|d tjdddS tjdtjdS t | }|du r>tjdtjdS g }g }	g }
d	D ]-}|  d
| }||vsW||vrXqF|| |	||  |
tj|| d tjd qF|
s~tjdtjdS tj	 fdd|	D tjd}t
|d  d|d   }||  }tj|
dd}|dddf | jdd}|rt }tj|tjd}t|| |||  ||< t|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                   s   g | ]
}t j | qS r%   )r    linalgnorm)r#   avr   r%   r&   
<listcomp>p   s    z&compile_expressive.<locals>.<listcomp>r   g       @)axisg        r2   )r
   getr    clipr!   r"   zerosr1   appendarrayexpsumstackr	   int64maximumastype)r   r   r5   r7   r9   r;   r<   r   levelsanchor_vads
bs_vectorsLkeydistsweightsbs_stackr.   parammaskr%   rF   r&   compile_expressive9   s>   

 r^   emotionsList[Optional[str]]vadsc           	   	   C  sP   |j d }tj|dftjd}t|D ]}t| | || ||||d||< q|S )z-Batch version. vads: (N, 3). Returns (N, 52).r   r?   r   )r7   r9   r;   )shaper    emptyr"   ranger^   )	r_   ra   r5   r7   r9   r;   Nr.   ir%   r%   r&   compile_expressive_batch   s   
	rg   )r   r   )r   r3   r   r4   r5   r6   r7   r8   r9   r:   r;   r8   r   r4   )r_   r`   ra   r4   r5   r6   r7   r8   r9   r:   r;   r8   r   r4   )__doc__
__future__r   r(   pathlibr   typingr   r   r   numpyr    	constantsr   
parametricr	   utilsr
   __file__resolveparentsr*   EXPRESSIVE_SIGMAMOUTH_CHEEK_OVERLAY_CHANNELSr   __annotations__r1   r^   rg   r%   r%   r%   r&   <module>   s,    

O