diff options
| author | yum <yum.food.vr@gmail.com> | 2022-12-20 14:20:17 -0800 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2022-12-20 14:20:17 -0800 |
| commit | a048e2c1b1940805c0dcb29bc162f804ca463214 (patch) | |
| tree | 3c8d35007fcc4e3470650b80db72a3ce3c04fa28 /Scripts/libunity.py | |
| parent | 8d225cfd66dfb60998b4eab43d8aa3b287375695 (diff) | |
GUI can now generate animator
Still need to generate params & merge menus. Getting close....
Diffstat (limited to 'Scripts/libunity.py')
| -rw-r--r-- | Scripts/libunity.py | 70 |
1 files changed, 53 insertions, 17 deletions
diff --git a/Scripts/libunity.py b/Scripts/libunity.py index 7223568..0c5228a 100644 --- a/Scripts/libunity.py +++ b/Scripts/libunity.py @@ -228,6 +228,23 @@ class Metadata: 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") 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) @@ -911,14 +928,15 @@ class UnityAnimator(): 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): + 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.load(off_anim_path) + off_anim_meta.loadOrCreate(off_anim_path, guid_map) on_anim_meta = Metadata() - on_anim_meta.load(on_anim_path) + 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) @@ -936,7 +954,7 @@ class UnityAnimator(): def setNoopAnimations(self, guid_map, noop_anim_path): noop_anim_meta = Metadata() - noop_anim_meta.load(noop_anim_path) + noop_anim_meta.loadOrCreate(noop_anim_path, guid_map) for node in self.nodes: if node.class_id != "1102": @@ -1215,10 +1233,14 @@ def getGuidMap(d): 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 " + @@ -1227,11 +1249,12 @@ if __name__ == "__main__": "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: - print("--fx0 and --fx1 required", file=sys.stderr) + 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) @@ -1247,8 +1270,9 @@ if __name__ == "__main__": print("Merging animators", file=sys.stderr) anim0.merge(anim1) - print("Serializing", file=sys.stderr) - print(unityYamlToString(anim0.nodes)) + print("Serializing to {}".format(args.fx_dest), file=sys.stderr) + with open(args.fx_dest, "w") as f: + f.write(unityYamlToString(anim0.nodes)) elif args.cmd == "guid_map": if not args.project_root or not args.save_to: @@ -1312,19 +1336,30 @@ if __name__ == "__main__": anim.generateOffAnimations(guid_map, "generated/animations") elif args.cmd == "add_toggle": - if not args.fx0: - print("--fx0 required") + 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("Animations/TaSTT_Toggle_Off.anim", - "Animations/TaSTT_Toggle_On.anim", "TaSTT_Toggle") - print(str(anim)) + 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") 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: @@ -1338,8 +1373,8 @@ if __name__ == "__main__": print(str(anim)) elif args.cmd == "set_noop_anim": - if not args.fx0 or not args.guid_map: - print("--fx0 and --guid_map required") + 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) @@ -1351,9 +1386,10 @@ if __name__ == "__main__": parser = MulticoreUnityParser() anim = parser.parseFile(args.fx0) - anim.setNoopAnimations(guid_map, "Animations/TaSTT_Do_Nothing.anim") + anim.setNoopAnimations(guid_map, args.gen_anim_dir + "/TaSTT_Do_Nothing.anim") - print(str(anim)) + with open(args.fx_dest, "w") as f: + f.write(str(anim)) else: print("Unrecognized command: {}".format(args.cmd)) |
