Skip to content

Instantly share code, notes, and snippets.

@harpwood
Last active July 7, 2023 09:58
Show Gist options
  • Save harpwood/4f21293f497fd103b44734d8501085ab to your computer and use it in GitHub Desktop.
Save harpwood/4f21293f497fd103b44734d8501085ab to your computer and use it in GitHub Desktop.
Aseprite Animation Code Exporter for HaxeFlixel
--[[
Script: 🙏Can you help me find a catchy name for this script❓
Description: Aseprite Animation Code Exporter for HaxeFlixel
Author: Harpwood Studio
Website: https://harpwood.itch.io/
This script generates animation code for Aseprite sprites and allows users to save it to a custom file.
Usage:
1. Run the script in Aseprite.
2. It iterates over each tag in the timeline, extracting tag name, frame range, and frame rate.
3. Generates animation code for each tag, adjusting frame numbers to start from 0.
4. Prompts the user to choose a file path to save the animation code.
5. The animation code is saved to the specified file with a .txt extension.
6. Displays a success message when the code is saved.
If this script is useful to you, you can buy me a coffee:
https://www.buymeacoffee.com/harpwood/aseprite-animation-code-exporter-haxeflixel
!!! Please take a moment to review the license included at the end of this script. !!!
]]
-- Get the current sprite
local sprite = app.activeSprite
if not sprite then
return app.alert("No active sprite found. Please open a sprite and run the script again.")
end
-- Prompt the user to choose a file path and frame rate
local function showDialog()
local dlg = Dialog()
dlg:file{ id = "path", label = "Save animation code to:", save = true, filetypes = {"txt"} }
dlg:number{ id = "frameRate", label = "Frame Rate:", decimals = 0, text = "7" }
dlg:button{ id = "save", text = "Save", focus = true }
dlg:button{ id = "cancel", text = "Cancel" }
dlg:show()
return dlg.data
end
local data = showDialog()
-- Loop to continue prompting the user until a valid file name is provided or the 'cancel' button is pressed
while true do
if data.save then
local path = data.path
local frameRate = tonumber(data.frameRate)
-- Check if the file name is empty
if path == "" then
app.alert("Please provide a valid file name.")
data = showDialog() -- Show the dialog again
else
-- Append .txt extension if it's missing
if not path:match("%.txt$") then
path = path .. ".txt"
end
-- Initialize the generated code string
local code = ""
-- Iterate over each tag in the timeline
for i, tag in ipairs(sprite.tags) do
local tagName = tag.name
local fromFrame = tag.fromFrame.frameNumber - 1
local toFrame = tag.toFrame.frameNumber - 1
-- Generate the animation code for the tag with adjusted frame numbers and frame rate
local frameList = {}
for frame = fromFrame, toFrame do
table.insert(frameList, frame)
end
local animationCode = 'animation.add("' .. tagName .. '", [' .. table.concat(frameList, ', ') .. '], ' .. frameRate .. ');'
-- Append the animation code to the generated code string
code = code .. animationCode .. "\n"
end
-- Save the generated code to the custom file path
local file = io.open(path, "w")
file:write(code)
file:close()
-- Display a success message
app.alert("Animation code has been saved to " .. path .. " with frame rate " .. frameRate)
break -- Exit the loop after successful file save
end
else
break -- Exit the loop if the user cancels the dialog
end
end
--[[
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment