SHARE
TWEET

Gimp Spritesheet Script V2 (2020)

a guest Apr 17th, 2020 211 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # License: Public Domain - https://creativecommons.org/share-your-work/public-domain/cc0/
  3. # This is a script for Gimp 2.10 to generate spritesheets, originally from Karn Bianco.
  4. # https://github.com/Spydarlee/scripts/blob/master/GIMP/create_spritesheet.py
  5. # This functionality should be natively inside Gimp, because it automates the process of generating
  6. # Spritesheets where many artists needed. I myself use this script A LOT, it's a lifesaver.
  7. #########################################################################################################
  8. # ADDED: Custom input for rows and columns (2020) - In the case of a preference for a specific use case.#
  9. # for example, if you have 8 sprites, and want to have 4 rows with 2 colms, you can specify in the input#
  10. # box and also check the bool box to use custom values.                                                 #
  11. #########################################################################################################
  12. # NOTE: Do not use odd numbers, only pairs, in sprite resolution. Otherwise weird shift will occur. #####
  13. #########################################################################################################
  14. from gimpfu import *
  15. import math
  16.  
  17. def create_spritesheet(image, singleRow, customNums, customRows, customCol):
  18.  
  19.     # Grab all the layers from the original image, each one of which will become an animation frame
  20.     layers = image.layers
  21.     numLayers = len(layers)
  22.  
  23.     #Use customs if allowed
  24.     if customNums:
  25.         numCols = customCol
  26.         numRows = customRows
  27.     else:
  28.         # Work out how many rows and columns we need for each of our layers/animation frames
  29.         numCols = numLayers if singleRow else int(math.floor(math.sqrt(numLayers)))
  30.         numRows = 1 if singleRow else int(math.ceil(float(numLayers) / float(numCols)))
  31.    
  32.     # Maybe we could input these values by hand, then we were able to control say, a multiple sprite
  33.     #break, if needed. This will automate a splitt if needed, for let's say, 4 frames * 8 directions.
  34.     #Would be 4 rows with 8 columns, right?
  35.    
  36.     #if singleRow:
  37.     #    numRows = 1
  38.     #else:
  39.     #     numRows = int(math.ceil(float(numLayers) / float(numCols)))
  40.    
  41.     # And then determine the size of our new image based on the number of rows and columns
  42.     newImgWidth = image.width * numCols
  43.     newImgHeight = image.height * numRows
  44.  
  45.     # Create a new image and a single layer that fills the entire canvas
  46.     newImage = gimp.Image(newImgWidth, newImgHeight, RGB)
  47.     newLayer = gimp.Layer(newImage, "Spritesheet", newImgWidth, newImgHeight, RGBA_IMAGE, 100, NORMAL_MODE)
  48.     newImage.add_layer(newLayer, 1)
  49.  
  50.     # Clear any selections on the original image to esure we copy each layer in its entirety
  51.     pdb.gimp_selection_none(image)
  52.  
  53.     # Layers are in the reverse order we want them so start at the end of the list and work backwards
  54.     layerIndex = (numLayers - 1)
  55.  
  56.     # Loop over our spritesheet grid filling each one row at a time
  57.     for y in xrange(0, numRows):
  58.         for x in xrange(0, numCols):
  59.  
  60.             # Copy the layer's contents and paste it into a "floating" layer in the new image
  61.             pdb.gimp_edit_copy(layers[layerIndex])
  62.             floatingLayer = pdb.gimp_edit_paste(newLayer, TRUE)
  63.  
  64.             # This floating layer will default to the center of the new image so we first shift to the top left
  65.             # corner (0, 0) and and then shift to correct grid position based on current row and column index
  66.             xOffset = (-newImgWidth/2) + (image.width/2) + (x * image.width)
  67.             yOffset = (-newImgHeight/2) + (image.height/2) + (y * image.height)
  68.  
  69.             # GIMP will only copy non-transparent pixels, so if our image contains transparency
  70.             # the new floating layer may be smaller than we want which will cause animation issues.
  71.             # To resolve this we adjust our position by the difference in layer size to ensure everything aligns
  72.             xOffset += (image.width - floatingLayer.width) / 2
  73.             yOffset += (image.height - floatingLayer.height) / 2
  74.            
  75.             #yOffset += 1#BUG FIX: the sprite was always eating the first pixel up. Maybe it's Y FOR loop the issue. Don't know why.
  76.             #Maybe it's because of a version update some base code now considers Y as 1, well, this fixes it.
  77.             #AHA it's because of odd numbers in the sprite size.
  78.             #Always use pair numbers in the sprite size. That's it.
  79.            
  80.             # Move the floating layer into the correct position
  81.             pdb.gimp_layer_translate(floatingLayer, xOffset, yOffset)
  82.  
  83.             # Move to the next layer, unless we're all done in which case exit!
  84.             layerIndex = (layerIndex - 1)
  85.             if layerIndex < 0:
  86.                 break;
  87.  
  88.     # Merge the last floating layer into our final 'Spritesheet' layer
  89.     pdb.gimp_image_merge_visible_layers(newImage, 0)
  90.  
  91.     # Create and show a new image window for our spritesheet
  92.     gimp.Display(newImage)
  93.     gimp.displays_flush()
  94.  
  95. # Register the plugin with Gimp so it appears in the filters menu
  96. register(
  97.     "python_fu_create_spritesheet",
  98.     "Creates a spritesheet (in a new image) from the layers of the current image.",
  99.     "Creates a spritesheet (in a new image) from the layers of the current image.",
  100.     "Karn Bianco",
  101.     "Karn Bianco",
  102.     "2018",
  103.     "Create Spritesheet",
  104.     "*",
  105.     [
  106.         (PF_IMAGE, 'image', 'Input image:', None),
  107.         (PF_BOOL, "singleRow", "Output to a single row?", FALSE),
  108.         (PF_BOOL, "customNums", "Use Custom Rows and Columns?", FALSE),
  109.         (PF_INT, "customRows", "Custom Rows (Vertical)", 2),
  110.         (PF_INT, "customCol", "Custom Columns (Horizontal)", 2)
  111.     ],
  112.     [],
  113.     create_spritesheet, menu="<Image>/Filters/Animation/")
  114.  
  115. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top