texture_packer.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import tempfile
  2. import os
  3. import subprocess
  4. import glob
  5. import json
  6. ERROR_OK=0
  7. ERROR_INVALID_IN_PATH=1
  8. ERROR_NO_SRC_IMAGES=2
  9. ERROR_TEXTURE_PACKER_NOT_FOUND=3
  10. ERROR_TEXTURE_PACKER_FAILED=4
  11. def load_filelist(path):
  12. try:
  13. with open(path,'r') as f:
  14. s=f.read()
  15. rv=[line.strip() for line in s.split('\n')]
  16. return ERROR_OK,rv
  17. except:
  18. return ERROR_INVALID_IN_PATH,None
  19. def find_texture_packer():
  20. return ERROR_OK,'C:\Program Files\CodeAndWeb\TexturePacker\\bin\TexturePacker.exe'
  21. # return ERROR_TEXTURE_PACKER_NOT_FOUND,None
  22. class Image:
  23. def to_line(self):
  24. return '{name};{atlas};{aw};{ah};{ox};{oy};{ow};{oh};{fw};{fh};{x0};{y0};{x1};{y1};{x2};{y2};{x3};{y3};{rotated:d}'.format(**self.__dict__)
  25. def process_json(info):
  26. images=[]
  27. atlas_name=info['meta']['image']
  28. atlas_w=info['meta']['size']['w']
  29. atlas_h=info['meta']['size']['h']
  30. for frame in info['frames']:
  31. x=frame['frame']['x']
  32. y=frame['frame']['y']
  33. w=frame['frame']['w']
  34. h=frame['frame']['h']
  35. image=Image()
  36. image.name=frame['filename']
  37. image.atlas=atlas_name
  38. image.aw=atlas_w
  39. image.ah=atlas_h
  40. image.ox=frame['spriteSourceSize']['x']
  41. image.oy=frame['spriteSourceSize']['y']
  42. image.ow=w
  43. image.oh=h
  44. image.fw=frame['sourceSize']['w']
  45. image.fh=frame['sourceSize']['h']
  46. image.rotated=frame['rotated']
  47. if image.rotated:
  48. image.x0=x+h
  49. image.y0=y
  50. image.x1=x+h
  51. image.y1=y+w
  52. image.x2=x
  53. image.y2=y+w
  54. image.x3=x
  55. image.y3=y
  56. else:
  57. image.x0=x
  58. image.y0=y
  59. image.x1=x+w
  60. image.y1=y
  61. image.x2=x+w
  62. image.y2=y+h
  63. image.x3=x
  64. image.y3=y+h
  65. images.append(image)
  66. d=[]
  67. for image in images:
  68. l=image.to_line()
  69. d.append(l)
  70. return d
  71. def process(in_path,out_path,texture_packer_path=None,log_path=None,premultiply=False):
  72. error,src_images=load_filelist(in_path) if isinstance(in_path,str) else (ERROR_OK,in_path)
  73. if error!=ERROR_OK:
  74. return error,None
  75. if len(src_images)==0:
  76. return ERROR_NO_SRC_IMAGES,None
  77. error,texture_packer_path=find_texture_packer() if texture_packer_path is None else (ERROR_OK,os.path.abspath(texture_packer_path))
  78. if error!=ERROR_OK:
  79. return error,None
  80. if out_path is None:
  81. out_path='.'
  82. out_path=os.path.abspath(out_path)
  83. log_path=os.path.abspath(os.devnull if log_path is None else log_path)
  84. temp_path=os.path.abspath(tempfile.gettempdir()+'/.libmovie/TexturePacker')
  85. data_path=os.path.abspath(temp_path+'/data')
  86. json_path=os.path.abspath(data_path+'/atlas_{n}.json')
  87. sheet_path=os.path.abspath(out_path+'/atlas_{n}.png')
  88. for fn in glob.glob(os.path.abspath(data_path+'/*.json')):
  89. os.remove(fn)
  90. cmd_line=[]
  91. cmd_line.extend([texture_packer_path])
  92. cmd_line.extend(['--multipack'])
  93. cmd_line.extend(['--enable-rotation'])
  94. cmd_line.extend(['--trim-mode','Trim'])
  95. cmd_line.extend(['--size-constraints','POT'])
  96. cmd_line.extend(['--data',json_path])
  97. cmd_line.extend(['--sheet',sheet_path])
  98. cmd_line.extend(['--format','json-array'])
  99. cmd_line.extend(['--texture-format','png'])
  100. if premultiply:
  101. cmd_line.extend(['--alpha-handling','PremultiplyAlpha'])
  102. cmd_line.extend(['--max-width','2048'])
  103. cmd_line.extend(['--max-height','2048'])
  104. cmd_line.extend(['--max-size','2048'])
  105. for src_image in src_images:
  106. src_image=src_image.strip()
  107. if src_image!='':
  108. cmd_line.extend([os.path.abspath(src_image)])
  109. with open(log_path,'w') as log_file:
  110. try:
  111. exit_code=subprocess.call(cmd_line,stdout=log_file,stderr=log_file,shell=True)
  112. except:
  113. return ERROR_TEXTURE_PACKER_NOT_FOUND,None
  114. if exit_code!=0:
  115. return ERROR_TEXTURE_PACKER_FAILED,exit_code
  116. data=[]
  117. for json_fn in glob.glob(os.path.abspath(data_path+'/*.json')):
  118. with open(json_fn,'r') as f:
  119. j = json.load(f)
  120. d = process_json(j)
  121. data.extend(d)
  122. return ERROR_OK, (len(data), data)
  123. #if __name__=='__main__':
  124. # print(process('test.txt','.','./result.txt','C:\Program Files\CodeAndWeb\TexturePacker\\bin\TexturePacker.exe','texture_packer.log',True))
  125. # print(process('test.txt','.','./result.txt','C:\Program Files\CodeasdsadasdAndWeb\TexturePacker\\bin\TexturePacker.exe','texture_packer.log',True))
  126. # print(process(['bishop-icon.png','test with spaces.png'],'.',None,None,None,False))