Convert a regular layer to a reference layer

Below is a quick demo script for how to turn a regular layer into a reference layer.

local sprite = app.site.sprite
if not sprite then return end
local srcLayer = app.site.layer
if not srcLayer then return end

if srcLayer.isReference then
    app.alert {
        title = "Error",
        text = "Layer is already a reference."
    }
    return
end

if srcLayer.isGroup then
    app.alert {
        title = "Error",
        text = "Group layers are not supported."
    }
    return
end

if srcLayer.isTilemap then
    app.alert {
        title = "Error",
        text = "Tilemap layers are not supported."
    }
    return
end

app.transaction("Convert to Reference", function()
    app.command.NewLayer {
        name = "Reference",
        reference = true,
    }
    local refLayer = app.layer
    if refLayer and refLayer.isReference then
        local frObjs = sprite.frames
        local lenFrObjs = #frObjs
        local i = 0
        while i < lenFrObjs do
            i = i + 1
            local frObj = frObjs[i]
            local srcCel = srcLayer:cel(frObj)
            if srcCel then
                local srcImg = srcCel.image
                local refCel = sprite:newCel(
                    refLayer, frObj, srcImg,
                    srcCel.position)
                refCel.color = Color(srcCel.color)
                refCel.data = srcCel.data
                refCel.opacity = srcCel.opacity
                refCel.zIndex = srcCel.zIndex
            end
        end

        refLayer.name = "Reference"
        refLayer.blendMode = srcLayer.blendMode
        refLayer.color = Color(srcLayer.color)
        refLayer.data = srcLayer.data
        refLayer.isContinuous = srcLayer.isContinuous
        refLayer.isEditable = srcLayer.isEditable
        refLayer.isVisible = srcLayer.isVisible
        refLayer.opacity = srcLayer.opacity
        refLayer.parent = srcLayer.parent
        refLayer.stackIndex = srcLayer.stackIndex

        app.layer = refLayer
        sprite:deleteLayer(srcLayer)
    end
end)

app.refresh()

Reference layer cel images will be the same size as they were in the original layer; they will not be expanded to fill the canvas. The scripting API does not have access to reference image scaling afaik.

Group and tile map layers are not supported. Cel and layer custom properties are not duplicated. Linked cels are not preserved.

A longer term version is on Github. It depends on other scripts in the repo.

To do the opposite – turn a reference layer into a regular layer – see this thread:

1 Like