Mask flag is not removed even if I remove the mask

Hi,

I use GIMP3.04 on Kubuntu 24.02.
There is a image which has multiple layers and each layer has mask. I would like to remove the mask of first layer and copy the second layer mask to first layer with Python script. However I failed it.

I wrote following codes.

    layers = image.get_layers()
    layers[0].remove_mask(1)
    mask = layers[1].get_mask()
    layers[0].add_mask(mask)

However, I encountered following error messages.

Calling error for procedure ‘gimp-layer-add-mask’:
Item ‘L mask mask’ (5) has already been added to an image

Even if I remove the mask once, it seems that the mask flag of the layer is kept and I can’t add a copied mask to the layer. Is there any way to resolve this issue?

I have found the solution by myself. Following code is successful.

    layers = image.get_layers()
    mask0 = layers[0].get_mask()
    mask1 = layers[1].get_mask()
    Gimp.edit_copy([mask1])
    Gimp.edit_paste(mask0, True)
    Gimp.floating_sel_anchor(image.get_floating_sel())