Recently a very simple thought stoke me - we can't use different layers in scripting, but we can use different parts of the image with rectangular selection. Here is example of using 3 "layers".
int w=(selection.Right-selection.Left)/3;
for (int y = rect.Top; y < rect.Bottom; y++)
{
for (int x = rect.Left; x < rect.Right; x++)
{
if (x=selection.Left+2*w)
{
continue;
}
//here we can change only middle part of the image using left and right "layer".
}
}
}
For non-rectangular selections offsets of "layers" have to be passed via parameters.