Script: sprite.layers

Hi @Olga_Galvanova, this is a common problem when we have to remove elements from a collection that is being iterated. In this case the problem is that we cannot go through the sprite.layers collection and modify it at the same time.

My recommendation would be to create a table of layers first, and then iterate that table:

local sprite = app.activeSprite
local layers = {}
for i, layer in ipairs(sprite.layers) do
  table.insert(layers, layer)
end
for i, layer in ipairs(layers) do
  if not layer.isVisible then
    app.activeLayer = layer
    app.command.RemoveLayer()
  end
end
1 Like