Export Aseprite Sprites: Difference between revisions

From F256 Foenix
Jump to navigationJump to search
(The Aseprite export script.)
 
No edit summary
Line 1: Line 1:
 
====== '''This LUA script file needs to be placed in Aseprite's "scripts" folder.''' ======
 
  <nowiki>local sprite = app.sprite
  <nowiki>local sprite = app.sprite
   
   

Revision as of 16:16, 9 April 2024

This LUA script file needs to be placed in Aseprite's "scripts" folder.
local sprite = app.sprite
 
 -- Check constraints
 if sprite == nil then
   app.alert("No Sprite...")
   return
 end
 if sprite.colorMode ~= ColorMode.INDEXED then
   app.alert("Sprite needs to be indexed")
   return
 end
 
 local function getPaletteData(palette)
     local ncolors = #palette
     local res = string.format(";%i\n", ncolors)
 
     for i=0, ncolors-1 do
         local color = palette:getColor(i)
         res = res .. "  .byte   " .. string.format("$%x,$%x,$%x,$%x\n", color.blue, color.green, color.red, color.alpha)  
     end
 
     return res
 end
 
 local function getIndexData(img, x, y, w, h)
     local res = ""
     for y = 0,h-1 do
         res = res .. "  .byte   "
         for x = 0, w-1 do
             if x > 0 then
                 res = res ..","
             end
             px = img:getPixel(x, y)
             res = res .. string.format("$%x", px)
         end
         res = res .. "\n"
     end
 
 
    return res
 end
 
 local function exportFrame(frm,sFlag)
     if frm == nil then
         frm = 1
     end
 
     local img = Image(sprite.spec)
     img:drawSprite(sprite, frm)
 
     if frm == 1 or frm == nil or sFlag == 1 then
         io.write(";Palette Indexed Picture\n")
         io.write("CLUT:\n")
         io.write(";begin palette\n")
         io.write(getPaletteData(sprite.palettes[1]))
         io.write(";end palette\n\n")
     end
     io.write("sprite")
     io.write(frm)
     io.write(":\n")
     io.write(";begin Sprite\n")
     io.write(getIndexData(img, x, y, sprite.width, sprite.height))
     io.write(";end Sprite\n\n")
 end
 
 local dlg = Dialog()
 dlg:file{ id = "exportFile",
           label = "File",
           open = false,
           save = true,
         --filename= p .. fn .. "pip",
           filetypes = {"txt", "pip" }}
 dlg:check{ id="onlyCurrentFrame",
            text="Export only current frame",
            selected=false }
 dlg:modify{ title = "F256 Export Sprite" }
 dlg:button{ id="ok", text="OK" }
 dlg:button{ id="cancel", text="Cancel" }
 dlg:show()
 local data = dlg.data
 if data.ok then
     local f = io.open(data.exportFile, "w")
     io.output(f)
 
     if data.onlyCurrentFrame then
         exportFrame(app.frame.frameNumber,1)
     else
         for i = 1,#sprite.frames do
             exportFrame(i,0)
         end
     end
 
     io.close(f)
 end