Skip to content

Instantly share code, notes, and snippets.

@aqt
Created July 13, 2023 21:04
Show Gist options
  • Save aqt/e38bcaae9c0ab340334585b6cdfdb36d to your computer and use it in GitHub Desktop.
Save aqt/e38bcaae9c0ab340334585b6cdfdb36d to your computer and use it in GitHub Desktop.
Aseprite export tags to sprite sheets (WebP)
-- Adapted from https://github.com/aseprite/Aseprite-Script-Examples/blob/68ed42647ef9990d6a27c4244277f83d6de8a69b/Export%20Tags%20To%20Different%20Sprite%20Sheets.lua
-- It worked great except that I prefer WebP. I added some more opinionated
-- changes while at it.
--
-- Changes include:
-- 1. WebP instead of PNG - Smaller, still supporting lossless and transparency
-- 2. Removed data generation - I don't need it
-- 3. Removed superfluous overwrite confirmation - Aseprite already asks
-- 4. Underscore separating file name - To fit my naming convention
-- 5. Lowercase tag in filename - Compatibility between OSs
-- Create a new RGB sprite based on the indexed sprite
local function createRGBSpriteFromIndexed(indexedSprite)
-- Create a copy of the active sprite
local rgbSprite = Sprite(indexedSprite)
-- Set the color mode to RGB. Presumably none of the other settings matter in
-- this use case (going to RGB)
return pcall(function()
app.command.ChangePixelFormat {
format = "rgb",
dithering = "",
rgbmap = "default",
toGray = "luma"
}
return rgbSprite
end)
end
local function export(activeSprite)
local success, rgbSprite = createRGBSpriteFromIndexed(activeSprite)
if not success then
print(string.format("Error in ChangePixelFormat: '%s'", result))
return
end
local filename = activeSprite.filename:gsub("%.[^.]*$", "")
for i, tag in ipairs(rgbSprite.tags) do
local fn = filename .. '_' .. string.lower(tag.name) .. '.webp'
local success, result = pcall(function()
app.command.ExportSpriteSheet {
ui = false,
type = SpriteSheetType.HORIZONTAL,
textureFilename = fn,
tag = tag.name,
listLayers = false,
listTags = false,
listSlices = false,
}
end)
if not success then
print(string.format(
"Error in ExportSpriteSheet: '%s'. Tag name: '%s'",
result, tag.name))
break
end
end
rgbSprite:close()
end
if not app.activeSprite then return end
export(app.activeSprite)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment