GIMP 2.99/Python How to use gradients

I’m trying to use a shapeburst gradient with a specified metric ( CHEBYSHEV) in a script. I’m not sure if what I do is correct.
I do

Gimp.context_set_gradient_fg_bg_rgb() # works
Gimp.context_set_distance_metric(2) # does not work

It sets the fg to bg gradient corectly (as I can see in the UI) but it doesn’t set the distance metric ( nothing to see in the UI).
Then, to fill the layer I use

Gimp.Layer.edit_gradient_fill(second_layer,
                                  7,  # GIMP_GRADIENT_SHAPEBURST_SPHERICAL  # doesn't work
                                  0.0,
                                  False,
                                  1,
                                  0.0,
                                  True,
                                  0.0, 0.0, # (x1, y1) - ignored
                                  0.0, 0.0, # (x2, y2) - ignored
                                  )  
    

And it fills the layer in black. No gradient.
Complete script is here:

Can anybody help, please?

I am also trying to port some of my Python2 plugins. I ran into a similar problem. The edit_gradient_fill method is to fill a selection so you must have a selection on the start image before running the plugin. And I could see the result I wanted in the layers dialog but not on my start image until I added Gimp.displays_flush() after adding the gradient. I hope that helps
Jim Denney

Unfortunately in Python the identifiers as used in GIMP don’t translate exactly. The name of enums are usually the last part (where an underscore divides the parts) of the values as seen in GIMP, but sometimes also the last two parts if the last part isn’t unique. You also can’t just exchange an enum value for a number as shown above.

So, here we should use CHEBYSHEV instead of GEGL_DISTANCE_METRIC_CHEBYSHEV. But that is not enough:

  • Since we haven’t done that yet, you should also add from gi.repository import Gegl to the beginning of your plug-in.
  • When using an enum, we need to reference the module it comes from (Gegl), and the name of the enum (GradientType): edited the example below since I forgot to update
Gimp.context_set_distance_metric(Gegl.DistanceMetric.CHEBYSHEV)

The same for Gimp.Layer.edit_gradient_fill use Gimp.GradientType.SHAPEBURST_SPHERICAL instead of GIMP_GRADIENT_SHAPEBURST_SPHERICAL

Some other comments about your commented out code:

    # Gimp.context_set_opacity (100.0) # Howto?
    # Gimp.context_set_gradient_repeat_mode (GIMP_REPEAT_NONE) # Howto?
    # Gimp.context_set_gradient_reverse (False) # Howto?

Use:

    Gimp.context_set_opacity (100.0) # works
    Gimp.context_set_gradient_repeat_mode (Gimp.RepeatMode.NONE)
    Gimp.context_set_gradient_reverse (False) # This should work

Note that I thought that this should have worked. However, the layer still shows black. I’ll have to investigate what exactly needs changing, or if there is an issue in GIMP that needs fixing.

Thank you both for your suggestions.
Once I start with a selection or set a selection in the script it works!.
That behaviour is different from GIMP 2.10.x so I didn’t think of the selection.

Using the UI in GIMP 2.10 vs 2.99 behaves similarly:
Without selection: in 2.10 it fills with the gradient in 2.99 it fills black.

Please feel free to open issues if you see anything that isn’t working as it should. This is new API, so any testing before 3.0 comes out is very welcome :smiley:

I have already opened an issue about the above not working without a selection present.