summaryrefslogtreecommitdiffstats
path: root/Scripts/libunity.py
blob: f79cd6f1596d4494d73f96648e9b820a0b2bf657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
#!/usr/bin/env python3

import argparse
import copy
import enum
import math
import os
import pickle
import random
import sys
# python3 -m pip install pyyaml
# License: MIT.
import yaml

import multiprocessing as mp

WRITE_DEFAULTS_ANIM_TEMPLATE = """
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!74 &7400000
AnimationClip:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_Name: TaSTT_Reset_Animations
  serializedVersion: 6
  m_Legacy: 0
  m_Compressed: 0
  m_UseHighQualityCurve: 1
  m_RotationCurves: []
  m_CompressedRotationCurves: []
  m_EulerCurves: []
  m_PositionCurves: []
  m_ScaleCurves: []
  m_FloatCurves:
  - curve:
      serializedVersion: 2
      m_Curve:
      - serializedVersion: 3
        time: 0
        value: 0
        inSlope: 0
        outSlope: 0
        tangentMode: 136
        weightedMode: 0
        inWeight: 0
        outWeight: 0
      m_PreInfinity: 2
      m_PostInfinity: 2
      m_RotationOrder: 4
    attribute: REPLACEME_ATTRIBUTE
    path: REPLACEME_PATH
    classID: 137
    script: {fileID: 0}
  m_PPtrCurves: []
  m_SampleRate: 60
  m_WrapMode: 0
  m_Bounds:
    m_Center: {x: 0, y: 0, z: 0}
    m_Extent: {x: 0, y: 0, z: 0}
  m_ClipBindingConstant:
    genericBindings:
    - serializedVersion: 2
      path: 2794480623
      attribute: 2284639795
      script: {fileID: 0}
      typeID: 137
      customType: 22
      isPPtrCurve: 0
    pptrCurveMapping: []
  m_AnimationClipSettings:
    serializedVersion: 2
    m_AdditiveReferencePoseClip: {fileID: 0}
    m_AdditiveReferencePoseTime: 0
    m_StartTime: 0
    m_StopTime: 0
    m_OrientationOffsetY: 0
    m_Level: 0
    m_CycleOffset: 0
    m_HasAdditiveReferencePose: 0
    m_LoopTime: 1
    m_LoopBlend: 0
    m_LoopBlendOrientation: 0
    m_LoopBlendPositionY: 0
    m_LoopBlendPositionXZ: 0
    m_KeepOriginalOrientation: 0
    m_KeepOriginalPositionY: 1
    m_KeepOriginalPositionXZ: 0
    m_HeightFromFeet: 0
    m_Mirror: 0
  m_EditorCurves:
  - curve:
      serializedVersion: 2
      m_Curve:
      - serializedVersion: 3
        time: 0
        value: 0
        inSlope: 0
        outSlope: 0
        tangentMode: 136
        weightedMode: 0
        inWeight: 0
        outWeight: 0
      m_PreInfinity: 2
      m_PostInfinity: 2
      m_RotationOrder: 4
    attribute: REPLACEME_ATTRIBUTE
    path: REPLACEME_PATH
    classID: 137
    script: {fileID: 0}
  m_EulerEditorCurves: []
  m_HasGenericRootTransform: 0
  m_HasMotionFloatCurves: 0
  m_Events: []
"""[1:][:-1]

METADATA_TEMPLATE = """
fileFormatVersion: 2
guid: REPLACEME_GUID
NativeFormatImporter:
  externalObjects: {}
  mainObjectFileID: 7400000
  userData:
  assetBundleName:
  assetBundleVariant: 
"""[1:][:-1]

ANIMATION_STATE_TEMPLATE = """
--- !u!1102 &110200000
AnimatorState:
  serializedVersion: 6
  m_ObjectHideFlags: 1
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_Name: REPLACEME_ANIMATION_NAME
  m_Speed: 1
  m_CycleOffset: 0
  m_Transitions: []
  m_StateMachineBehaviours: []
  m_Position: {x: 50, y: 50, z: 0}
  m_IKOnFeet: 0
  m_WriteDefaultValues: 0
  m_Mirror: 0
  m_SpeedParameterActive: 0
  m_MirrorParameterActive: 0
  m_CycleOffsetParameterActive: 0
  m_TimeParameterActive: 0
  m_Motion: {}
  m_Tag: 
  m_SpeedParameter: 
  m_MirrorParameter: 
  m_CycleOffsetParameter: 
  m_TimeParameter: 
"""[1:][:-1]

TRANSITION_TEMPLATE = """
--- !u!1101 &110100000
AnimatorStateTransition:
  m_ObjectHideFlags: 1
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_Name: 
  m_Conditions: []
  m_DstStateMachine: {fileID: 0}
  m_DstState: {fileID: 0}
  m_Solo: 0
  m_Mute: 0
  m_IsExit: 0
  serializedVersion: 3
  m_TransitionDuration: 0
  m_TransitionOffset: 0
  m_ExitTime: 0.0
  m_HasExitTime: 0
  m_HasFixedDuration: 1
  m_InterruptionSource: 2
  m_OrderedInterruption: 1
  m_CanTransitionToSelf: 1
"""[1:][:-1]

BLEND_TREE_TEMPLATE = """
--- !u!206 &1071664566462684110
BlendTree:
  m_ObjectHideFlags: 1
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_Name: REPLACEME_BLEND_TREE_NAME
  m_Childs:
  - serializedVersion: 2
    m_Motion: {fileID: 7400000, guid: REPLACEME_GUID_LO, type: 2}
    m_Threshold: -1
    m_Position: {x: 0, y: 0}
    m_TimeScale: 1
    m_CycleOffset: 0
    m_DirectBlendParameter: REPLACEME_BLEND_PARAMETER
    m_Mirror: 0
  - serializedVersion: 2
    m_Motion: {fileID: 7400000, guid: REPLACEME_GUID_HI, type: 2}
    m_Threshold: 1
    m_Position: {x: 0, y: 0}
    m_TimeScale: 1
    m_CycleOffset: 0
    m_DirectBlendParameter: REPLACEME_BLEND_PARAMETER
    m_Mirror: 0
  m_BlendParameter: REPLACEME_BLEND_PARAMETER
  m_BlendParameterY: REPLACEME_BLEND_PARAMETER
  m_MinThreshold: -1
  m_MaxThreshold: 1
  m_UseAutomaticThresholds: 0
  m_NormalizedBlendValues: 0
  m_BlendType: 0
"""[1:][:-1]

