Draw in Layer with Selection LUA

I have an issue with the position when I try to draw on a selection on a layer:

local img = cel.image:clone()
local selection = app.activeLayer.sprite.selection
if selection:contains(x,y) then
coloridx = math.random(1,colorcount)

I’ve tried offseting the selection.origin but I cannot get it work fine.

Any ideas what I am doing wrong?

I think it has to be with the position of the layer but no idea how to read the value, tried bounds, position, origin.

This is likely because you’re mixing up what your x and y values are relative to. I’m fairly new to this as well so someone correct me if I’m wrong, but app.activeLayer.sprite.selection operates relative to the whole canvas. ie when you go selection.contains(0, 0), your asking if the top left corner pixel of the canvas is in the selection.

Images have their own coordinate space. (0.0) relative to an image refers to the top left corner of its bounding box. In your case, this bounding box is likely offset from your larger canvas, which is often the case when you’re working with transparency. You should be able to read this image offset from the cel using cel.bounds.x and cel.bounds.y (or cel.position to get a Point).

Try printing out this value to get an understanding of what’s going on. You likely just have to account for this offset when using selection:contains() and/or img:drawPixel().

1 Like

Yes, it is has to be about something with the offset, because as you can see

this is using: img:drawPixel(0,0,ARRAY[coloridx]) , but I do not now how to find the offset of that layer. because there is no app.activeLayer.offset, position, neither bounds

As I mentioned, cel.bounds is what you’re looking for. You would iterate over all the local x and y values in your selection area similar to something like this:

for x = 0, selection.bounds.width - 1 do
    for y = 0, selection.bounds.height - 1 do
        if selection:contains(selection.bounds.x + x, selection.bounds.y + y) then
            image:drawPixel(selection.bounds.x-cel.bounds.x+x, selection.bounds.y-cel.bounds.y+y, ARRAY[coloridx])

Though this will likely fail if you try to draw pixels outside the bounds of the image/cel, or if the frame has no image/cel.


Ok it worked! thank you a lot!