Export Aseprite Sprites: Difference between revisions

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

Latest revision as of 16:19, 9 April 2024

This LUA script file needs to be placed in Aseprite's "scripts" folder.[edit | edit source]
-- export_sprite_256.lua

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