Create a layer by script

As the title suggests, is it possible to create a new layer and add it to the stack by script?
The idea would be to have multiple “Templates” of layers for different kind of projects (body, mountains, city, clouds, etc…) you can select from a dialog box.

Thank you in advance!

Dax

hi, dax! it is possible. here’s a documentation: GitHub - aseprite/api: Scripting API for Aseprite
you can create new layer by using app.command.NewLayer().

1 Like

Thanks a lot! How did I miss that in the API…

1 Like

Also check sprite:newLayer() api/sprite.md at main · aseprite/api · GitHub

If you have a template .ASE file with your layers, you could copy the desired layer from that file (using templateSprite.layers[index] then do something like

--newSprite is the new file to receive the clone
local newLayer = newSprite:newLayer()
newLayer = templateSprite.layers[someIndex]

Which would clone the template layer’s name, settings, blending mode, opacity etc, and in theory, all its frames/cels as well. But keep in mind you’ll also need to define the receiving sprite’s canvas size (its bounds).

And lastly, be aware that if the template has an indexed color mode, it might copy a garbage image because the receiving end won’t have the same palette. If it’s just one frame/one cel, a useful way to circumvent that mess would be using image:drawImage() api/image.md at main · aseprite/api · GitHub (you’d probably have to hide all the other layers so it only copies the pixels from the one you choose)

If you want to delve further, this sample script and this script (for the hide/show layers part of the algorithm) would be a nice starting point, though there might be simpler methods.

1 Like

Thanks for the precision!
My idea was more to have some layers presets for each “kind of project”
For example I often use a simple Mountain-Clouds-Sky layers. The canvas size is predefined, but could also be an option to ship with the presets.
It all came when I realized I needed a way to automate the “New Layer->Ctrl-P->rename->New Layer->rename” to have a background layer.
I have a precise idea how to do it now!