From b414301570b57211b17ce97e24f54e1e712ad7b2 Mon Sep 17 00:00:00 2001 From: Lukas Scheerer Date: Thu, 6 Aug 2020 20:06:17 +0200 Subject: [PATCH] Creates version 1.0 of cells export script. --- ...ells-in-single-files-ignore-duplicates.lua | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 export-cells-in-single-files-ignore-duplicates.lua diff --git a/export-cells-in-single-files-ignore-duplicates.lua b/export-cells-in-single-files-ignore-duplicates.lua new file mode 100644 index 0000000..a33df81 --- /dev/null +++ b/export-cells-in-single-files-ignore-duplicates.lua @@ -0,0 +1,106 @@ +--[[ +Version: 1.0 +Description: +Exports cells in single files ignoring duplicates (linked cells). +Scripted by https://community.aseprite.org/u/scovillo +--]] + +local function contains (table, value) + for _, item in ipairs(table) do + if item == value then + return true + end + end + return false +end + +local function count (table) + counter = 0 + for _, _ in ipairs(table) do + counter = counter + 1 + end + return counter +end + +function getPath(str, sep) + sep = sep or '/' + return str:match("(.*" .. sep .. ")") +end + +function getFileName(str, sep) + str = str:match("^.+" .. sep .. "(.+)$") + return str:match("(.+)%..+") +end + +local sprite = app.activeSprite +if (sprite == nil) then + local dialog = Dialog("Error") + dialog:label { + id = 0, + text = "Open a sprite before you run the script!" + } + dialog:newrow() + dialog:button { + id = 1, + text = "OK", + onclick = function() + dialog:close() + end + } + dialog:show() + return +end + +local path = sprite.filename +local separator +if (string.sub(path, 1, 1) == "/") then + separator = "/" +else + separator = "\\" +end +local outputPath = getPath(path, separator) .. "spriter" .. separator .. "frames" .. separator + +local function extractCellsFrom (layer, groupName) + layerImages = {} + if (count(sprite.tags) == 0) then + animationIndex = 0 + for k, _ in ipairs(sprite.frames) do + local cel = layer:cel(k) + if (not contains(layerImages, cel.image)) then + cel.image:saveAs(outputPath .. layer.name .. groupName .. "_" .. animationIndex .. ".png") + animationIndex = animationIndex + 1 + table.insert(layerImages, cel.image) + end + end + else + frameIndex = 0 + for _, tag in ipairs(sprite.tags) do + animationIndex = 0 + for _ = 1, tag.frames do + frameIndex = frameIndex + 1 + local cel = layer:cel(frameIndex) + if (not contains(layerImages, cel.image)) then + cel.image:saveAs(outputPath .. layer.name .. groupName .. "_" .. tag.name .. "_" .. animationIndex .. ".png") + animationIndex = animationIndex + 1 + table.insert(layerImages, cel.image) + end + end + end + end +end + +for _, layer in ipairs(sprite.layers) do + if (layer.isGroup) then + if (layer.isVisible) then + for _, subLayer in ipairs(layer.layers) do + if (subLayer.isVisible) then + extractCellsFrom(subLayer, "_" .. layer.name) + end + end + end + else + if (layer.isVisible) then + extractCellsFrom(layer, "") + end + end +end \ No newline at end of file