class Metadata:
    def __init__(self):
        self.guid = "%032x" % random.randrange(16 ** 32)

    def load(self, path):
        if not path.endswith(".meta"):
            path = path + ".meta"

        self.guid = None
        with open(path, "r", encoding="utf-8") as f:
            for line in f:
                if line.startswith("guid"):
                    self.guid = line.split()[1]

    def loadOrCreate(self, path, guid_map):
        if not path.endswith(".meta"):
            path = path + ".meta"

        if os.path.exists(path):
            self.load(path)
            return

        self.persist(path, guid_map)

    def persist(self, path, guid_map):
        with open(path, "w", encoding="utf-8") as f:
            f.write(str(self))

        guid_map[self.guid] = path
        guid_map[path] = self.guid

    def __str__(self):
        return METADATA_TEMPLATE.replace("REPLACEME_GUID", self.guid)

class Node:
    def __init__(self):
        # Optional. In Unity, this is the fileID of an object. Not all YAML
        # mappings have an anchor.
        self.anchor = None

        # Pointer to the Node containing this one.
        self.parent = None

class Sequence(Node):
    def __init__(self):
        super().__init__()
        self.sequence = []

    def copy(self):
        new = Sequence()
        new.anchor = self.anchor
        new.parent = self.parent

        for v in self.sequence:
            if hasattr(v, "copy"):
                new.sequence.append(v.copy())
                new.sequence[-1].parent = new
            else:
                new.sequence.append(v)

        return new

    def prettyPrint(self, first_indent=None, leading_newline=None):
        depth = 0
        p = self.parent
        while p != None:
            depth += 1
            p = p.parent
        indent = "  " * depth

        lines = []
        first = True
        for item in self.sequence:
            cur_indent = indent
            if first:
                if first_indent != None:
                    cur_indent = first_indent
                first = False
            if hasattr(item, "prettyPrint"):
                lines.append("{}- {}".format(cur_indent, item.prettyPrint(first_indent="", leading_newline=False)))
            else:
                lines.append("{}- {}".format(cur_indent, item))

        if len(lines) == 0:
            return "[]"

        return "\n" + '\n'.join(lines)

    def __str__(self):
        return self.prettyPrint()

    def addChildMapping(self, anchor = None, add_to_head = False):
        child = Mapping()
        child.anchor = anchor
        child.parent = self
        child.sequence = []

        if add_to_head:
            self.sequence = [child] + self.sequence
        else:
            self.sequence.append(child)

        return child

    def addChildSequence(self, anchor = None):
        child = Sequence()
        child.anchor = anchor
        child.parent = self
        child.sequence = []

        self.sequence.append(child)

        return child

    def forEach(self, cb):
        for k in self.sequence:
            cb(k)

class Mapping(Node):
    def __init__(self):
        super().__init__()
        self.mapping = {}

    def copy(self):
        new = Mapping()
        new.anchor = self.anchor
        new.parent = self.parent

        for k, v in self.mapping.items():
            if hasattr(v, "copy"):
                new.mapping[k] = v.copy()
                new.mapping[k].parent = new
            else:
                new.mapping[k] = v

        return new

    def prettyPrint(self, first_indent=None, leading_newline=True):
        depth = 0
        p = self.parent
        while p != None:
            depth += 1
            p = p.parent
        indent = "  " * depth

        lines = []
        first = True
        for k, v in self.mapping.items():
            cur_indent = indent
            if first:
                if first_indent != None:
                    cur_indent = first_indent
                first = False
            lines.append("{}{}: {}".format(cur_indent, k, v))

        result = '\n'.join(lines)

        # Inline 1-item mappings, matching Unity behavior.
        if len(self.mapping.keys()) == 1 and len(result.split("\n")) == 1:
            if first_indent == None:
                return self.prettyPrint(first_indent="")
            return "{" + lines[0] + "}"

        # Empty mappings are represented by '{}'. If we don't do this, Unity
        # will assume that they are Sequences and get very sad.
        if len(self.mapping.keys()) == 0:
            return "{}"

        if leading_newline:
            result = "\n" + result

        return result

    def __str__(self):
        return self.prettyPrint()

    def addChildMapping(self, key, anchor = None):
        child = Mapping()
        child.anchor = anchor
        child.parent = self
        child.mapping = {}

        self.mapping[key] = child

        return child

    def addChildSequence(self, key, anchor = None):
        child = Sequence()
        child.anchor = anchor
        child.parent = self
        child.mapping = {}

        self.mapping[key] = child

        return child

    def forEach(self, cb):
        for k, v in self.mapping.items():
            cb(v)

class UnityDocument(Mapping):
    def __init__(self):
        super().__init__()
        self.class_id = None

    def __str__(self):
        return super().__str__()

    def copy(self):
        result = super().copy()
        result.class_id = self.class_id
        return result

# Class representing a Unity AnimatorController. Implements manipulations, like
# merging and reanchoring.
class UnityAnimator():
    def __init__(self):
        self.nodes = []
        self.id_to_node = {}
        self.next_id = 1000 * 1000

    def __str__(self):
        return unityYamlToString(self.nodes)

    def addNodes(self, nodes):
        for node in nodes:
            self.nodes.append(node)
            anchor = node.anchor
            if anchor == None:
                raise Exception("Node is missing anchor: {}".format(str(node)))
            if anchor in self.id_to_node:
                raise Exception("Duplicate anchor: {}, node 1: {}, node 2: {}".format(anchor, str(node), str(self.id_to_node[anchor])))
            self.id_to_node[anchor] = node

            if int(anchor) > self.next_id:
                self.next_id = int(anchor) + 1
        # I don't know why but this fixes a bug in the `fixWriteDefaults`
        # codepath: two documents wind up with the same anchor.
        self.next_id += 1

    def allocateId(self) -> int:
        result = self.next_id
        self.next_id += 1
        return result

    # Checks if `old_id` is in `self.id_mapping`, and if so, returns the
    # already-generated ID. Otherwise this allocates a new ID and
    # records it in `self.id_mapping`.
    def mapId(self, old_id: str) -> int:
        new_id = None
        if old_id in self.id_mapping.keys():
             new_id = self.id_mapping[old_id]
        else:
            new_id = self.allocateId()
            self.id_mapping[old_id] = new_id
        return new_id

    # Recursively iterate every mapping under `node` and assign new IDs to
    # every identifier. Mappings are recorded in `self.id_mapping`.
    def mergeIterator(self, node):
        if hasattr(node, "mapping"):
            # Don't relabel anything that's defined in an external file.
            # TODO(yum) do this.
            if 'fileID' in node.mapping and not 'guid' in node.mapping:
                if node.mapping['fileID'] != '0':
                    old_id = node.mapping['fileID']
                    new_id = self.mapId(old_id)
                    node.mapping['fileID'] = str(new_id)
        if hasattr(node, "forEach"):
            node.forEach(self.mergeIterator)

    def peekNodeOfClass(self, classId):
        for node in self.nodes:
            if node.class_id == classId:
                return node
        return None

    def popNodeOfClass(self, classId):
        result = None
        for node in self.nodes:
            if node.class_id == classId:
                result = node
                self.nodes.remove(result)
                break
        if result:
            del self.id_to_node[result.anchor]
        return result

    def pushNode(self, node):
        self.nodes.append(node)
        self.id_to_node[node.anchor] = node

    # Merges two animator controllers and returns the result. Any identifiers
    # in the animators are reassigned in a new namespace. The mappings from old
    # identifiers to new identifiers are recorded in `self.id_mapping0` and
    # `self.id_mapping1`.
    def mergeAnimatorControllers(self, ctrl0, ctrl1):
        ctrl0 = copy.deepcopy(ctrl0)
        ctrl1 = copy.deepcopy(ctrl1)

        self.id_mapping0 = {}
        self.id_mapping1 = {}

        p0 = ctrl0.mapping['AnimatorController'].mapping['m_AnimatorParameters']
        p1 = ctrl1.mapping['AnimatorController'].mapping['m_AnimatorParameters']

        a0 = ctrl0.mapping['AnimatorController'].mapping['m_AnimatorLayers']
        a1 = ctrl1.mapping['AnimatorController'].mapping['m_AnimatorLayers']

        self.id_mapping = self.id_mapping0
        p0.forEach(self.mergeIterator)
        a0.forEach(self.mergeIterator)

        # Hack to prevent ctrl1 from getting a new ID for the animator.
        # TODO(yum) delete this?
        #del self.class_to_next_id['91']

        self.id_mapping = self.id_mapping1
        p1.forEach(self.mergeIterator)
        a1.forEach(self.mergeIterator)

        p0.sequence += p1.sequence
        a0.sequence += a1.sequence

        for elm in p0.sequence:
            elm.mapping['m_Controller'].mapping['fileID'] = ctrl0.anchor
        for elm in a0.sequence:
            elm.mapping['m_Controller'].mapping['fileID'] = ctrl0.anchor

        return ctrl0

    def merge(self, other):
        ctrl0 = self.popNodeOfClass('91')
        ctrl1 = other.popNodeOfClass('91')
        # Merge animators and populate `self.id_mapping0` and
        # `self.id_mapping1.
        merged_anim = self.mergeAnimatorControllers(ctrl0, ctrl1)

        # Mapping from class ID (string) to new class ID (int)
        self.id_mapping = self.id_mapping0
        for node in self.nodes:
            new_id = self.mapId(node.anchor)
            node.anchor = str(new_id)
            node.forEach(self.mergeIterator)

        self.id_mapping = self.id_mapping1
        for node in other.nodes:
            new_id = self.mapId(node.anchor)
            node.anchor = str(new_id)
            node.forEach(self.mergeIterator)

        nodes = self.nodes
        self.nodes = []
        self.id_to_node = {}
        self.pushNode(merged_anim)
        self.addNodes(nodes)
        self.addNodes(other.nodes)

    # TODO(yum) support overwriting duplicates
    def addParameter(self, param_name, param_type):
        unity_type = None
        if param_type == float:
            unity_type = '1'
        elif param_type == int:
            unity_type = '3'
        elif param_type == bool:
            unity_type = '4'

        anim = self.peekNodeOfClass('91')
        params = anim.mapping['AnimatorController'].mapping['m_AnimatorParameters']

        for p in params.sequence:
            if p.mapping['m_Name'] == param_name:
                return

        param = params.addChildMapping()
        param.mapping['m_Name'] = param_name
        param.mapping['m_Type'] = unity_type
        param.mapping['m_DefaultFloat'] = '0'
        param.mapping['m_DefaultInt'] = '0'
        param.mapping['m_DefaultBool'] = '0'
        ctrl = param.addChildMapping('m_Controller')
        ctrl.mapping['fileID'] = anim.anchor

    def addLayer(self, layer_name, add_to_head = False, weight: float = 1.0) -> UnityDocument:
        # Add layer to controller
        anim = self.peekNodeOfClass('91')
        layers = anim.mapping['AnimatorController'].mapping['m_AnimatorLayers']
        layer = layers.addChildMapping(add_to_head = add_to_head)
        layer.mapping['serializedVersion'] = '5'
        layer.mapping['m_Name'] = layer_name
        new_id = self.allocateId()
        layer.addChildMapping('m_StateMachine').mapping['fileID'] = str(new_id)
        layer.addChildMapping('m_Mask').mapping['fileID'] = '0'
        layer.addChildSequence('m_Motions')
        layer.addChildSequence('m_Behaviours')
        layer.mapping['m_BlendingMode'] = '0'
        layer.mapping['m_SyncedLayerIndex'] = '-1'
        layer.mapping['m_DefaultWeight'] = str(weight)
        layer.mapping['m_IKPass'] = '0'
        layer.mapping['m_SyncedLayerAffectsTiming'] = '0'
        layer.addChildMapping('m_Controller').mapping['fileID'] = anim.anchor

        # Create layer object
        layer = UnityDocument()
        layer.class_id = "1107"
        layer.anchor = str(new_id)
        mach = layer.addChildMapping('AnimatorStateMachine')

        mach.mapping['serializedVersion'] = '6'

        mach.mapping['m_ObjectHideFlags'] = '1'
        mach.addChildMapping('m_CorrespondingSourceObject').mapping['fileID'] = '0'
        mach.addChildMapping('m_PrefabInstance').mapping['fileID'] = '0'
        mach.addChildMapping('m_PrefabAsset').mapping['fileID'] = '0'
        mach.mapping['m_Name'] = layer_name
        mach.addChildSequence('m_ChildStates')
        mach.addChildSequence('m_ChildStateMachines')
        mach.addChildSequence('m_AnyStateTransitions')
        mach.addChildSequence('m_EntryTransitions')
        mach.addChildMapping('m_StateMachineTransitions')
        mach.addChildSequence('m_StateMachineBehaviours')
        pos = mach.addChildMapping('m_AnyStatePosition')
        pos.mapping['x'] = '50'
        pos.mapping['y'] = '20'
        pos.mapping['z'] = '0'
        pos = mach.addChildMapping('m_EntryPosition')
        pos.mapping['x'] = '50'
        pos.mapping['y'] = '120'
        pos.mapping['z'] = '0'
        pos = mach.addChildMapping('m_ExitPosition')
        pos.mapping['x'] = '800'
        pos.mapping['y'] = '120'
        pos.mapping['z'] = '0'
        pos = mach.addChildMapping('m_ParentStateMachinePosition')
        pos.mapping['x'] = '800'
        pos.mapping['y'] = '20'
        pos.mapping['z'] = '0'
        mach.addChildMapping('m_DefaultState')

        self.nodes.append(layer)
        return layer

    def addAnimatorState(self, layer, state_name, is_default_state = False,
            dx = 0, dy = 0) -> UnityDocument:
        # Create animation state
        parser = UnityParser()
        parser.parse(ANIMATION_STATE_TEMPLATE)
        new_anim = UnityAnimator()
        new_anim.addNodes(parser.nodes)
        node = new_anim.nodes[0]

        new_id = self.allocateId()
        node.class_id = "1102"
        node.anchor = str(new_id)
        state = node.mapping['AnimatorState']
        state.mapping['m_Name'] = state_name
        #state.mapping['m_Motion'].mapping['guid'] = anim_guid
        self.nodes.append(node)

        # Add state to layer
        child_state = layer.mapping['AnimatorStateMachine'].mapping['m_ChildStates'].addChildMapping()
        child_state.mapping['serializedVersion'] = '1'
        child_state.addChildMapping('m_State').mapping['fileID'] = str(new_id)
        state_pos = child_state.addChildMapping('m_Position')
        state_pos.mapping['x'] = str(280 + dx)
        state_pos.mapping['y'] = str(80 + dy)
        state_pos.mapping['z'] = '0'

        if is_default_state:
            layer.mapping['AnimatorStateMachine'].mapping['m_DefaultState'].mapping['fileID'] = str(new_id)

        return node

    def setAnimatorStateAnimation(self, anim_state, anim_guid):
        anim_state.mapping['AnimatorState'].mapping['m_Motion'].mapping['guid'] = anim_guid
        anim_state.mapping['AnimatorState'].mapping['m_Motion'].mapping['fileID'] = '7400000'
        anim_state.mapping['AnimatorState'].mapping['m_Motion'].mapping['type'] = '2'

    # Adds a blend tree which uses the parameter named `param_name` to blend
    # between anim_lo and anim_hi. Also creates the corresponding animation
    # state.
    def addAnimatorBlendTree(self, layer, state_name, param_name,
            anim_guid_lo, anim_guid_hi, dx = 0, dy = 0,
            lo_threshold = -1.0, hi_threshold = 1.0,
            is_default_state = False) -> UnityDocument:
        # Create the blend tree.
        parser = UnityParser()
        parser.parse(BLEND_TREE_TEMPLATE)
        new_anim = UnityAnimator()
        new_anim.addNodes(parser.nodes)
        node = new_anim.nodes[0]

        new_id = self.allocateId()
        node.class_id = "206"
        node.anchor = str(new_id)
        tree = node.mapping['BlendTree']
        tree.mapping['m_Name'] = state_name
        # Low animation
        tree.mapping['m_Childs'].sequence[0].mapping['m_Motion'].mapping['guid'] = anim_guid_lo
        tree.mapping['m_Childs'].sequence[0].mapping['m_DirectBlendParameter'] = param_name
        tree.mapping['m_Childs'].sequence[0].mapping['m_Threshold'] = str(lo_threshold)
        # High animation
        tree.mapping['m_Childs'].sequence[1].mapping['m_Motion'].mapping['guid'] = anim_guid_hi
        tree.mapping['m_Childs'].sequence[1].mapping['m_DirectBlendParameter'] = param_name
        tree.mapping['m_Childs'].sequence[1].mapping['m_Threshold'] = str(hi_threshold)

        tree.mapping['m_BlendParameter'] = param_name
        tree.mapping['m_BlendParameterY'] = param_name

        self.nodes.append(node)

        # Create the corresponding animation state.
        anim_state = self.addAnimatorState(layer, state_name, is_default_state, dx = dx, dy =
                dy)
        anim_state.mapping['AnimatorState'].mapping['m_Motion'].mapping['fileID'] = node.anchor

        return anim_state

    def addTransition(self, dst_state, dur_s = 0.0):
        # Create animation state
        parser = UnityParser()
        parser.parse(TRANSITION_TEMPLATE)
        new_transition = UnityAnimator()
        new_transition.addNodes(parser.nodes)
        node = new_transition.nodes[0]

        new_id = self.allocateId()
        node.class_id = "1101"
        node.anchor = str(new_id)
        state = node.mapping['AnimatorStateTransition']
        state.mapping['m_DstState'].mapping['fileID'] = copy.copy(dst_state.anchor)
        state.mapping['m_TransitionDuration'] = dur_s
        self.nodes.append(node)

        return node

    def fixWriteDefaults(self, guid_map, generated_anim_path):
        # TODO(yum) we should have an Animation class which encapsulates all
        # this stuff.
        parser = UnityParser()
        parser.parse(WRITE_DEFAULTS_ANIM_TEMPLATE)
        new_anim = UnityAnimator()
        new_anim.addNodes(parser.nodes)

        new_clip = new_anim.peekNodeOfClass('74').mapping['AnimationClip']
        curve_template = new_clip.mapping['m_FloatCurves'].sequence[0]
        new_clip.mapping['m_FloatCurves'].sequence = []
        new_clip.mapping['m_EditorCurves'].sequence = []

        # Keep track of the (attribute, path) tuples we've already set to avoid
        # animating the same thing twice.
        attributes_set = set()

        animator_state_id = '1102'
        for node in self.nodes:
            if node.class_id != animator_state_id:
                continue

            # Looking at an animator state.
            if node.mapping['AnimatorState'].mapping['m_WriteDefaultValues'] != '1':
                continue

            # Disable write defaults.
            node.mapping['AnimatorState'].mapping['m_WriteDefaultValues'] = '0'

            # Looking at an animator state with write defaults.
            motion = node.mapping['AnimatorState'].mapping['m_Motion']
            # Some animations have write defaults but don't trigger an
            # animation. No idea what that's about. For now, just ignore.
            if not 'guid' in motion.mapping:
                continue
            guid = motion.mapping['guid']

            # Again, not really sure what's going on here, just ignore and
            # revisit if we hit problems.
            if not guid in guid_map.keys():
                continue

            # OK, we found an animation with write defaults, and we know where
            # the animation lives. Crack it open and see what it's writing.
            animation_path = guid_map[guid]
            print("Animation has write defaults: {}".format(animation_path), file=sys.stderr)
            parser = UnityParser()
            parser.parseFile(animation_path)
            anim = UnityAnimator()
            anim.addNodes(parser.nodes)

            clip = anim.peekNodeOfClass('74')

            for curve in clip.mapping['AnimationClip'].mapping['m_FloatCurves'].sequence:
                attr = curve.mapping['attribute']
                path = curve.mapping['path']
                if (attr, path) in attributes_set:
                    continue
                #print("Fix attr/path {}/{}".format(attr, path), file=sys.stderr)
                attributes_set.add((attr, path))

                new_curve = curve_template.copy()
                new_curve.mapping['attribute'] = attr
                new_curve.mapping['path'] = path

                new_clip.mapping['m_FloatCurves'].sequence.append(new_curve)
                new_clip.mapping['m_EditorCurves'].sequence.append(new_curve)

                #print("len float curves: {}".format(len(new_clip.mapping['m_FloatCurves'].sequence)), file=sys.stderr)

    def generateOffAnimationForGuid(self, guid_map, generated_anim_dir, guid):
        # Looking at an animation.
        if not guid in guid_map.keys():
            return

        animation_path = guid_map[guid]
        print("Checking animation at {}".format(animation_path), file=sys.stderr)
        parser = UnityParser()
        parser.parseFile(animation_path)
        anim = UnityAnimator()
        anim.addNodes(parser.nodes)

        clip = anim.peekNodeOfClass('74')

        has_nonzero = False
        curve_members = ["m_FloatCurves", "m_EditorCurves"]
        for memb in curve_members:
            for curve in clip.mapping['AnimationClip'].mapping[memb].sequence:
                attr = curve.mapping['attribute']
                path = curve.mapping['path']

                for m_curve in curve.mapping['curve'].mapping['m_Curve'].sequence:
                    if m_curve.mapping['value'] != '0':
                        has_nonzero = True
                    m_curve.mapping['value'] = '0'

        if not has_nonzero:
            print("Animation does not set anything nonzero")
            return

        print("Animation sets things nonzero, fixing")

        new_anim_path = "OFF_{}".format(os.path.basename(animation_path))
        new_anim_path = "{}/{}".format(generated_anim_dir, new_anim_path)

        with open(new_anim_path, "w", encoding="utf-8") as f:
            f.write(str(anim))

        meta = Metadata()
        with open(new_anim_path + ".meta", "w", encoding="utf-8") as f:
            f.write(str(meta))

    def generateOffAnimationsAnimStates(self, guid_map, generated_anim_dir):
        animator_state_id = '1102'
        for node in self.nodes:
            if node.class_id != animator_state_id:
                continue

            # Looking at an animation state.
            motion = node.mapping['AnimatorState'].mapping['m_Motion']
            if not 'guid' in motion.mapping:
                continue
            guid = motion.mapping['guid']
            self.generateOffAnimationForGuid(guid_map, generated_anim_dir, guid)


    def generateOffAnimationsBlendTrees(self, guid_map, generated_anim_dir):
        animator_state_id = '206'
        for node in self.nodes:
            if node.class_id != animator_state_id:
                continue

            # Looking at an animation state.
            for child in node.mapping['BlendTree'].mapping['m_Childs'].sequence:
                motion = child.mapping['m_Motion']

                if not 'guid' in motion.mapping:
                    continue
                guid = motion.mapping['guid']
                self.generateOffAnimationForGuid(guid_map, generated_anim_dir, guid)

    def generateOffAnimations(self, guid_map, generated_anim_dir):
        self.generateOffAnimationsAnimStates(guid_map, generated_anim_dir)
        self.generateOffAnimationsBlendTrees(guid_map, generated_anim_dir)

    def addTransitionBooleanCondition(self, from_state, trans, param, branch):
        # Populate the transition's condition logic.
        cond = trans.mapping['AnimatorStateTransition'].mapping['m_Conditions'].addChildMapping()
        if branch:
            cond.mapping['m_ConditionMode'] = '1'
        else:
            cond.mapping['m_ConditionMode'] = '2'
        cond.mapping['m_ConditionEvent'] = param
        cond.mapping['m_EventThreshold'] = '0'
        # Register the transition with the `from_state`.
        if from_state:
            from_state_trans = from_state.mapping['AnimatorState'].mapping['m_Transitions'].addChildMapping()
            from_state_trans.mapping['fileID'] = copy.copy(trans.anchor)

    def addTransitionIntegerEqualityCondition(self, from_state, trans, param, param_val):
        # Populate the transition's condition logic.
        cond = trans.mapping['AnimatorStateTransition'].mapping['m_Conditions'].addChildMapping()
        cond.mapping['m_ConditionMode'] = '6'
        cond.mapping['m_ConditionEvent'] = param
        # Curiously, the typo ("treshold" only has 1 'h') is needed for this to
        # work, but not for boolean conditions to work.
        cond.mapping['m_EventTreshold'] = str(param_val)
        # Register the transition with the `from_state`.
        if from_state:
            from_state_trans = from_state.mapping['AnimatorState'].mapping['m_Transitions'].addChildMapping()
            from_state_trans.mapping['fileID'] = trans.anchor

    def addTransitionIntegerGreaterCondition(self, from_state, trans, param, param_val):
        # Populate the transition's condition logic.
        cond = trans.mapping['AnimatorStateTransition'].mapping['m_Conditions'].addChildMapping()
        cond.mapping['m_ConditionMode'] = '3'
        cond.mapping['m_ConditionEvent'] = param
        cond.mapping['m_EventThreshold'] = str(param_val)
        # Register the transition with the `from_state`.
        if from_state:
            from_state_trans = from_state.mapping['AnimatorState'].mapping['m_Transitions'].addChildMapping()
            from_state_trans.mapping['fileID'] = trans.anchor

    # TODO(yum) this should be factored out into generate_fx.py
    def addTasttToggle(self, off_anim_path, on_anim_path, toggle_param,
            guid_map):
        self.addParameter(toggle_param, bool)

        off_anim_meta = Metadata()
        off_anim_meta.loadOrCreate(off_anim_path, guid_map)

        on_anim_meta = Metadata()
        on_anim_meta.loadOrCreate(on_anim_path, guid_map)

        layer = self.addLayer('TaSTT_Toggle')
        off_anim = self.addAnimatorState(layer, 'TaSTT_Toggle_Off', is_default_state = True)
        self.setAnimatorStateAnimation(off_anim, off_anim_meta.guid)
        on_anim = self.addAnimatorState(layer, 'TaSTT_Toggle_On')
        self.setAnimatorStateAnimation(on_anim, on_anim_meta.guid)

        # TODO(yum) make a Transition class with methods for adding boolean
        # conditions
        off_to_on = self.addTransition(on_anim)
        self.addTransitionBooleanCondition(off_anim, off_to_on, toggle_param, True)

        on_to_off = self.addTransition(off_anim)
        self.addTransitionBooleanCondition(on_anim, on_to_off, toggle_param, False)

    def setNoopAnimations(self, guid_map, noop_anim_path):
        noop_anim_meta = Metadata()
        noop_anim_meta.loadOrCreate(noop_anim_path, guid_map)

        for node in self.nodes:
            if node.class_id != "1102":
                continue
            motion = node.mapping['AnimatorState'].mapping['m_Motion']
            replace = False

            name = node.mapping['AnimatorState'].mapping['m_Name']
            anchor = node.anchor

            # As of 8 May 2023, idle states look like this:
            #   m_Motion: {fileID: 7400000, guid: e5881c5b0c09be854b0fd6fd8144333f, type: 2}
            # Before that, they looked like this:
            #   m_Motion: {fileID: 0}
            # The first predicate looks for the new pattern.
            # The second predicate looks for the second pattern.
            if "fileID" in motion.mapping.keys() and \
                    "guid" in motion.mapping.keys() and \
                    not motion.mapping["guid"] in guid_map:
                motion.mapping["fileID"] = "7400000"
                print(f"Set noop animation to guid {noop_anim_meta.guid} in state {node.anchor}")
                motion.mapping["guid"] = noop_anim_meta.guid
                motion.mapping["type"] = "2"
            elif not ("fileID" in motion.mapping.keys() and
                    motion.mapping["fileID"] != "0") and not ("guid" in
                            motion.mapping.keys() and motion.mapping["guid"] in
                            guid_map):
                motion.mapping["fileID"] = "7400000"
                print(f"Set noop animation to guid {noop_anim_meta.guid} in state {node.anchor}")
                motion.mapping["guid"] = noop_anim_meta.guid
                motion.mapping["type"] = "2"
            else:
                #print(f"Skipping state {anchor} / {name}")
                pass

def unityYamlToString(nodes):
    lines = []
    preamble = """
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
"""[1:][:-1]
    lines.append(preamble)
    for doc in nodes:
        lines.append("--- !u!" + doc.class_id + " &" + doc.anchor)
        lines.append(str(doc))
    result = '\n'.join(lines)

    for i in range(0,10):
        result = result.replace("\n\n", "\n")

    return result

class UnityParser:
    STREAM_START = 100
    STREAM_END = 199

    DOCUMENT_START = 200
    DOCUMENT_END = 299

    MAPPING_START = 300
    MAPPING_KEY = 301

    SEQUENCE_VALUE = 400

    def __init__(self):
        self.state = self.STREAM_START
        self.cur_scalar = None
        self.cur_node = None

        # Simple list of parsed documents. Populated by parse().
        self.nodes = []
        self.prev_states = []

    def __str__(self):
        return unityYamlToString(self.nodes)

    def pushState(self, state):
        self.prev_states.append(self.state)
        self.state = state
        #print("state {} ({})".format(self.state, len(self.prev_states)))

    def popState(self):
        self.state = self.prev_states[-1]
        self.prev_states = self.prev_states[0:len(self.prev_states) - 1]
        #print("state {} ({})".format(self.state, len(self.prev_states)))
        return self.state

    def cleanYaml(self, yaml_str):
        lines = []
        first_document = True
        got_document = False
        for line in yaml_str.split("\n"):
            # Add end-of-document indicators.
            if line.startswith("---"):
                got_document = True
                if not first_document:
                    lines.append("...\n")
                first_document = False

            # Remove class ID tag from each block.
            if line.startswith("---"):
                parts = line.split()
                lines.append(parts[0] + " " + parts[2] + "\n")
                continue
            lines.append(line)

        if got_document:
            lines.append("...\n")
        return '\n'.join(lines)

    def getClassIds(self, yaml_str):
        anchor_to_class_id = {}
        for line in yaml_str.split("\n"):
            if not line.startswith("---"):
                continue

            parts = line.split()
            class_id = parts[1][3:]
            anchor = parts[2][1:]
            anchor_to_class_id[anchor] = class_id

        return anchor_to_class_id

    def parseFile(self, yaml_file):
        yaml_str = ""
        with open(yaml_file, "r", encoding="utf-8") as f:
            yaml_str = f.read()
        return self.parse(yaml_str)

    def parse(self, yaml_str):
        anchor_to_class_id = self.getClassIds(yaml_str)
        yaml_str = self.cleanYaml(yaml_str)

        for event in yaml.parse(yaml_str):
            if isinstance(event, yaml.StreamStartEvent):
                if len(self.prev_states) > 0:
                    raise Exception("Multiple StreamStartEvents received")
                self.pushState(self.STREAM_START)

            elif isinstance(event, yaml.StreamEndEvent):
                if self.state != self.STREAM_START:
                    raise Exception("Document end received after state {}".format(self.state))
                self.popState()
                if len(self.prev_states) > 0:
                    raise Exception("Extra states at stream end")

            elif isinstance(event, yaml.DocumentStartEvent):
                if self.state != self.STREAM_START and self.state != self.DOCUMENT_END:
                    raise Exception("Document start received after state {}".format(self.state))
                self.pushState(self.DOCUMENT_START)

            elif isinstance(event, yaml.DocumentEndEvent):
                if self.state != self.DOCUMENT_START:
                    raise Exception("Document end received after state {}".format(self.state))
                self.popState()
                self.nodes.append(self.cur_node)
                self.cur_node = None

            elif isinstance(event, yaml.MappingStartEvent):
                if self.cur_node == None:
                    self.cur_node = UnityDocument()
                    self.cur_node.anchor = event.anchor
                    self.cur_node.class_id = anchor_to_class_id[event.anchor]
                else:
                    self.cur_node = self.cur_node.addChildMapping(self.cur_scalar)
                self.pushState(self.MAPPING_START)

            elif isinstance(event, yaml.MappingEndEvent):
                if self.state != self.MAPPING_START:
                    raise Exception("Mapping end received after state {}".format(self.state))
                self.popState()
                if self.state == self.MAPPING_KEY:
                    self.popState()
                if self.cur_node.parent != None:
                    self.cur_node = self.cur_node.parent

            elif isinstance(event, yaml.SequenceStartEvent):
                self.cur_node = self.cur_node.addChildSequence(self.cur_scalar)
                self.pushState(self.SEQUENCE_VALUE)

            elif isinstance(event, yaml.SequenceEndEvent):
                if self.state != self.SEQUENCE_VALUE:
                    raise Exception("Sequence end received after state {}".format(self.state))
                self.popState()
                if self.state == self.MAPPING_KEY:
                    self.popState()
                self.cur_node = self.cur_node.parent

            elif isinstance(event, yaml.ScalarEvent):
                if self.state == self.MAPPING_START:
                    self.cur_scalar = event.value
                    self.pushState(self.MAPPING_KEY)
                elif self.state == self.MAPPING_KEY:
                    self.cur_node.mapping[self.cur_scalar] = event.value
                    self.popState()
                elif self.state == self.SEQUENCE_VALUE:
                    self.cur_node.sequence.append(event.value)
                else:
                    raise Exception("Scalar event received after state {}".format(self.state))
            else:
                raise Exception("Unhandled event {}".format(event))
            continue

