Created
November 15, 2021 14:14
-
-
Save IgnacioLuxP/906842a3f3a0dc33e4dee772cc3cf4fa to your computer and use it in GitHub Desktop.
Aseprite script to convert all open sprite sheets into sliced sprite sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------------------------------------------------------------------------- | |
--Bulk Import Sprite Sheets -- v0.1 | |
----------------------------------------------------------------------------------------------- | |
--[[Instructions: | |
1. Open your sprite sheets in Aseprite. | |
2. Close any files that you do not wish to slice/import as sprite sheets (this process will be applied to all open files!) | |
3. Open the script | |
4. Enter your desired settings, some are optional | |
5. Click Import | |
6. Wait until the process finishes | |
7. You're done! | |
--]] | |
----------------------------------------------------------------------------------------------- | |
-- Internal functions | |
----------------------------------------------------------------------------------------------- | |
--Receives a string, returns an int from the enum (idk if Lua supports actual enums but whatever) | |
--ref: https://github.com/aseprite/api/blob/main/api/spritesheettype.md#spritesheettype | |
local function GetType(str) | |
local _type = 0 | |
if str == "Horizontal Strip" then | |
_type = SpriteSheetType.HORIZONTAL | |
elseif str == "Vertical Strip" then | |
_type = SpriteSheetType.VERTICAL | |
elseif str == "By Rows" then | |
_type = SpriteSheetType.ROWS | |
elseif str == "By Columns" then | |
_type = SpriteSheetType.COLUMNS | |
end | |
return _type | |
end | |
--Receives a table with the dialog data (dlgData or dlg.data), returns a rectangle object with the entered dimensions | |
--ref: https://github.com/aseprite/api/blob/main/api/rectangle.md | |
local function GetRectangleObj(t) | |
return Rectangle(t.x, t.y, t.width, t.height) | |
end | |
--Receives a table with the dialog data (dlgData or dlg.data), returns a size object with some padding (equal to zero if the padding checkbox is disabled) | |
--ref: https://github.com/aseprite/api/blob/main/api/size.md#size | |
local function GetSize(t) | |
if t.padding == false then | |
return Size(0, 0) | |
else | |
return Size(t.hor, t.ver) | |
end | |
end | |
--[[Calls the ImportSpriteSheet app.command | |
@params: | |
- t = dialog's data table | |
- rect = Rectangle object, usually from GetRectangle | |
- size = Size object, equal to Size(0, 0) if the padding checkbox is disabled | |
--ref: https://github.com/aseprite/api/blob/main/api/command/ImportSpriteSheet.md#importspritesheet | |
--]] | |
local function DoImport(t, rect, size) | |
app.command.ImportSpriteSheet { | |
ui = false, | |
type = GetType(t.type), | |
frameBounds = rect, | |
padding = size, | |
partialTiles = t.partial | |
} | |
end | |
----------------------------------------------------------------------------------------------- | |
-- Dialog/UI | |
----------------------------------------------------------------------------------------------- | |
local sheetTypes = { "Horizontal Strip", "Vertical Strip", "By Rows", "By Columns" } | |
local dlg = Dialog("Import multiple sheets") | |
dlg | |
:combobox{ id = "type", label = "Type:", option = "By Rows", options = sheetTypes } | |
:separator() | |
:number{ id = "x", label = "X:", decimals = integer, } | |
:number{ id = "y", label = "Y:", decimals = integer, } | |
:newrow() | |
:number{ id = "width", label = "Width:", decimals = integer, } | |
:number{ id = "height", label = "Height:", decimals = integer, } | |
:check{ id = "padding", text = "Padding", selected = false, | |
onclick = function() | |
dlg:modify{id = "hor", visible = dlg.data.padding } | |
dlg:modify{id = "ver", visible = dlg.data.padding } | |
end } | |
:number{ id = "hor", label = "Horizontal:", decimals = integer, visible = dlg.data.padding } | |
:number{ id = "ver", label = "Vertical:", decimals = integer, visible = dlg.data.padding } | |
:check{ id = "partial", text = "Include partial tiles at bottom/right edges", selected = false } | |
:button{ id = "import", text = "&Import", | |
onclick = function() | |
local dlgData = dlg.data | |
local n = #app.sprites | |
if n == 0 then | |
return app.alert ("No open sprites were found! (Open only the files you want to slice/import)") | |
end | |
local rectObj = GetRectangleObj(dlgData) | |
if rectObj.isEmpty then | |
return app.alert ("The width and height fields cannot be equal to 0 nor can be empty") | |
end | |
local sizeObj = GetSize(dlgData) | |
--iterate through the open sprites. Since app.command relies on the open sprite, we set activeSprite with each sprite in the array, then run the command and move on | |
for i, sprite in ipairs(app.sprites) do | |
app.activeSprite = sprite | |
DoImport(dlgData, rectObj, sizeObj) | |
end | |
app.alert("Imported " .. n .. " sprite sheets") | |
dlg:close() | |
end } | |
:button{ id = "cancel", text = "&Cancel", onclick = function() dlg:close() end } | |
dlg:show{ wait = false } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment