Gimpfu 脚本:为什么图层的 ID 会损坏?

Gimpfu scripting: Why does layer's ID get corrupted?

我一直在试验 GIMP(v2.8.10) 层的操作,以编程方式使用自定义 gimpfu 插件。我成功实现 select、旋转、缩放和平移现有图层的一部分:

插件内部部分代码:

相关性已简化。所有这些说明都有效。

# I only use one layer in the probe image
layer = img.layers[0]

selection = pdb.gimp_rect_select(img, init_coords[0], init_coords[1],
                                            my_width,my_height,2,0,0)

# Copy and paste selection
pdb.gimp_edit_copy(layer)
float_layer = pdb.gimp_edit_paste(layer, 1)

# Transform floating selection step by step
float_layer = pdb.gimp_item_transform_rotate_simple(float_layer, 
                                             ROTATE_90, 1, 0, 0)
float_layer = pdb.gimp_layer_scale(float_layer, new_width,new_height, 1)
float_layer = pdb.gimp_layer_translate(float_layer, h_offset, 0)

问题

The procedure gimp-layer-translate has been called with incorrect ID for «layer» arg. Probably is trying to operate with an unexisting layer.

我不知道为什么会这样失败。 如果他们单独工作,为什么不一起工作?我想知道。

参考资料

我习惯从 gimpbook.com as a starting point to build the plug-ins, due to it seems to have a solid structure to enclose the code into "undo blocks". My main source of information about pdb is this website that I found on Google. Also, I found some of the procedures I needed on this question 中的 template.py 开始。

此外,我的 之一可能有助于参考如何开始为 gimp 制作自定义插件。

是什么让我陷入困境

我不确定是 gimp 的错误还是我的代码执行错误。由于在 linked question 的答案中发布了一些代码,我想知道我是否过度使用了可用内存(毕竟,我不知道我必须使用特定的过程调用来销毁对象,直到我发现了那个答案)。 如果我不删除它们会怎样?我想知道。 在我关闭 GIMP 之前,它们是否会一直存在于 RAM 中,或者它们是否会生成会淹没 GIMP 系统的持久文件?老实说,我不知道忽略这个问题的后果。

我通过将 floating_layer 切换到新的 Layer 实例来修复它,然后再执行这两个函数。

fixed_layer = gimp.Layer(img, 'float_layer', int(gh), int(gw),
                                           RGBA_IMAGE, 100, 1)

所以我做了以下组合:

# Transform floating selection step by step
float_layer = pdb.gimp_item_transform_rotate_simple(float_layer, 
                                             ROTATE_90, 1, 0, 0)
fixed_layer = gimp.Layer(img, 'float_layer', new_width, new_height,
                                           RGBA_IMAGE, 100, 1)
fixed_layer = pdb.gimp_layer_scale(float_layer, new_width,new_height, 1)
fixed_layer = pdb.gimp_layer_translate(float_layer, h_offset, 0)

问题好像是,在层上执行了一些程序后,它变成了None(好像它被自动从内存中删除了......)。这个解决方案允许我执行 2 或 3 个以上的操作......这不是一个完整的解决方案。我会继续研究

在使用图层时删除分配。下面是销毁 float_layer 变量的方法:

# This creates a new layer that you assign to the float_layer
float_layer = pdb.gimp_item_transform_rotate_simple(float_layer, 
                                             ROTATE_90, 1, 0, 0)
# This does not return anything at all, it works with the layer in-place.
# But you overwrite the float_layer variable anyway, destroying it.
float_layer = pdb.gimp_layer_scale(float_layer, new_width,new_height, 1)
# This does not work because float_layer is no longer referencing the layer at all
float_layer = pdb.gimp_layer_translate(float_layer, h_offset, 0)

所以改为这样做:

float_layer = pdb.gimp_item_transform_rotate_simple(float_layer, 
                                             ROTATE_90, 1, 0, 0)
pdb.gimp_layer_scale(float_layer, new_width,new_height, 1)
pdb.gimp_layer_translate(float_layer, h_offset, 0)