class MulticoreUnityParser:
    def parseFile(self, yaml_file):
        yaml_str = ""
        with open(yaml_file, "r", encoding="utf-8") as f:
            yaml_str = f.read()
        return self.parse(yaml_str)

    def parse(self, yaml_str):
        lines = []
        documents = []
        first = True
        n_lines = 0
        for line in yaml_str.split("\n"):
            n_lines += 1
            if line.startswith("---"):
                if not first:
                    documents.append("\n".join(lines))
                    lines = []
                first = False
            lines.append(line)
        if len(lines) > 0:
            documents.append("\n".join(lines))
            lines = []
        print("Got {} documents out of {} lines".format(len(documents), n_lines), file=sys.stderr)

        # Divide the work evenly among the # of CPUs we have available.
        n_threads = os.cpu_count()
        window_size = int(math.ceil(len(documents) / n_threads))
        merge_window = []
        merged_documents = []
        for i in range(0, len(documents)):
            if i > 0 and i % window_size == 0:
                merged_documents.append("\n".join(merge_window))
                merge_window = []
            merge_window.append(documents[i])
        if len(merge_window) > 0:
            merged_documents.append("\n".join(merge_window))
            merge_window = []
        documents = merged_documents

        mgr = mp.Manager()

        print("Spawning {} threads".format(len(documents)), file=sys.stderr)
        threads = []
        for document in documents:
            res = mgr.dict()
            thread = mp.Process(target = self.parseOneSerial, args = (document, res,))
            threads.append((thread, res))
            thread.start()

        print("Joining threads", file=sys.stderr)
        nodes = []
        for thread, res in threads:
            thread.join()
            nodes += res['nodes']

        print("Creating animator", file=sys.stderr)
        result = UnityAnimator()
        result.addNodes(nodes)

        return result

    def parseOneSerial(self, document, res):
        parser = UnityParser()
        parser.parse(document)
        res['nodes'] = parser.nodes

    def parseFile(self, yaml_file):
        yaml_str = ""
        with open(yaml_file, "r", encoding="utf-8") as f:
            yaml_str = f.read()
        return self.parse(yaml_str)

