GIMP 2.99 / Python3 Gimp Units how to set PIXELS

In this one:

 procedure = Gimp.get_pdb().lookup_procedure('gimp-text-layer-new')
 config = procedure.create_config()
 config.set_property('image', image)
 config.set_property('text', "My text")
 config.set_property('font', Gimp.Font.get_by_name("Liberation Sans Bold"))
 config.set_property('size', 50)
 config.set_property('unit', ?)
 result = procedure.run(config)
 success = result.index(0)
 my_layer = result.index(1)

How do I tell it to use ‘unit’ PIXELS?

There still is an open issue about the API for GimpUnit. It doesn’t work at the moment, so you’ll have to wait until that is fixed.

Ok.
Thanks for the info.

IMHO is it is this complicated to make a single call, there is a very big open issue about the general API…

1 Like

Honestly I haven’t found how to do the one-liner ( which I would prefer).
But the problem about the unit remains, no matter how I do it, doesn’t it?

The only way I have found to create a text layer is through the text_font method which accepts fontsize in pixels (not points). SelFont has to be a font object from the font chooser or using get_by_name.
mytext = Gimp.text_font(image, None, x_text, y_text, text, border, antialias, fontsize, SelFont)
Hope that helps

Thanks for that.
It works, but unfortunately, this one doesn’t let me specify a parent.
And I can’t find a way to move layers into a group.
So I’ll just have to wait until we can tell the proper function
(something like Gimp.TextLayer.new() ?)
how to set “pixels”

The GimpUnit implementation is a very weird exception and probably one of the reasons to break our old API is getting rid of this technical debt.
GimpUnit is a weird combination of an enum that is merged into a class behind the scenes. Gobject introspection that is used for our new API fails to correctly interpret this.
Untangling this is one of the last things left for the 3.0 API.

I can’t stand that multi-line sequence, it’s impossible to remember and it’s too easy to forget a line or two, so I use a wrapper.

def run_pdb(procname, argdict):
    global pdb
    if not pdb:
        pdb = Gimp.get_pdb()
    pdb_proc = pdb.lookup_procedure(procname)
    pdb_config = pdb_proc.create_config()
    for argname in argdict:
        pdb_config.set_property(argname, argdict[argname])
    vals = pdb_proc.run(pdb_config)
    # Convert to an iterable list
    return [ vals.index(i) for i in range(vals.length()) ]

# How to call it
result = run_pdb('gimp-file-save', {
        'run-mode':      Gimp.RunMode.NONINTERACTIVE,
        'image':         image,
        'file':          Gio.File.new_for_path(filepath)
        })

I’m sure I’ll have to tweak it as time goes by and the API changes. I’d like to see something more like this in the official API.

1 Like

I personally don’t mind that multi-line sequence (if I can’t find a one-liner).
I can copy it from the procedure browser, so no fear to forget anything.
I find it much better than in GIMP 2.10, because the parameters are recognizable by names. It’s not just that you have to put in a value for position 37 or so (for map-object, for example).

Once it will work, what do I put in as unit if I want pixels?
Gimp.Unit.pixel() ?

@Jehan actually just fixed this! And yes, the syntax looks to be Gimp.Unit.pixel ().

Still struggling with the unit thing.
Has it already been merged?
I’m on commit 0310fba (flatpak nightly).

I write

month_layer = Gimp.TextLayer.new(image,
                                     "Mytext",
                                     Gimp.Font.get_by_name("Liberation Sans Bold"),
                                     fontsize, Gimp.Unit.pixel
                                     )

and I get this message:
TypeError: argument unit: Expected Gimp.Unit, but got gi.repository.Gimp.gi.FunctionInfo

So, please what’s the correct way to handle the unit?

or is it

Gimp.Unit.pixel()  

But it still errors with

TypeError: constructor returned NULL

Like you I have been trying to use this but without success. I have tried all sorts of combinations but just get error messages. eg for Gimp.Unit.PIXEL i get AttributeError: type object ‘Unit’ has no attribute ‘PIXEL’. It would be useful if it then gave a suggestion!

This works:

text_layer = Gimp.TextLayer.new(image “hello world”, Gimp.Font.get_by_name(“DejaVu Sans”), 20, Gimp.Unit.point())

There seems to be an issue remaining with using Gimp.Unit.pixel() though. We’ll look into it.

Thanks. That works in my plugin and I can now work on the rest of it

1 Like

Works for me, too, now.
Thanks for the example @CmykStudent

@nelo, @JimDee2 And Jehan just fixed the issue with Gimp.Unit.pixel(), so that’ll work in 3.0 and the next nightly Flatpak. Thank for pointing out the issue!

1 Like

The fact it didn’t work with Gimp.Unit.pixel() was a bug (the function’s arg’s range was wrongly set and simply refused pixel unit). It has been fixed for anyone using the HEAD of our main development branch. :slight_smile:

2 Likes