Calling a layer via script by its name inside a for loop

hey… trying to make a script that goes through some layers and switches the visibility of a layer…

so i have made an array / table which contains layer names i want to go through and trying to use something like this to hide all layers defined in that array / table inside the for loop:

–layer names from aseprite
layerArray = {“layer1”, “layer2”, “layer3”}

for i=1, 3 do
sprite.layers.LAYERNAMEHERE.isVisible = false
end

at least straight layerArray[i] didnt work and tried few other options too and my head aches now :slight_smile: any help?

1 Like

welcome, iiiikoooo.
sprite.layer api/sprite.md at main · aseprite/api · GitHub is only array and
layer.name api/layer.md at main · aseprite/api · GitHub only serves to get or set the name and it returns a string.
so i don’t think sprite.layer.name.isVisible will work.

i guess what you want to do is loop through the stack of layers (the array stored in sprite.layers) and ask with if condition if layer.name == "whatevername". if it does meet the requirements, then you set visibility with layer.isVisible = false.

something like this:

layername = "name"
local sprite = app.activeSprite
for i, layer in ipairs(sprite.layers) do
  if layer.name == layername then 
     layer.isVisible = false
  end
end
2 Likes

hmmm, cool! thanks i start to evaluate!

i had kind of few layers named with some numeric identifier and wanted to go through those in numeric order inside FOR loop… like hide all, but not layer one etc… but yeah, thanks a lot!

… if you put exact layer name in it actually hides the layer when script is run… but just confused how to do it in loop… how to go through every layer in numerical order.

ah btw… i had that local sprite defined at the beginning but didnt mention it.
local sprite = app.activeSprite

You could do that with ipairs like in @Olga_Galvanova’s example. I suggest you read about it in Lua’s reference manual.

for i, layer in ipairs(sprite.layers) do
  --do something
end

The layers object (e.g., sprite.layers) is an ordered array as far as I know. The numeral order, or indices (the i), correspond to the stackIndex property, so for example, the fifth layer would be sprite.layers[5], or in that for loop, it would be i = 5 and layer.stackIndex == 5 would be true. In the UI it would be the 5th layer starting from the bottom. But the ipairs loop already takes care of that order.

To check if the layer has a specific number (like “Arms 433”) in it instead of checking against the exact name, you’d have to use one of Lua’s string matching functions, like string.find or string.match. Both return nil if there isn’t a match.

And keep in mind that if your sprite has layers nested inside groups, those sublayers are not accessible with the above sample for loop, they’re actually another layers object, nested inside that layer group, something like layer.layers (or layers[index].layers if outside the ipairs for loop), and you’d have to loop through it as well. If one of those sublayers are a group as well, then there would be a third nested layer object (layers[i].layers[j].layer(s)), etc. You could use recursion to do it.

2 Likes

LuxTK made a good points. it depends on what is your end goal. for a simple loop through list of names you can just wrap a for loop in another for loop which will use the length of your array :

local sprite = app.activeSprite

local layerArray = {"Layer 1", "Layer 2", "Layer 3"} 
local names = #layerArray --length of table

for j=1, names do
	for i, layer in ipairs(sprite.layers) do
		if layer.name == layerArray[j] then 
			layer.isVisible = false
		end
	end
end

note that this is not undoable. for that you’d need to implement a check, so if the layer is visible then switch it off and vice versa.

2 Likes

thanks everyone! got it sorted out and now more familiar with syntax! cool!

2 Likes