Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Aseprite script to export all frames of every layer as individual sprites.
--[[
Description:
A script to save all different layers in different .png files.
Made by Gaspi.
- Itch.io: https://gaspi.itch.io/
- Twitter: @PK_Gaspi
--]]
-- Auxiliar functions.
function getPath(str,sep)
-- Source: https://stackoverflow.com/questions/9102126/lua-return-directory-path-from-path
sep=sep or'/'
return str:match("(.*"..sep..")")
end
function getFileName(str,sep)
--[[ Sources:
- https://codereview.stackexchange.com/questions/90177/get-file-name-with-extension-and-get-only-extension
- https://stackoverflow.com/questions/18884396/extracting-filename-only-with-pattern-matching
--]]
str = str:match("^.+"..sep.."(.+)$")
return str:match("(.+)%..+")
end
-- Identify current sprite.
local sprite = app.activeSprite
if (sprite == nil) then
-- Show error, no sprite active.
local dlg = Dialog("Error")
dlg:label{ id = 0,
text = "No sprite is currently active. Please, open a sprite first and run the script with it active."
}
dlg:newrow()
dlg:button{ id = 1,
text = "Close",
onclick = function()
dlg:close()
end
}
dlg:show()
return
end
-- Path where sprites are saved.
local spritePath = sprite.filename
-- Identify operative system.
local separator
if (string.sub(spritePath, 1, 1) == "/") then
separator = "/"
else
separator = "\\"
end
local spriteName = getFileName(spritePath, separator)
local path = getPath(spritePath, separator) .. spriteName .. separator
-- Add a '_' to the filename if it has multiple frames
local multipleFrames = ""
if (#sprite.frames > 1) then
multipleFrames = "_"
end
-- Make destination directory.
os.execute("mkdir -p " .. path)
-- Hide all layers.
layerVisible = {}
for i,layer in ipairs(sprite.layers) do
layerVisible[i] = layer.isVisible
-- Avoid group layers.
if (not layer.isGroup) then
layer.isVisible = false
end
end
-- Save the layers as individual sprites.
for i,layer in ipairs(sprite.layers) do
-- Avoid group layers.
if (not layer.isGroup) then
layer.isVisible = true
local layerName = "_" .. string.lower(layer.name)
sprite:saveCopyAs(path .. spriteName .. layerName .. multipleFrames .. ".png")
layer.isVisible = false
end
end
-- Restore layers visibility.
for i,layer in ipairs(sprite.layers) do
-- Avoid group layers.
if (not layer.isGroup) then
layer.isVisible = layerVisible[i]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.