api icon indicating copy to clipboard operation
api copied to clipboard

Sprite sheet export using API

Open sadovsf opened this issue 5 years ago • 6 comments

Essentially i would like to create exporter for my own engine with different file format. For that it would be really usefull if i would be able to work with sprite sheet.

My idea is something like this:

local ss = Sprite:createSpriteSheet(rows, columns) --< Maybe some description table with params you normally enter in export dialog instead of just row and column

--! SpriteSheet minimal api:
ss.width
ss.height
ss:SafeToFile("Myfile.png")
for i,sframe in ss.sheetFrames do
    --! minimal sheet frame API:
    sframe.posx
    sframe.posy
    sframe.width
    sframe.height
    sframe.spriteFrameNumber
end

This would be enough to be able to do custom exporter with all data necessary to recreate animation in external renderer (engine)

sadovsf avatar Mar 07 '19 10:03 sadovsf

I think i found one way of achieving this now. That consists of generating sprite sheet as a new sprite. In that case i have all coordinates under my control and can export them along with simple save of new sprite to png. Just think that it could be done much simpler by exposing some internals of sprite sheet export to API ;-)

sadovsf avatar Mar 07 '19 10:03 sadovsf

Hi againa @sadovsf, actually for v1.2.10 there are plans to expose the ExportSpriteSheet command like this which is similar of what you are looking for. It's not the same thing because the intermediate sprite sheet result is not exposed in the Lua scripting API (e.g. the JSON information).

I'll leave this issue open because is something to implement some time in the future.

dacap avatar Mar 16 '19 16:03 dacap

That would be actually enough probably if I could load lua library Abe to read json. That way I could use save feature and "translate" json format 🤔

Of course having full support for this would be best. Thank you for your time 😊

sadovsf avatar Mar 16 '19 17:03 sadovsf

Now that I think, tests are already using the json.lua library to check if the export sprite sheet JSON is the expected result:

https://github.com/aseprite/tests/blob/0dbe0bdffb0d4d9d8ea979b73b837f14e47b6b5e/cli/sheet.sh#L20

dacap avatar Mar 16 '19 19:03 dacap

Awesome so it should be possible 🤔😊

sadovsf avatar Mar 16 '19 19:03 sadovsf

I also need each (visible) layer x and y (top left) information like the question above.

I already transform the output in some way. And now I'm trying to solve this problem with your api.

But where do I get the layer x and y position, and can I do this in combination with this command ?

/Applications/Aseprite.app/Contents/MacOS/aseprite \
    --batch \
    --format json-array \
    --filename-format '{title}~{layer}' \
    --ignore-empty \
    --border-padding 2 \
    --shape-padding 2 \
    --sheet-type packed \
    --trim-sprite \
    --split-layers \
    --script transform_after_export.lua \
    --sheet ${image_file} \
    --data ${json_file} \
    ${ase_files} \

Inside ase_export.lua I can get the generated json file, but that don't have the layer x,y information.

I was trying to run app.command.ExportSpriteSheet {} in lua but I don't think that is the correct way, because I'm combine multiple ase files.

And dumping the default command returns the same arguments.

local t =
    app.command.ExportSpriteSheet(
    {
        ui = false,
        askOverwrite = true,
        type = SpriteSheetType.HORIZONTAL,
        columns = 0,
        rows = 0,
        width = 0,
        height = 0,
        bestFit = false,
        textureFilename = "",
        dataFilename = "",
        dataFormat = SpriteSheetDataFormat.JSON_HASH,
        borderPadding = 0,
        shapePadding = 0,
        innerPadding = 0,
        trim = false,
        extrude = false,
        openGenerated = false,
        layer = "",
        tag = "",
        splitLayers = false,
        listLayers = true,
        listTags = true,
        listSlices = true
    }
)

dump(t) -- get a table with arguments.

Another way around this is.

  • run the cli command
  • start the script --script (maybe with --script-parameter with the ase files that I combine
  • that script need to open the output json and re-open all the ase files and search for each layer to get the x,y

Is that the way to solve this ? Or is that to complex thinking ;)

EDIT

@dacap Interesting, i'm testing some code and don't get the layer x,y (cel.position) positions. I think that is the reason why it's not in the default export.

But I don't understand why. I did assume that each layer do have a x and y postion (top, left) in the whole 'scene'.

So the question is now, does each layer have a top, left position (that is the first found (non transparent pixel in a layer top, left). And can I read them using layers cel position ?

function getLayers(layers)
    for j, layer in ipairs(layers) do
        print(layer.name)

        local cel = layer:cel(1) -- framenumber
        if cel then
            print(cel.bounds, cel.position, cel.data, layer.data)
        end

        if layer.isGroup then
            print("is group")
            getLayers(layer.layers)
        end
    end
end

output is

Open: ../project/aseprite/production/scene/1/000_scene.ase
doc::Sprite: 0x7fbeb35bf978
Something (group)
is group
Grond
Rectangle{ x=0, y=0, width=2048, height=768 }	Point{ x=0, y=0 }		
lucht
Rectangle{ x=0, y=0, width=2048, height=768 }	Point{ x=0, y=0 }		
Bord
Rectangle{ x=0, y=0, width=1024, height=768 }	Point{ x=0, y=0 }

gcmartijn avatar Apr 05 '20 12:04 gcmartijn