Is there a way to programmatically attach filters to layers

@Ofnuts Your code works fine but with the following limitations:

  • It only works for applying/merging a layer directly into a layer’s buffer. Our new API will allow this and will also allow to create a non-destructive filter attached to a layer (which, unless mistaken, was your initial question).
  • It will also eventually works for GIMP’s internal operations. For instance, your code won’t work with opName="gimp:color-balance" (because this op is not visible on plug-in side) whereas this will work with our API (once I’ll have handled the passing of the config file indirection for these internal ops).
  • You can also change the blending mode and opacity.

With our new API, you’d be able to transform your help function like this to make good use of the 2 first advantages at least (and adding more args, you could support blending mode/opacity too):

def applyGeglOnBuffers(drawable, merge, opName, **kwargs):
  # XXX: the last arg is the filter name; you could set custom names
  # helping making it clearer filters which came from a given script
  # in the effect list.
  filter = Gimp.DrawableFilter.new(drawable, opName, opName)
  config = filter.get_config ()
  for name, value in kwargs.items():
    config.set_property(name.replace('_','-'), value)
  filter.update()
  if merge:
    # Not yet implemented in the MR but will be by RC2:
    drawable.merge_filter(filter)
  else:
    # Non-destructive filter:
    drawable.append_filter(filter)

Of course, this is meant to work on a drawable. If you want to work on a separate buffer (not attached to a drawable) like with your current function, your current code is the right one.

1 Like