
    oh9                        d Z ddlZddlZddlZddlZddlZddlZdZdZdZ	dZ
dde
e	d	d
eeddg
ZdZdZdZdZddgZg dZdZdZdZdZdZdZdZdZdZdZdZdZdZeeeeegZd Z d!Z!d"d#gZ"d$d%gZ#ej$        %                    ej$        &                    ej$        '                    ej$        (                     ej)         ej*                                                                      Z+ej$        (                    ej$        (                    e+                    Z,ej$        -                    e,d&d'          Z.ej$        -                    e.d(          Z/d) Z0d* Z1d+ Z2d, Z3d- Z4d. Z5e/e!fd/Z6dS )0z
IMPORTANT: This version of binary_conversion.py is now specific to Victor,
so it should NOT be used for the original Cozmo because, for example, this
version strips out the Left and Right backpack light data.
    NBodyMotionKeyFrameRobotAudioKeyFrameBackpackLightsKeyFrameProceduralFaceKeyFrameLiftHeightKeyFrameHeadAngleKeyFrameFaceAnimationKeyFrameEventKeyFrameRecordHeadingKeyFrameTurnToRecordedHeadingKeyFrame	radius_mmspeed	height_mm	angle_degrightEyeleftEye)r   r   gʡE?gRQ?r   333333?r   r   r   r   r   r   r   r   r   r   r   r   r      r   r   r   r   r   eventGroupseventIdsvolumesprobabilities	audioNameaudioEventIdvolumeprobabilitydurationTime_mstriggerTime_msName	keyframesclipsz.binz$typepathFromRootLeftRight	resourcesconfigzcozmo_anim.fbsc                 X   t          | d          }	 t          j        |          }n(# t          $ r}t	          d| d|            d}~ww xY w	 |                                 n# |                                 w xY wt          |                                          d         \  }}||fS )z
    Given the path to a .json animation file, this function
    will read the contents of that file and return a 2-item
    tuple of (animation name, list of all keyframes)
    rzFailed to read z file because: Nr   )openjsonload	Exceptionprintcloselistitems)	anim_filefhcontentse	anim_clipr    s         I/home/metal/girl-os/anki/victor/project/buildScripts/binary_conversion.pyread_anim_filer7   h   s     
i		B9R==   yyy!!DFFF 	
 	







 0 011!4Iyy!!s%   ' A% 
AAAA% %A;c           	         i }| |t           <   i |t          <   t          D ]}g |t                   |<   |D ]}|t                   }||t                   vrg |t                   |<   |                    t                     	 |t
                   }n2# t          $ r%}d|d| dt
          d}t          |          d}~ww xY wt          D ](}	 |                    |           # t          $ r Y %w xY w|t          k    r0t          D ](}	 |                    |           # t          $ r Y %w xY w|t          k    rt          ||           }|t          k    r;	 |                    t                     n# t          $ r Y nw xY wt          |           |t          k    rBt!          |t"                   t$                    s"t%          |t"                             |t"          <   t&          D ]<}		 ||	         }
t)          t+          |
                    }|||	<   -# t          $ r Y 9w xY w|t                   |                             |           |S )a  
    Given the name of the animation and a list of all keyframes for that
    animation, this function will separate those keyframes by each track
    to build and return a dictionary that looks like:

        {'Name'     : 'anim_blah',
         'keyframes': {'BackpackLightsKeyFrame': [<list of dictionaries>],
                       'BodyMotionKeyFrame': [<list of dictionaries>],
                       'EventKeyFrame': [<list of dictionaries>],
                       'FaceAnimationKeyFrame': [<list of dictionaries>],
                       'HeadAngleKeyFrame': [<list of dictionaries>],
                       'LiftHeightKeyFrame': [<list of dictionaries>],
                       'ProceduralFaceKeyFrame': [<list of dictionaries>],
                       'RobotAudioKeyFrame': [<list of dictionaries>],
                       'RecordHeadingKeyFrame': [<list of dictionaries>],
                       'TurnToRecordedHeadingKeyFrame': [<list of dictionaries>],
                      }
        }
    zAt least one 'z' in 'z' is missing ''N)ANIM_NAME_ATTRKEYFRAMES_ATTR
