# Having issues with reading from multiple layers in a custom script

**URL:** <https://community.aseprite.org/t/having-issues-with-reading-from-multiple-layers-in-a-custom-script/29191>\
**Category:** Help\
**Created:** [November 26, 2026, 7:28pm UTC](https://community.aseprite.org/t/having-issues-with-reading-from-multiple-layers-in-a-custom-script/29191 "2026-11-26T19:28:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![57E](https://community.aseprite.org/letter_avatar/57e/32/6_449c533f586939b6e5f246632a46421f.png) [@57E](https://community.aseprite.org/u/57E)\
**Post date:** [November 26, 2026, 7:28pm UTC](https://community.aseprite.org/t/having-issues-with-reading-from-multiple-layers-in-a-custom-script/29191/1 "2026-11-26T19:28:17Z")

</div>

I have a sprite with three layers and I need to merge them into one single layer in a way that:

In the resulting new layer One layer is used the red channel, second is for green and the third is for blue.

So that colors #120000 + #003400 + #000056 would make the final pixel be #123456

This is needed for craming three eight bit heightmaps into one for use in a shader to save resources.

This can be done without a script, but I would like to simplify the process to make it less easy to mess up and overwrite one layer’s colors completely (I did that a lot and it made debuging the shader itself a pain).

Im not familar enough on LUA so this script is partly copied without understanding the excat purpose of parts of it. And Im definetly not at all familar with Aseprite’s functions.

So here’s what I made so far:

> if app.apiVersion \< 1 then  
> return app.alert(“This script requires Aseprite v1.2.10-beta3”)  
> end
> 
> local sprite = app.activeSprite  
> local frame = app.activeFrame
> 
> local currentLayer = app.activeLayer  
> local currentCel = currentLayer:cel(frame)  
> local img = currentCel.image
> 
> local l = sprite.layers[1]  
> local c = l:cel(frame)  
> local img\_R = c.image
> 
> l = sprite.layers[2]  
> c = l:cel(frame)  
> local img\_G = c.image
> 
> l = sprite.layers[3]  
> c = l:cel(frame)  
> local img\_B = c.image
> 
> local img\_w = sprite.width  
> local img\_h = sprite.height  
> local colorMode = sprite.colorMode
> 
> local layer\_output = sprite:newLayer()  
> layer\_output.name = currentLayer.name .. “\_READY”
> 
> app.refresh()
> 
> local img\_output = Image(img\_w, img\_h, colorMode)
> 
> for y = 0, img\_h - 1, 1 do  
> for x = 0, img\_w - 1, 1 do  
> – get pixel values  
> local r = Color(img\_R:getPixel(x, y))  
> local g = Color(img\_G:getPixel(x, y))  
> local b = Color(img\_B:getPixel(x, y))  
> img\_output:drawPixel(x, y, Color( math.max(r.red,r.green,r.blue),math.max(g.red,g.green,g.blue),math.max(b.red,b.green,b.blue),255))  
> end  
> end
> 
> sprite:newCel(layer\_output, frame, img\_output, Point(0,0))
> 
> app.refresh()

There are at least two problems:

The layers are not in the same order that they are set on the editor and it seems to vary between sprites. Am I reading them from the wrong place? Im not sure if it matters what layer is currently selected, but it shouldn’t.

For some reason the layers when being read, do not line up with each other as they should and instead the resulting layer has their colors in wildly wrong positions. Everything seems to be end up piled into a mess in the top left corner of the sprite? Should I use something else than getPixel()?

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![57E](https://community.aseprite.org/letter_avatar/57e/32/6_449c533f586939b6e5f246632a46421f.png) [@57E](https://community.aseprite.org/u/57E)\
**Post date:** [November 27, 2026, 12:41pm UTC](https://community.aseprite.org/t/having-issues-with-reading-from-multiple-layers-in-a-custom-script/29191/2 "2026-11-27T12:41:20Z")

</div>

Made a hack that sorts the layers based on their names that seems to be working.

However it still does what ever this is:

Left is what the new layer is supposed to look like and on the right is what it currently looks like

 ![shit](https://community.aseprite.org/uploads/default/original/3X/4/2/42ba5ecaa3973b1726a13b4451872030ad095468.png)

> if app.apiVersion \< 1 then  
> return app.alert(“This script requires Aseprite v1.2.10-beta3”)  
> end
> 
> local sprite = app.activeSprite  
> local frame = app.activeFrame
> 
> local currentLayer = app.activeLayer  
> local currentCel = currentLayer:cel(frame)  
> local img = currentCel.image
> 
> local img\_R = img  
> local img\_G = img  
> local img\_B = img  
> local c  
> for i, layer in ipairs(sprite.layers) do  
> c = layer:cel(frame)  
> if layer.name == “R” then  
> img\_R = c.image  
> end  
> if layer.name == “G” then  
> img\_G = c.image  
> end  
> if layer.name == “B” then  
> img\_B = c.image  
> end  
> end
> 
> local img\_w = sprite.width  
> local img\_h = sprite.height  
> local colorMode = sprite.colorMode
> 
> local layer\_output = sprite:newLayer()  
> layer\_output.name = currentLayer.name .. “\_READY”
> 
> app.refresh()
> 
> local img\_output = Image(img\_w, img\_h, colorMode)
> 
> for y = 0, img\_h - 1, 1 do  
> for x = 0, img\_w - 1, 1 do  
> – get pixel values  
> local r = Color(img\_R:getPixel(x, y))  
> local g = Color(img\_G:getPixel(x, y))  
> local b = Color(img\_B:getPixel(x, y))
> 
> img\_output:drawPixel(x, y, Color( r.red,g.green,b.blue,math.max(r.alpha,g.alpha,b.alpha) ) )  
> end  
> end
> 
> sprite:newCel(layer\_output, frame, img\_output, Point(0,0))
> 
> app.refresh()

---

<div class="post-metadata">

**Author:** ![57E](https://community.aseprite.org/letter_avatar/57e/32/6_449c533f586939b6e5f246632a46421f.png) [@57E](https://community.aseprite.org/u/57E)\
**Post date:** [November 28, 2026, 10:56am UTC](https://community.aseprite.org/t/having-issues-with-reading-from-multiple-layers-in-a-custom-script/29191/3 "2026-11-28T10:56:41Z")

</div>

Well I was way over thinking what I needed to do and it only took like four or five days to figure it out.

So this is the final script:

> local sprite = app.activeSprite  
> for i, layer in ipairs(sprite.layers) do  
> layer.blendMode = BlendMode.ADDITION  
> end
> 
> app.command.FlattenLayers {  
> visibleOnly=false  
> }

And since I can’t delete this now unnecessary topic:

In case anyone comes along who wants to answer the original question I would still like to know if only for the sake of my curiosity.

---

<div class="post-metadata">

**Author:** ![johnwhite](https://community.aseprite.org/letter_avatar/johnwhite/32/6_449c533f586939b6e5f246632a46421f.png) [@johnwhite](https://community.aseprite.org/u/johnwhite)\
**Post date:** [December 1, 2026, 7:07pm UTC](https://community.aseprite.org/t/having-issues-with-reading-from-multiple-layers-in-a-custom-script/29191/4 "2026-12-01T19:07:21Z")

</div>

This post was flagged by the community and is temporarily hidden.
