# How can I get a list of layers inside a group?

**URL:** https://community.aseprite.org/t/how-can-i-get-a-list-of-layers-inside-a-group/20437
**Category:** Scripts & Extensions
**Created:** [October 27, 2023, 3:46pm UTC](https://community.aseprite.org/t/how-can-i-get-a-list-of-layers-inside-a-group/20437 "2023-10-27T15:46:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![gamedevshrimp](https://community.aseprite.org/user_avatar/community.aseprite.org/gamedevshrimp/32/16358_2.png) [@gamedevshrimp](https://community.aseprite.org/u/gamedevshrimp)
#### Post date: [October 27, 2023, 3:46pm UTC](https://community.aseprite.org/t/how-can-i-get-a-list-of-layers-inside-a-group/20437/1 "2023-10-27T15:46:51Z")

</div>

Hello, dear community! I trying to learn Aseprite API. I want to iterate through all layers in my sprite.  
I found a great solution from [Olga\_Galvanova](https://community.aseprite.org/u/Olga_Galvanova)

> [@Calling a layer via script by its name inside a for loop](https://community.aseprite.org/t/calling-a-layer-via-script-by-its-name-inside-a-for-loop/11386/2):
>
> welcome, iiiikoooo. sprite.layer [api/sprite.md at main · aseprite/api · GitHub](https://github.com/aseprite/api/blob/main/api/sprite.md) is only array and layer.name [api/layer.md at main · aseprite/api · GitHub](https://github.com/aseprite/api/blob/main/api/layer.md#layer) 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…

However, I noticed this algorithm doesn’t go into a group layer. How I can fix it?  
I’ve tried to use `app.command.OpenGroup()` but doesn’t work for me

---

<div class="post-metadata">

### Author: ![Olga\_Galvanova](https://community.aseprite.org/user_avatar/community.aseprite.org/olga_galvanova/32/3294_2.png) [@Olga\_Galvanova](https://community.aseprite.org/u/Olga_Galvanova)
#### Post date: [October 27, 2023, 4:07pm UTC](https://community.aseprite.org/t/how-can-i-get-a-list-of-layers-inside-a-group/20437/2 "2023-10-27T16:07:32Z")

</div>

hi there, this is a recursive version of that script:

```auto
local vis = app.layer 
local visibility = vis.isVisible 
vis.isVisible = not visibility 

function goop(a)
	for _, layer in ipairs(a.layers) do 
		local visible = layer.isVisible
		if visible == visibility then 
			layer.isVisible = not visibility 
		end
		if layer.isGroup then 
			goop(layer) 
		end
	end
end 

if vis.isGroup then 
	goop(vis) 
end

```

what i do is checking `if layer.isGroup` and if it is, then i call the function from within itself again.

just one note though: if you plan to edit the stack of layers (for instance if you want to remove one or more of the layers), you need to put them into the table first and then iterate through that table. like in this example by dacap:

> [@Script: sprite.layers](https://community.aseprite.org/t/script-sprite-layers/7051/2):
>
> 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…

which reminds me i planned make recursive version of delete hidden layers… :E

---

<div class="post-metadata">

### Author: ![gamedevshrimp](https://community.aseprite.org/user_avatar/community.aseprite.org/gamedevshrimp/32/16358_2.png) [@gamedevshrimp](https://community.aseprite.org/u/gamedevshrimp)
#### Post date: [October 27, 2023, 5:41pm UTC](https://community.aseprite.org/t/how-can-i-get-a-list-of-layers-inside-a-group/20437/3 "2023-10-27T17:41:13Z")

</div>

Thank you! It’s literally that I want