ALL_TRACKSKEYFRAME_TYPE_ATTRpopTRIGGER_TIME_ATTRKeyErrorOLD_ANIM_TOOL_ATTRSBACKPACK_LIGHT_TRACKOLD_BACKPACK_LIGHT_ATTRSROBOT_AUDIO_TRACKprep_audio_key_frame_jsonPROCEDURAL_FACE_TRACKDURATION_TIME_ATTRfill_out_eye_parametersBODY_MOTION_TRACK
isinstanceBODY_RADIUS_ATTRstrINTEGER_ATTRSintroundappend)	anim_namer    	anim_dicttrackkeyframetrigger_timer4   	error_msgold_attrint_attrorig_valint_vals               r6   prep_json_for_binary_conversionr[   {   s   ( I )In "In . . ,.	.!%(( =: =:+,	.111/1In%e,'(((	&#$56LL 	& 	& 	& 	&GLuuiiiYjYjYjkI9%%%	&
 , 	 	HX&&&&    ((( 5  LL****   D %%%09EEH)))/0000   #H---
 %%%h'78#>> M-0:J1K-L-L)* & 	- 	-H-#H- eHoo..%,""	     	.!%(//9999s`   =B
B: B55B:C
C*)C*D
D%$D%E**
E76E7 H


HHc                     t           D ]W}t          | |                   }|t          t                    k     r(| |                             t          |d                    XdS )a<  
    If the provided keyframe doesn't have enough attributes for each
    eye (because it is an old animation from a time when we had fewer
    attributes per eye), then use DEFAULT_EYE_SETTINGS to fill in
    those missing attributes for each eye (using the default values
    defined in DEFAULT_EYE_SETTINGS).
    N)	EYE_ATTRSlenDEFAULT_EYE_SETTINGSextend)rT   eye_attrnum_eye_settingss      r6   rH   rH      so      O Ox122c"67777X%%&:;K;L;L&MNNNO O    c                 Z   t           | vr4t          | v r)| t                   D ]}	 |t          = # t          $ r Y w xY w| S i }| t                   |t          <   i }t          | t                    t                    r| t                    |t          <   n| t                    g|t          <   d}t          |v rt          |t                             }|dk    r|g|t          <   |S d}t          | v r_t          | t                   t                    r'| t                   }t          |          }||t          <   n| t                   g|t          <   d}||k    r/|dk    rd|z  }t          |           d|z  }|g|z  }	|	|t          <   d}
t          | v r| t                   }
|
g|z  |t          <   |g|t          <   |S )a  
    1. Check audio key frame json format
    2. Migrate old version to new format
    3. Handle exporter edge cases
    Current Audio Key Frame format
    {
        "triggerTime_ms": uint
        "eventGroups": [
          {
            "eventIds": [uint],
            "volumes": [float],
            "probabilities": [float]
          }
        ],
        "states": [
          {
            stateGroupId: uint,
            stateId: uint
          }
        ]
        "switches": [
          {
            switchGroupId: uint,
            stateId: uint
          }
        ]
        "parameters": [
          {
            parameterId: uint,
            value: float,
            time_ms: uint,
            curve: byte
          }
        ]
    }
    r   r   zKFailed to migrate '%s' because the event and probability count do not matchg      ?)AUDIO_DEP_EVENT_ATTRAUDIO_EVENT_GROUPS_ATTRAUDIO_NAME_ATTRr@   r?   rJ   r/   AUDIO_EVENT_IDS_ATTRr^   AUDIO_DEP_PROBABILITY_ATTRAUDIO_PROBABILITIES_ATTRr-   AUDIO_DEP_VOLUME_ATTRAUDIO_VOLUMES_ATTR)rT   rQ   
eventGroup
audioFrame
eventCount	probCountdep_probabilitymsgvalr   
defaultVols              r6   rE   rE      s   N  8++ #h..&'>?  
"?33   D  J$,->$?J !J (/0$77 L+34H+I
'((,45I,J+K
'(Jz))$89::
 Q/9l
