Advertisement
Guest User

Untitled

a guest
Apr 6th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.55 KB | None | 0 0
  1. import copy
  2. import json
  3. import yaml
  4. import sys
  5.  
  6. yamlname = sys.argv[1]
  7. jsonname = sys.argv[2]
  8. print("Loading from %s/%s" % (yamlname, jsonname))
  9.  
  10. with open(yamlname) as fh:
  11.     yamldat = yaml.load(fh.read(), yaml.Loader)
  12. jsondat = json.loads(open(jsonname).read())
  13.  
  14. ymax = 0
  15. for sprite in jsondat["frames"].values():
  16.     ymax = max(ymax, sprite["frame"]["y"] + sprite["frame"]["h"])
  17. print("ymax is %d" % ymax)
  18.  
  19. newsprites = []
  20. usedNames = set()
  21. for sprite in yamldat["TextureImporter"]["spriteSheet"]["sprites"]:
  22.     rect = sprite["rect"]
  23.     cx = int(rect["x"]) + int(rect["width"]) / 2
  24.     cy = int(rect["y"]) + int(rect["height"]) / 2
  25.     best = None
  26.     bestDist = 0
  27.     for name, jsonsprite in jsondat["frames"].items():
  28.         frame = jsonsprite["frame"]
  29.         scx = int(frame["x"]) + int(frame["w"]) / 2
  30.         # Note Y axis is inverted, because of course it is.
  31.         scy = ymax - (int(frame["y"]) + int(frame["h"]) / 2)
  32.         dist = abs(scx - cx) + abs(scy - cy)
  33.         if best is None or dist < bestDist:
  34.             best = name
  35.             bestDist = dist
  36.     print("Best for (%d,%d) is %s with dist %d" % (cx,cy,best,bestDist))
  37.     if best in usedNames:
  38.         raise RuntimeError("Best sprite is %s for multiple cases" % best)
  39.     usedNames.add(best)
  40.     sprite["name"] = best
  41.     newsprites.append(copy.deepcopy(sprite))
  42. if len(usedNames) != len(jsondat["frames"]):
  43.     raise RuntimeError("Didn't use all sprites!")
  44. yamldat["TextureImporter"]["spriteSheet"]["sprites"] = newsprites
  45.  
  46. with open(yamlname, "w") as fh:
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement