Advertisement
AbsoluteGamer

TextureEditor (Splitter/Merger)

Mar 14th, 2017
4,754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.53 KB | None | 0 0
  1. # (c) Absolute Gamer 2017
  2. # YouTube: https://www.youtube.com/c/AbsoluteGamer
  3. # Discord: Absolute Gamer#0068
  4. # Email: xxwatergunxx@gmail.com
  5. # Site: http://abs0lute.altervista.org/
  6.  
  7. from PIL import Image
  8. import time,os,os.path
  9.  
  10. keys = []
  11. data = ''
  12.  
  13. # functions
  14.  
  15. def GetArray(key):
  16.     'Get plist array from key'
  17.     global data # grabbing sprite file data
  18.     try:
  19.         array = data.split('<key>'+key+'</key>')[1].split('<dict>')[1].split('</dict')[0].replace('    ','') # getting array from spitename
  20.     except IndexError: # catching IndexError if raised
  21.         return None # exiting function
  22.     return array # returning array
  23.  
  24. def GetSizeFromArray(array):
  25.     'Get sprite size from plist array'
  26.     size = array.split('<key>spriteSize</key>')[1].split('<string>')[1].split('</string')[0].replace('{','').replace('}','') # getting spriteSize from sprite array
  27.     x,y = int(size.split(',')[0]),int(size.split(',')[1]) # splitting data and getting x&y
  28.     return x,y # returning spriteSize
  29.  
  30. def GetPosFromArray(array):
  31.     'Get sprite pos from plist array'
  32.     size = array.split('<key>textureRect</key>')[1].split('<string>')[1].split('</string')[0].replace('{','').replace('}','') # getting sprite position from sprite array
  33.     x,y = int(size.split(',')[0]),int(size.split(',')[1]) # splitting data and getting x&y
  34.     return x,y # returning spriteSize
  35.  
  36. def IsSpriteRotated(array):
  37.     'Check if sprite is rotated on gamesheet'
  38.     string = array.split('<key>textureRotated</key>')[1] # getting bool from file
  39.     if ('false' in string): # checking if false
  40.         return False
  41.     return True
  42.  
  43. def GetGamesheetSize():
  44.     'Get size of Gamesheet'
  45.     strsize = GetArray('metadata').split('<key>size</key>\n<string>{')[1].split('}')[0].split(',')
  46.     return int(strsize[0]),int(strsize[1])
  47.  
  48. def SaveSprite(key):
  49.     global f
  50.     'Created image file form sprite in gamesheet'
  51.     array = GetArray(key) # getting data of the sprite
  52.     size = GetSizeFromArray(array) # getting sprite size
  53.     position = GetPosFromArray(array) # getting sprite position
  54.     rotated = IsSpriteRotated(array) # getting sprite rotation
  55.     gamesheet = Image.open(f+'.png','r') # opening gamesheet
  56.     if (rotated):
  57.         size = size[::-1]
  58.     img = Image.new('RGBA',size) # creating new image with size of target sprite
  59.     for x in range(size[0]):
  60.         for y in range(size[1]):
  61.             img.putpixel((x,y),
  62.                          gamesheet.getpixel((x+position[0],y+position[1])))
  63.             # creating image of the target sprite, pixel by pixel
  64.     img.save(f+'\\'+key) # saving file :D
  65.     img.close() # closing file
  66.     gamesheet.close() # closing gamesheet
  67.     return None
  68.  
  69. # main code:
  70.  
  71. print('Texture Editor by Absolute Gamer (2017)')
  72. print('Make sure to copy the gamesheets, both png & plist into the directory of the program!')
  73.  
  74. loaded = False
  75.  
  76. while 1:
  77.     print('\nOptions:')
  78.     print('1) Load texture')
  79.     if (loaded):
  80.         print('2) Split texture')
  81.         print('3) Merge texture')
  82.     ui = input('Enter valid number: ')
  83.  
  84.     if (ui == '1'): # load textures
  85.         loaded = True
  86.         f = input('Texture name (without the extention): ') # get texture name
  87.  
  88.         if not('-hd' in f) and not('-uhd' in f):
  89.             print('NOTE: You have chosen a low quality file') # warn user if non-hd file used
  90.  
  91.         print('Getting keys...')
  92.         fr = open(f+'.plist','r') # opening file with read permissions
  93.         data = fr.read() # reading file
  94.         fr.close() # closing file
  95.  
  96.         for i in data.split('<key>')[1:]:
  97.             if not(i.split('</key>')[0] in ['aliases','spriteOffset','spriteSize','spriteSourceSize','textureRect','textureRotated','frames']):
  98.                 if (i.split('</key>')[0] == 'metadata'):
  99.                     break # break loop if key = 'metadata'
  100.                 keys.append(i.split('</key>')[0]) # append key to array if not blacklisted
  101.         print('Done!')
  102.  
  103.         try:
  104.             if not os.path.isdir('merged'):
  105.                 os.makedirs('merged') # create folder if not exist
  106.                 print('Merged folder created')
  107.             if not os.path.isdir(f):
  108.                 os.makedirs(os.makedirs(f)) # create folder if not exist
  109.                 print('Gamesheet folder created')
  110.         except:
  111.             pass
  112.     if (ui == '2'): # split texture
  113.         print('Saving images...')
  114.         for i in keys:
  115.             SaveSprite(i) # for all keys, save sprite
  116.             print('Completed: '+i)
  117.         print('Rotating images...')
  118.         for i in keys: # rotate images needed
  119.             if IsSpriteRotated(GetArray(i)):
  120.                 img = Image.open(f+'\\'+i) # opening target image
  121.                 img.rotate(90, expand=True).save(f+'\\'+i) # rotating image
  122.                 img.close() # closing image
  123.                 print('Rotated: '+i)
  124.     if (ui == '3'):
  125.         print('Rotating images...')
  126.         for i in keys: # rotating images back if needed
  127.             if IsSpriteRotated(GetArray(i)):
  128.                 img = Image.open(f+'\\'+i) # opening target image
  129.                 img.rotate(-90, expand=True).save(f+'\\'+i) # rotating image by -90
  130.                 img.close() # closing image
  131.                 print('Rotated: '+i)
  132.  
  133.         new = Image.new('RGBA',GetGamesheetSize())
  134.  
  135.         for key in keys: # for all textures
  136.             targ = GetArray(key) # getting plist array from plist file
  137.             pos = GetPosFromArray(targ) # getting position of key on gamesheet
  138.             size = GetSizeFromArray(targ) # getting size of key on gamesheet
  139.             print('Completed: '+key)
  140.             rd = Image.open(f+'\\'+key) # opening image (made a stupid mistake earlier by opening the image per pixel, oops)
  141.             if (IsSpriteRotated(targ)): # if rotated, spaw x and y in the size variable
  142.                 size = size[::-1] # reverse tuple
  143.             for x in range(size[0]):
  144.                 for y in range(size[1]):
  145.                     new.putpixel((x+pos[0],y+pos[1]),rd.getpixel((x,y))) # add target image, pixel by pixel, to gamesheet by size and position
  146.             rd.close() # close target image
  147.         new.save('merged\\'+f+'.png') # save gamesheet in 'merged' folder
  148.         new.close() # close gamesheet
  149.  
  150.         print('Rotating images...')
  151.         for i in keys:
  152.             if IsSpriteRotated(GetArray(i)):
  153.                 img = Image.open(f+'\\'+i) # opening target image
  154.                 img.rotate(90, expand=True).save(f+'\\'+i) # rotating image by 90
  155.                 img.close() # closing image
  156.                 print('Rotated: '+i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement