Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- import json
- import yaml
- import sys
- yamlname = sys.argv[1]
- jsonname = sys.argv[2]
- print("Loading from %s/%s" % (yamlname, jsonname))
- with open(yamlname) as fh:
- yamldat = yaml.load(fh.read(), yaml.Loader)
- jsondat = json.loads(open(jsonname).read())
- ymax = 0
- for sprite in jsondat["frames"].values():
- ymax = max(ymax, sprite["frame"]["y"] + sprite["frame"]["h"])
- print("ymax is %d" % ymax)
- newsprites = []
- usedNames = set()
- for sprite in yamldat["TextureImporter"]["spriteSheet"]["sprites"]:
- rect = sprite["rect"]
- cx = int(rect["x"]) + int(rect["width"]) / 2
- cy = int(rect["y"]) + int(rect["height"]) / 2
- best = None
- bestDist = 0
- for name, jsonsprite in jsondat["frames"].items():
- frame = jsonsprite["frame"]
- scx = int(frame["x"]) + int(frame["w"]) / 2
- # Note Y axis is inverted, because of course it is.
- scy = ymax - (int(frame["y"]) + int(frame["h"]) / 2)
- dist = abs(scx - cx) + abs(scy - cy)
- if best is None or dist < bestDist:
- best = name
- bestDist = dist
- print("Best for (%d,%d) is %s with dist %d" % (cx,cy,best,bestDist))
- if best in usedNames:
- raise RuntimeError("Best sprite is %s for multiple cases" % best)
- usedNames.add(best)
- sprite["name"] = best
- newsprites.append(copy.deepcopy(sprite))
- if len(usedNames) != len(jsondat["frames"]):
- raise RuntimeError("Didn't use all sprites!")
- yamldat["TextureImporter"]["spriteSheet"]["sprites"] = newsprites
- with open(yamlname, "w") as fh:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement