Create New Continuous Layer

@behreandtjeremy That’s a great point, I also didn’t even think about groups!

I’d suggest a recursive implementation that doesn’t count the actual groups themselves as layers:

local function CountLayers(layers)
    local sum = 0

    for _, layer in ipairs(layers) do
        if layer.layers == nil then
            sum = sum + 1
        else
            sum = sum + CountLayers(layer.layers)
        end
    end

    return sum
end

-- The rest of the code...

newLayer.name = "Layer " .. CountLayers(sprite.layers)

So the result looks more like this:

image

2 Likes