Having issues with reading from multiple layers in a custom script

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?

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

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()

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.