Organizing and managing layers in Aseprite sprites
Layers are the fundamental building blocks for organizing visual content in Aseprite. They allow you to separate different elements of your artwork, making it easier to edit and animate complex scenes.
A layer is a transparent sheet that can contain images (cels) at different frames. Layers are stacked on top of each other, with content from upper layers appearing in front of lower layers.
Layers are organized in a hierarchy - you can have layer groups that contain other layers, creating a tree-like structure.
Containers that organize other layers into a hierarchy.
local sprite = Sprite(32, 32)local group = sprite:newGroup() -- Creates a layer groupprint(group:isGroup()) -- true-- Add layers to the grouplocal childLayer = sprite:newLayer()childLayer.parent = group
A special locked layer that represents the opaque background:
local layer = sprite.layers[1]-- Check if backgroundif layer:isBackground() then print("This is a background layer")end-- Convert to background layerlayer:setBackground(true)print(layer:isMovable()) -- false (background layers can't move)
Background layers are always at the bottom of the layer stack and cannot be moved or deleted easily.
local sprite = Sprite(32, 32)-- Create a grouplocal group = sprite:newGroup()group.name = "Characters"-- Add layers to grouplocal layer1 = sprite:newLayer()layer1.parent = group-- Access group's layersfor i, layer in ipairs(group.layers) do print(layer.name)end-- Count layers in groupprint(#group.layers)
Layers contain cels (cells) - the actual image data at specific frames.
local sprite = Sprite(32, 32)local layer = sprite.layers[1]-- Get cel at specific framelocal cel = layer:cel(1) -- Frame 1if cel then print(cel.image.width) -- Access image data print(cel.position) -- Cel positionend-- Create new celsprite:newCel(layer, 2) -- Create cel at frame 2-- Get all cels from layerlocal cels = {}layer:getCels(cels)print(#cels) -- Number of cels in layer
A layer can have at most one cel per frame. Cels can be linked, meaning multiple frames share the same image data.
A layer’s effective visibility depends on its entire parent chain:
local layer = sprite.layers[1]-- Direct visibilityprint(layer.isVisible) -- Layer's own visibility flag-- Effective visibility (includes parent visibility)print(layer:isVisibleHierarchy()) -- true only if layer AND all parents are visible-- Editable in hierarchyprint(layer:isEditableHierarchy()) -- true only if layer AND all parents are editable-- Can edit pixelsprint(layer:canEditPixels()) -- Combines visibility, editability, and layer type
-- Layers are stacked with the first layer at the bottomlocal layer1 = sprite.layers[1] -- Bottomlocal layer2 = sprite.layers[2] -- Above layer1-- Move layers by changing parent relationships-- or use the app commands for reordering
local layer = sprite.layers[1]-- Store custom datalayer.data = "Character layer data"print(layer.data) -- "Character layer data"-- Data persists when savingsprite:saveAs("myfile.aseprite")
-- Get all layers (flattened hierarchy)local allLayers = sprite:allLayers()for i, layer in ipairs(allLayers) do print(i, layer.name)end-- Only visible layerslocal visibleLayers = sprite:allVisibleLayers()
function findLayerByName(sprite, name) for i, layer in ipairs(sprite:allLayers()) do if layer.name == name then return layer end end return nilendlocal layer = findLayerByName(sprite, "Character")
-- Duplicate a sprite to copy its layer structurelocal original = Sprite(32, 32)original:newLayer()original:newGroup()local copy = Sprite(original) -- Copies all layers