U
    8jg
                  	   @  s   d Z ddlmZ ddlmZmZ ddlZddlm	Z	 ddl
mZmZmZmZmZmZ ddlmZmZmZ dd	lmZ d
ddddZeddfdd
ddddd
dddZdd
dd
dddZdS )z7Main compiler API: blend parametric + archetype layers.    )annotations)ListOptionalN   )archetype_blend)BLEND_NEUTRAL_THRESHOLDBLEND_NEUTRAL_WEIGHTBLEND_EXTREME_THRESHOLDBLEND_EXTREME_WEIGHTBLEND_DEFAULT_WEIGHT	RBF_SIGMA)parametric_layerapply_conflict_resolutionapply_channel_mask)validate_vadz
np.ndarrayfloat)vadreturnc                 C  s,   t tj| }|tk rtS |tkr(tS tS )zLPer-query blend weight: neutral favors parametric, extreme favors archetype.)	r   npZlinalgZnormr   r   r	   r
   r   )r   Z	magnitude r   Z/home/ubuntu/service/kemix-engine/package/face/animasync-face-v3/scripts/compiler/blend.py_dynamic_blend_weight   s    r   TzOptional[str]dictboolzOptional[float])emotionr   presetssigmaapply_lipsync_maskoverride_blend_weightr   c           
      C  st   t |}t|}t||| |d}|dk	r,|nt|}|| |d|   }	t|	|}	|r^t|	}	t|	ddtj	S )u  Compute 52 blendshape values for given (emotion, VAD).

    Args:
        emotion: emotion family name (e.g. 'joy') for archetype boosting; None → pure VAD
        vad: (3,) in [-1, 1]
        presets: dict of archetype presets
        sigma: RBF bandwidth (archetype layer)
        apply_lipsync_mask: zero out LIPSYNC_ONLY channels
        override_blend_weight: if set, use this instead of dynamic

    Returns:
        (52,) float32 in [0, 1]
    )Zemotion_hintr   Ng      ?g        )
r   r   r   r   r   r   r   ZclipZastypefloat32)
r   r   r   r   r   r   paramZarchwoutr   r   r   compile_blendshapes#   s    
r#   zList[Optional[str]])emotionsvadsr   r   c                 K  sp   |j d }t| |kr.tdt|  d| tj|dftjd}t|D ] }t| | || |f|||< qJ|S )z-Batch version. vads: (N, 3). Returns (N, 52).r   zemotions length z	 != vads 4   )Zdtype)shapelen
ValueErrorr   emptyr   ranger#   )r$   r%   r   kwargsNr"   ir   r   r   compile_batchN   s    
r/   )__doc__Z
__future__r   typingr   r   Znumpyr   Z	archetyper   	constantsr   r   r	   r
   r   r   Z
parametricr   r   r   utilsr   r   r#   r/   r   r   r   r   <module>   s    +