def getGuidMap(d):
    result = {}
    for f in os.scandir(d):
        path = f.path
        if f.is_dir():
            result.update(getGuidMap(path))
        if not f.is_file():
            continue
        suffix = ".meta"
        if path.endswith(suffix):
            with open(path, "r", encoding="utf-8") as f:
                for line in f:
                    if line.startswith("guid"):
                        guid = line.split()[1]
                        result[guid] = path[:-len(suffix)]
    return result

if __name__ == "__main__":
    os.chdir(os.path.dirname(os.path.abspath(__file__)))

    parser = argparse.ArgumentParser()
    parser.add_argument("cmd", type=str, help="One of merge, guid_map, fix_write_defaults")
    parser.add_argument("--fx0", type=str, help="The first animator to merge")
    parser.add_argument("--fx1", type=str, help="The second animator to merge")
    parser.add_argument("--fx_dest", type=str, help="The path at which to " +
            "save the generated/merged animator")
    parser.add_argument("--project_root", type=str, help="The path to the " +
            "Unity project Assets folder")
    parser.add_argument("--save_to", type=str, help="The path to save the " +
            "result of the computation")
    parser.add_argument("--guid_map", type=str, help="Path to guid.map, " +
            "generated by a previous call to `guid_map`")
    parser.add_argument("--guid_map_append", type=bool, help="If set, " +
            "append to GUID map instead of overwriting.")
    parser.add_argument("--gen_anim_dir", type=str, help="The folder under which generated animations are stored")
    args = parser.parse_args()

    if args.cmd == "merge":
        if not args.fx0 or not args.fx1 or not args.fx_dest:
            print("--fx0, --fx1, and --fx_dest required", file=sys.stderr)
            parser.print_help()
            parser.exit(1)

        print("Parsing {}".format(args.fx0), file=sys.stderr)
        parser0 = MulticoreUnityParser()
        anim0 = parser0.parseFile(args.fx0)

        arg1 = "TaSTT_fx.controller"
        print("Parsing {}".format(args.fx1), file=sys.stderr)
        parser1 = MulticoreUnityParser()
        anim1 = parser1.parseFile(args.fx1)

        print("Merging animators", file=sys.stderr)
        anim0.merge(anim1)

        print("Serializing to {}".format(args.fx_dest), file=sys.stderr)
        with open(args.fx_dest, "w", encoding="utf-8") as f:
            f.write(unityYamlToString(anim0.nodes))

    elif args.cmd == "guid_map":
        if not args.project_root or not args.save_to:
            print("--project_root and --save_to required")
            parser.print_help()
            parser.exit(1)

        print("Looking up GUIDs under {}".format(args.project_root),
                file=sys.stderr)
        guid_map = getGuidMap(args.project_root)

        save_to_dir = os.path.dirname(args.save_to)
        os.makedirs(save_to_dir, exist_ok=True)

        if args.guid_map_append:
            tmp_map = {}
            with open(args.save_to, "rb") as f:
                tmp_map = pickle.load(f)
            # combine guid_map and tmp_map
            guid_map = {**guid_map, **tmp_map}
        print("Saving to {}".format(args.save_to), file=sys.stderr)
        with open(args.save_to, 'wb') as f:
            pickle.dump(guid_map, f)
    elif args.cmd == "fix_write_defaults":
        if not args.fx0 or not args.guid_map:
            print("--fx0 and --guid_map required")
            parser.print_help()
            parser.exit(1)

        guid_map = {}
        with open(args.guid_map, 'rb') as f:
            guid_map = pickle.load(f)

        print("Parsing {}".format(args.fx0), file=sys.stderr)
        parser0 = MulticoreUnityParser()
        anim = parser0.parseFile(args.fx0)

        print("Fixing write defaults", file=sys.stderr)
        anim_dir = "generated/animations/"
        os.makedirs(anim_dir, exist_ok=True)
        anim.fixWriteDefaults(guid_map, anim_dir + "TaSTT_Reset_Animation.anim")
        print(str(anim))

    elif args.cmd == "gen_off_anims":
        if not args.fx0 or not args.guid_map:
            print("--fx0 and --guid_map required")
            parser.print_help()
            parser.exit(1)

        guid_map = {}
        with open(args.guid_map, 'rb') as f:
            guid_map = pickle.load(f)

        print("Parsing {}".format(args.fx0), file=sys.stderr)
        parser0 = MulticoreUnityParser()
        anim = parser0.parseFile(args.fx0)

        print("Generating off animations", file=sys.stderr)
        anim_dir = "generated/animations/"
        os.makedirs(anim_dir, exist_ok=True)
        anim.generateOffAnimations(guid_map, "generated/animations")

    elif args.cmd == "add_toggle":
        if not args.fx0 or not args.fx_dest or not args.gen_anim_dir or not args.guid_map:
            print("--fx0, --fx_dest, --gen_anim_dir and --guid_map required")
            parser.print_help()
            parser.exit(1)

        guid_map = {}
        with open(args.guid_map, 'rb') as f:
            guid_map = pickle.load(f)

        print("Parsing {}".format(args.fx0), file=sys.stderr)
        parser0 = MulticoreUnityParser()
        anim = parser0.parseFile(args.fx0)

        print("Adding toggle", file=sys.stderr)
        anim.addTasttToggle(args.gen_anim_dir + "/TaSTT_Toggle_Off.anim",
                args.gen_anim_dir + "/TaSTT_Toggle_On.anim", "TaSTT_Toggle",
                guid_map)

        print("Serializing to {}".format(args.fx_dest), file=sys.stderr)
        with open(args.fx_dest, "w", encoding="utf-8") as f:
            f.write(str(anim))

        with open(args.guid_map, 'wb') as f:
            pickle.dump(guid_map, f)

    elif args.cmd == "fast_parse_test":
        if not args.fx0:
            print("--fx0 required")
            parser.print_help()
            parser.exit(1)

        print("Parsing {}".format(args.fx0), file=sys.stderr)
        parser0 = MulticoreUnityParser()
        anim = parser0.parseFile(args.fx0)
        print(str(anim))

    elif args.cmd == "set_noop_anim":
        if not args.fx0 or not args.fx_dest or not args.gen_anim_dir or not args.guid_map:
            print("--fx0, --fx_dest, --gen_anim_dir and --guid_map required")
            parser.print_help()
            parser.exit(1)

        guid_map = {}
        with open(args.guid_map, 'rb') as f:
            guid_map = pickle.load(f)

        print("Parsing {}".format(args.fx0), file=sys.stderr)
        parser = MulticoreUnityParser()
        anim = parser.parseFile(args.fx0)

        anim.setNoopAnimations(guid_map, args.gen_anim_dir + "/TaSTT_Do_Nothing.anim")

        with open(args.fx_dest, "w", encoding="utf-8") as f:
            f.write(str(anim))

    else:
        print("Unrecognized command: {}".format(args.cmd))