*+ I!X--h9:DAA 	&'ABOO,,I3BJ/00 5==W4X3YJ/0IJ>>_clmC#JJJJ
*/<
+, J((34
&0\J%>J!" ,6,J&'s   ,
99c                     	 t          | d          5 }t          j        ||dd           ddd           dS # 1 swxY w Y   dS # t          t          f$ r"}d| d|}t          |           Y d}~dS d}~ww xY w)z
    Given the path to a .json file and a dictionary of animation
    data, this function will write out the animation file
    (overwriting any existing file at that path).
    w   ),z: )indent
separatorsNzFailed to write 'z' file because: )r)   r*   dumpOSErrorIOErrorr-   )	json_filedatar2   r4   rV   s        r6   write_json_filer   Y  s    )S!! 	BRIdBq[AAAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	BW    ?Hyy!!L	is3   A 8A <A < A A8A33A8c                    t           j                            |           }t           j                            |d          }t           j                            |          st          d|z            |d|d|| g}t          j        |t          j        t          j                  }|	                                \  }}	|j
        }
|
dk    r$t          d|	z             t          d|
z            t           j                            |           d         |z   }t           j                            |          st          d	|z            |S )
Nflatcz;%s is not a file so JSON data cannot be converted to binaryz-oz-b)stdoutstderrr   zError encountered: %sz>Unable to successfully generate binary file (exit status = %s)z"Unable to generate binary file: %s)ospathdirnamejoinisfileEnvironmentError
subprocessPopenPIPEcommunicate
returncoder-   
ValueErrorsplitext)	file_path	flatc_dirschema_filebin_file_ext
output_dirr   argspr   r   exit_statusoutput_files               r6   convert_json_to_binaryr   g  s   ++JGLLG,,E7>>%   f\_ddeee4T;	BDjojoNNNA]]__NFF,Ka&/111Y\gghhh'""9--a0<?K7>>+&& M=KLLLrc   c                 Z   g }| D ]9}t          |          \  }}t          ||          }	|                    |	           :t          j        d          \  }
}t          |t          |i           t          ||||          }t          j	        |
           t          j
        |           t          j                            t          j                            |          |          }t          j        ||           t          j                            |          st!          d|z            |S )a{  
    Given:
        1: a list of .json animation files
        2: the desired name for the resulting binary file
        3: the path to a directory that contains the "flatc" binary
        4: the path to an .fbs FlatBuffers schema file (optional, default = "cozmo_anim.fbs")
        5: the desired file extension for the resulting binary file (optional, default = ".bin")
    this function will use "flatc" to generate a binary animation
    file and return the path to that file.

    See https://google.github.io/flatbuffers/flatbuffers_guide_using_schema_compiler.html
    for additional info about the "flatc" schema compiler.
    z.json)suffixzBinary file missing: %s)r7   r[   rP   tempfilemkstempr   
CLIPS_ATTRr   r   r.   remover   r   r   renamer   r   )
json_filesbin_namer   r   r   
anim_clipsr~   r5   r    rR   fdtmp_json_filebin_filerenamed_bin_files                 r6   mainr   }  s    J % %	-i88	93IyII	)$$$$ (888BMJz#:;;;%mY\ZZHHRLLLImw||BGOOH$=$=xHHIh()))7>>*++ G25EEFFFrc   )7__doc__sysr   r   r   r*   inspectrI   rD   rB   rF   r<   rK   BODY_SPEED_ATTRLIFT_HEIGHT_ATTRHEAD_ANGLE_ATTRr]   r_   rf   rh   rl   rj   rg   re   rk   ri   rG   r?   r=   r:   r;   rM   r   BIN_FILE_EXTrA   rC   r   normpathabspathrealpathr   getfilecurrentframeTHIS_DIRENGINE_ROOTr   
CONFIG_DIRSCHEMA_FILEr7   r[   rH   rE   r   r   r    rc   r6   <module>r      sr    


 				        ) ( / 0 "$79N"$;_!24KMln
   #	   < ( !  * %   *  ' $  "$6Zjk
/ "G, 7BGOOBG,<,<RW___W_]q]d]q]s]sMtMt=u=u,v,vwwxxgoobgooh7788W\\+{H==
gll:'788" " "&\ \ \~O O On n nb    , 7BP\      rc   