Aesprite脚本:分别导出每张可见图层实际像素部分

local spr = app.activeSprite
if not spr then
  return app.alert("请先打开一个 .aseprite 文件")
end

-- 获取导出路径
local outputPath = spr.filename:gsub("[^/\\]+$", "") .. "layers_exported/"
app.fs.makeDirectory(outputPath)

-- 递归处理所有图层,包括组内图层
local function exportVisibleImageLayers(layerList)
  for _, layer in ipairs(layerList) do
    if layer.isGroup then
      -- 递归组内图层
      exportVisibleImageLayers(layer.layers)
    elseif layer.isImage and layer.isVisible then
      local cel = layer:cel(1)
      if cel and cel.image then
        local image = Image(cel.image)
        local bounds = image.bounds
        if not image:isEmpty() then
          -- 创建临时 Sprite 用于导出
          local tempSprite = Sprite(image.width, image.height, spr.colorMode)
          tempSprite.filename = layer.name

          -- 绘制图层图像
          tempSprite.cels[1].image:drawImage(image, -bounds.x, -bounds.y)

          -- 裁切内容
          tempSprite:crop(tempSprite.bounds)

          -- 保存为 PNG
          local filename = outputPath .. layer.name .. ".png"
          tempSprite:saveCopyAs(filename)
          tempSprite:close()
        end
      end
    end
  end
end

-- 启动导出流程
exportVisibleImageLayers(spr.layers)

app.alert("✅ 所有可见图层已裁切并导出完毕")

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理