Skip to content

Instantly share code, notes, and snippets.

@kkukshtel
Created August 6, 2023 16:27
Show Gist options
  • Save kkukshtel/25153309f3a5e39cda5bcf798a99192a to your computer and use it in GitHub Desktop.
Save kkukshtel/25153309f3a5e39cda5bcf798a99192a to your computer and use it in GitHub Desktop.
toggles the visibility of layers in an aseprite file and exports the sprite with those layers visible only - can do multiple exports as part of a single script run as well
-- Obtain the active sprite
local spr = app.activeSprite
if not spr then
return app.alert("There is no active sprite!")
end
-- Extract the directory from the sprite's filename
local spritePath = spr.filename
local directory = spritePath:match("^(.*[/\\])")
if not directory or directory == "" then
return app.alert("Please save the Aseprite file to a directory before running this script.")
end
-- Dialog to choose a text file
local dlg = Dialog("Export Layer Groups from Text File")
dlg
:file{
id = "txtFile",
label = "Layer groups text file:",
open = true,
filetypes = { "txt" }
}
:button{ id="ok", text="OK" }
:button{ id="cancel", text="Cancel" }
dlg:show()
-- Function to toggle visibility of layers
local function toggleLayers(layerNames)
local originalVisibility = {}
for _, layer in ipairs(spr.layers) do
originalVisibility[layer] = layer.isVisible
layer.isVisible = false
for _, name in ipairs(layerNames) do
if layer.name == name then
layer.isVisible = true
break
end
end
end
return originalVisibility
end
-- Restore the original visibility of layers
local function restoreVisibility(originalVisibility)
for layer, visibility in pairs(originalVisibility) do
layer.isVisible = visibility
end
end
-- If OK was pressed
if dlg.data.ok then
local txtFilePath = dlg.data.txtFile
if not txtFilePath or #txtFilePath == 0 then
return app.alert("No text file selected!")
end
local currentFileName = ""
local layerNames = {}
for line in io.lines(txtFilePath) do
line = line:match("^%s*(.-)%s*$") -- trim spaces
if line == "" then
if currentFileName ~= "" and #layerNames > 0 then
local fullPath = directory .. currentFileName
local originalVisibility = toggleLayers(layerNames)
spr:saveCopyAs(fullPath)
restoreVisibility(originalVisibility)
end
currentFileName = ""
layerNames = {}
elseif currentFileName == "" then
currentFileName = line
else
table.insert(layerNames, line)
end
end
-- Handle the last group if it exists
if currentFileName ~= "" and #layerNames > 0 then
local fullPath = directory .. currentFileName
local originalVisibility = toggleLayers(layerNames)
spr:saveCopyAs(fullPath)
restoreVisibility(originalVisibility)
end
end

sprites are exported into the same folder as where the open aseprite file is at

myexport.png
layer 1
layer 2
anotherexport.png
layer 1
layer 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment