image_polygonize.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import tempfile
  2. import os
  3. import subprocess
  4. import glob
  5. import xml.etree.ElementTree as ET
  6. ERROR_OK=0
  7. ERROR_INVALID_IN_PATH=1
  8. ERROR_TEXTURE_PACKER_NOT_FOUND=3
  9. ERROR_TEXTURE_PACKER_FAILED=4
  10. def find_texture_packer():
  11. return ERROR_OK,'c:\Program Files\CodeAndWeb\TexturePacker\\bin\TexturePacker.exe'
  12. # return ERROR_TEXTURE_PACKER_NOT_FOUND,None
  13. def split1(f, text):
  14. return map(f, text.split(' '))
  15. pass
  16. def split2(f, text):
  17. vv = [v for v in map(f, text.split(' '))]
  18. return zip(vv[::2], vv[1::2])
  19. pass
  20. def getf(f, v, a, d):
  21. s=v.get(a, d)
  22. return d if s is None else f(s)
  23. def process_sprite_xml(sprite,offset_x,offset_y,width,height):
  24. x=getf(int,sprite,'x', 0)
  25. y=getf(int,sprite,'y', 0)
  26. w=getf(float,sprite,'w', 0)
  27. h=getf(float,sprite,'h', 0)
  28. ox=getf(int,sprite,'oX', x)
  29. oy=getf(int,sprite,'oY', y)
  30. ow=getf(float,sprite,'oW', w)
  31. oh=getf(float,sprite,'oH', h)
  32. if width<0.0:width=w
  33. if height<0.0:height=h
  34. positions=[]
  35. for vx,vy in split2(int, sprite.find('vertices').text):
  36. px=vx-x+offset_x
  37. py=vy-y+offset_y
  38. positions.extend((px,py))
  39. uvs=[]
  40. for uvx,uvy in split2(int, sprite.find('verticesUV').text):
  41. pu=uvx-x+ox
  42. pv=uvy-y+oy
  43. uvs.extend((float(pu)/float(width),float(pv)/float(height)))
  44. indices=[]
  45. for tri in split1(int, sprite.find('triangles').text):
  46. indices.append(tri)
  47. vertex_count=len(positions)//2
  48. index_count=len(indices)
  49. return [vertex_count,index_count,positions,uvs,indices]
  50. def process(in_path,temp_dir=None,texture_packer_path=None,log_path=None,offset_x=0,offset_y=0,width=-1.0,height=-1.0,tolerance=200):
  51. error,texture_packer_path=find_texture_packer() if texture_packer_path is None else (ERROR_OK,os.path.abspath(texture_packer_path))
  52. if error!=ERROR_OK:
  53. return error,None
  54. in_path=os.path.abspath(in_path)
  55. log_path=os.path.abspath(os.devnull if log_path is None else log_path)
  56. if temp_dir is None:
  57. temp_dir=tempfile.gettempdir()
  58. pass
  59. data_path=os.path.join(temp_dir, 'ae_image_polygonize_data.xml')
  60. cmd_line=[]
  61. cmd_line.extend([texture_packer_path])
  62. cmd_line.extend(['--shape-padding','0'])
  63. cmd_line.extend(['--border-padding','0'])
  64. cmd_line.extend(['--padding','0'])
  65. cmd_line.extend(['--disable-rotation'])
  66. cmd_line.extend(['--extrude','0'])
  67. cmd_line.extend(['--trim-mode','Polygon'])
  68. cmd_line.extend(['--trim-threshold','0'])
  69. cmd_line.extend(['--tracer-tolerance',str(tolerance)])
  70. cmd_line.extend(['--max-width','8192'])
  71. cmd_line.extend(['--max-height','8192'])
  72. cmd_line.extend(['--max-size','8192'])
  73. cmd_line.extend(['--data',data_path])
  74. # cmd_line.extend(['--format','json-array'])
  75. cmd_line.extend(['--format','xml'])
  76. cmd_line.extend([in_path])
  77. with open(log_path,'w') as log_file:
  78. try:
  79. exit_code=subprocess.call(cmd_line,stdout=log_file,stderr=log_file,shell=True)
  80. except:
  81. return ERROR_TEXTURE_PACKER_NOT_FOUND,None
  82. if exit_code!=0:
  83. return ERROR_TEXTURE_PACKER_FAILED,exit_code
  84. tree=ET.parse(data_path)
  85. root=tree.getroot()
  86. for sprite in root.findall('.//sprite'):
  87. info=process_sprite_xml(sprite,offset_x,offset_y,width,height)
  88. return ERROR_OK,info
  89. #if __name__=='__main__':
  90. # process('bishop-icon.png','test.txt',log_path='./test.log')
  91. # print(process('bishop-icon.png','test.txt',log_path='./test.log'))