6

我有一个带有嵌套层结构的 XCD 文件:

image
    front-layer
    content-layer
        content-layer-name-1
        content-layer-name-2
        content-layer-name-3
    back-layer

我用image = pdb.gimp_file_load(xcf_file, xcf_file)and can get front-layer, content-layerand back-layeras image.layers[0], image.layers[1]and image.layers[2]. 但是 Gimp 无法content-layer通过列表索引获取子层。

我可以使用pdb.gimp_image_get_layer_by_name(image, 'content-layer-name-3'),但我不知道图层的名称。

我尝试了pdb.gimp_item_get_children(image.layers[1]),但是这个方法返回INT32ARRAY了项目的子列表,我还没有找到如何通过它的 id 检索项目。

如何在 Gimp (2.8) 中使用 Python 从组图层中获取子图层?

4

2 回答 2

9

GIMP Python 在这个开发周期中大部分时间都没有维护(你可以把大部分责任归咎于我自己)。

完成的少数更新之一是创建“Item”类 - 并在其上实现类方法,允许使用 PDB 方法返回的数字 ID 来检索项目。

因此,您可以使用,就像您发现的那样pdb.gimp_item_get_children(group_layer),在返回的 ID 上供孩子们使用gimp.Item.from_id来检索实际图层。

这是一个 GIMP 控制台部分,我在其中“手动”检索子层:

>>> img = gimp.image_list()[0]
>>> c = img.layers[0]
>>> c
<gimp.Layer 'Layer Group'>
>>> pdb.gimp_item_get_children(c)
(1, (4,))
>>> c2 = gimp.Item.from_id(4)
>>> c2
<gimp.Layer 'cam2'>
>>> 

**更新**

我花了一些时间,GIMP 2.8 final 将为层组提供适当的支持——你需要上面的 hack 到 gimp 2.8 RC 1,但是如果你现在从 git master 构建项目,层组显示为实例“GroupLayer”,并具有“图层”属性,其工作方式与图像中的“图层”属性类似。

提交 75242a03e45ce751656384480e747ca30d728206

 Date:   Fri Apr 20 04:49:16 2012 -0300

     pygimp: adds proper support for layer groups

     Layer groups where barely supported using numeric IDs and
     by calling gimp.Item.from_id. This adds a Python
     GroupLayer class.
于 2012-04-20T03:36:13.620 回答
1

感谢您的故障,我在将插件从 2.6 更新到 2.7~2.8 的过程中遇到了同样的问题。这是编辑后的功能:

def find_layer_by_name (image, name):
for layer in image.layers:
    #check if layer is a group and drill down if it is
    if pdb.gimp_item_is_group(layer):
        gr = layer
        gr_items = pdb.gimp_item_get_children(layer)
        for index in gr_items[1]:
            item = gimp.Item.from_id(index)
            if item.name == name:
                return item

    # if layer is on the first level     
    if layer.name == name:
        return layer
        return None
于 2012-04-25T10:51:13.933 回答