§
    [ö6i˜9  ã                   óØ  — 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ðÝ”9˜R‘=”=ˆˆøÝð ð ð Ýˆ°y°y°yÀ!À!ÐDÑFÔFÐFØøøøøðøøøð 	ð
 	�Š‰
Œ
ˆ
ˆ
øˆ�Š‰
Œ
ˆ
ˆ
øøøÝ §¢Ñ 0Ô 0Ñ1Ô1°!Ô4Ñ€Iˆyà�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Ø )€I�nÑØ "€I�nÑåð .ð .ˆð ,.ˆ	•.Ô! %Ñ(Ð(àð =:ñ =:ˆØÕ+Ô,ˆØ˜	¥.Ô1Ð1Ð1Ø/1ˆI•nÔ% eÑ,Ø�ŠÕ'Ñ(Ô(Ð(ð	&Ø#Õ$5Ô6ˆLˆLøÝð 	&ð 	&ð 	&ð 	&ØGLÀuÀuÈiÈiÈiÕYjÐYjÐYjÐkˆIÝ˜9Ñ%Ô%Ð%øøøøð	&øøøõ
 ,ð 	ð 	ˆHðØ—’˜XÑ&Ô&Ð&Ð&øÝð ð ð Ø�ðøøøð Õ(Ò(Ð(õ 5ð ð �ðØ—L’L Ñ*Ô*Ð*Ð*øÝð ð ð Ø�Dðøøøð Õ%Ò%Ð%å0°¸9ÑEÔEˆHàÕ)Ò)Ð)ðØ—’Õ/Ñ0Ô0Ð0Ð0øÝð ð ð Ø�ðøøøå# HÑ-Ô-Ð-ð
 Õ%Ò%Ð%Ý˜hÕ'7Ô8½#Ñ>Ô>ð MÝ-0°Õ:JÔ1KÑ-LÔ-L�Õ)Ñ*õ &ð 	-ð 	-ˆHð-Ø# HÔ-�õ �e H™oœoÑ.Ô.�Ø%,�˜Ñ"Ð"øõ	 ð ð ð Ø�ðøøøð 	•.Ô! %Ô(×/Ò/°Ñ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Ø+3Õ4HÔ+Iˆ
Õ'Ñ(Ð(à,4Õ5IÔ,JÐ+Kˆ
Õ'Ñ(à€JÝ˜zÐ)Ð)Ý˜Õ$8Ô9Ñ:Ô:ˆ
ð �Q‚€Ø/9¨lˆ
Õ*Ñ+ØÐð €IÝ! XÐ-Ð-å�hÕ9Ô:½DÑAÔAð 	à&Õ'AÔBˆOÝ˜OÑ,Ô,ˆIØ3BˆJÕ/Ñ0Ð0ð 5=Õ=WÔ4XÐ3YˆJÕ/Ñ0ØˆIà�JÒÐØ˜Š>ˆ>à_ÐclÑmˆCÝ�#‰JŒJˆJà�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ÝŒI�d˜B q°[ÐAÑAÔAÐAð	Bð 	Bð 	Bñ 	Bô 	Bð 	Bð 	Bð 	Bð 	Bð 	Bð 	Bð 	Bøøøð 	Bð 	Bð 	Bð 	Bð 	Bð 	Bøå•WÐð ð ð ð Ø?H¸y¸yÈ!È!Ð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ÝŒG�LŠL˜ GÑ,Ô,€EÝŒ7�>Š>˜%Ñ Ô ð fÝÐ\Ð_dÑdÑeÔeÐeØ�4˜ T¨;¸	ÐB€DåÔ˜¥j¤o½j¼oÐNÑNÔN€AØ—]’]‘_”_�N€FˆFØ”,€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Ý3°I¸yÑIÔIˆ	Ø×Ò˜)Ñ$Ô$Ð$Ð$Ý Ô(°Ð8Ñ8Ô8Ñ€BˆÝ�M¥J¨zÐ#:Ñ;Ô;Ð;Ý% m°YÀÈ\ÑZÔZ€HÝ„HˆR�L„L€LÝ„IˆmÑÔÐÝ”w—|’|¥B¤G§O¢O°HÑ$=Ô$=¸xÑHÔHÐÝ„IˆhÐ(Ñ)Ô)Ð)ÝŒ7�>Š>Ð*Ñ+Ô+ð GÝÐ2Ð5EÑ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Ð à"Ð$7Ð9NØ"Ð$;¸_ØÐ!2Ð4KÐMlðn€
ð Ð à€àÐ à€à˜Ð#€	ðð ð Ð ð< (Ð Ø!Ð ØÐ Ø*Ð Ø€à%Ð Ø Ð Ø*Ð ð 'Ð à$Ð àÐ à€à€à"Ð$6¸ÈÐZjÐk€à€
à€à Ð/Ð à" GÐ,Ð àŒ7×Ò˜BœGŸOšO¨B¬G×,<Ò,<¸R¼W¿_º_È_ÈWÌ_Ð]qÐ]dÔ]qÑ]sÔ]sÑMtÔMtÑ=uÔ=uÑ,vÔ,vÑwÔwÑxÔx€àŒg�oŠo˜bœgŸošo¨hÑ7Ô7Ñ8Ô8€àŒW�\Š\˜+ {°HÑ=Ô=€
àŒg�lŠl˜:Ð'7Ñ8Ô8€ð"ð "ð "ð&\ð \ð \ð~Oð Oð Oðnð nð nðbð ð ðð ð ð, 7BÐP\ð ð ð ð ð ð rc   