How to call pdb.gimp_pencil in Gimp Python-fu plugin

967 Views Asked by At

I'm trying to automate the task of drawing lines with Gimp.

So I tried the scripting feature with no luck so far.


    >>>from pprint import pprint
    >>>img = gimp.Image(200, 200, RGB)
    >>>pprint (pdb.gimp_pencil.params)
    ((16, 'drawable', 'The affected drawable'), 
     (0,
       'num-strokes',
       'Number of stroke control points (count each coordinate as 2 points) (num-strokes >= 2)'),
     (8,
       'strokes',
       'Array of stroke coordinates: { s1.x, s1.y, s2.x, s2.y, ..., sn.x, sn.y }'))
    >>>pdb.gimp_pencil(img, 4, [0,0,200,200] )
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: wrong parameter type

I couldn't find any example of passing a vector (Array of stroke coordinates) in Python for Gimp

What's wrong here?

1

There are 1 best solutions below

0
On

Ok my mistake, I assumed the TypeError was on the last array argument. As it happens img is not the drawable, hence the TypeError.

You have to:

  1. create an image
  2. create a layer
  3. add the layer to the image
  4. set the layer active
  5. obtain the active drawable of the image

Then only you can use this drawable in the gimp_pencil() method.

img = gimp.Image(200, 200, RGB)
layer = gimp.Layer(img, "Test", 200, 200, RGBA_IMAGE, 100, NORMAL_MODE)
img.add_layer(layer, -1)
pdb.gimp_image_set_active_layer(img, layer)
draw = pdb.gimp_image_get_active_drawable(img)
pdb.gimp_pencil(draw, 4, [0,0,100,100])
disp1 = gimp.Display(img)