plug-ins: update foggify to latest API and add a GUI.

The following changes were made:
- run() signature now moved to multi-drawables.
- sensitivity mask now enables the plug-in when one or several drawables
  are selected. When several, a fog layer will be created over each
  selected layer.
- Mnemonics added to properties.
- Color property finally used now that our API has support for boxed
  parameters with GimpRGB value type.
- Use a config object instead of ordered GimpValueArray and use the
  begin|en_run() API to store values between runs.
- Add a graphical interface generated with the new GimpProcedureDialog
  API.
This commit is contained in:
Jehan 2021-04-20 16:48:42 +02:00
parent ca72c41fcd
commit f15d16150e
1 changed files with 68 additions and 39 deletions

View File

@ -17,6 +17,8 @@
import gi import gi
gi.require_version('Gimp', '3.0') gi.require_version('Gimp', '3.0')
from gi.repository import Gimp from gi.repository import Gimp
gi.require_version('GimpUi', '3.0')
from gi.repository import GimpUi
from gi.repository import GObject from gi.repository import GObject
from gi.repository import GLib from gi.repository import GLib
from gi.repository import Gio from gi.repository import Gio
@ -27,78 +29,104 @@ import gettext
_ = gettext.gettext _ = gettext.gettext
def N_(message): return message def N_(message): return message
def foggify(procedure, run_mode, image, drawable, args, data): def foggify(procedure, run_mode, image, n_drawables, drawables, args, data):
name = args.index(0) config = procedure.create_config()
turbulence = args.index(1) config.begin_run(image, run_mode, args)
opacity = args.index(2)
if run_mode == Gimp.RunMode.INTERACTIVE: if run_mode == Gimp.RunMode.INTERACTIVE:
# TODO: add a GUI. This first port works just with default GimpUi.init('python-fu-foggify')
# values. dialog = GimpUi.ProcedureDialog.new(procedure, config)
color = Gimp.RGB() # Even though gimp_procedure_dialog_get_color_widget() is
color.set(240.0, 180.0, 70.0) # transfer none, somehow if I don't set it to a variable PyGObject
# frees it, then the plug-in fails at dialog.fill().
b = dialog.get_color_widget('color', True, GimpUi.ColorAreaType.FLAT)
dialog.fill(None)
if not dialog.run():
dialog.destroy()
config.end_run(Gimp.PDBStatusType.CANCEL)
return procedure.new_return_values(Gimp.PDBStatusType.CANCEL, GLib.Error())
else:
dialog.destroy()
color = config.get_property('color')
name = config.get_property('name')
turbulence = config.get_property('turbulence')
opacity = config.get_property('opacity')
Gimp.context_push() Gimp.context_push()
image.undo_group_start() image.undo_group_start()
if image.base_type() is Gimp.ImageBaseType.RGB: if image.get_base_type() is Gimp.ImageBaseType.RGB:
type = Gimp.ImageType.RGBA_IMAGE type = Gimp.ImageType.RGBA_IMAGE
else: else:
type = Gimp.ImageType.GRAYA_IMAGE type = Gimp.ImageType.GRAYA_IMAGE
fog = Gimp.Layer.new(image, name, for drawable in drawables:
drawable.width(), drawable.height(), type, opacity, fog = Gimp.Layer.new(image, name,
Gimp.LayerMode.NORMAL) drawable.get_width(), drawable.get_height(),
fog.fill(Gimp.FillType.TRANSPARENT) type, opacity,
image.insert_layer(fog, None, 0) Gimp.LayerMode.NORMAL)
fog.fill(Gimp.FillType.TRANSPARENT)
image.insert_layer(fog, drawable.get_parent(),
image.get_item_position(drawable))
Gimp.context_set_background(color) Gimp.context_set_background(color)
fog.edit_fill(Gimp.FillType.BACKGROUND) fog.edit_fill(Gimp.FillType.BACKGROUND)
# create a layer mask for the new layer # create a layer mask for the new layer
mask = fog.create_mask(0) mask = fog.create_mask(0)
fog.add_mask(mask) fog.add_mask(mask)
# add some clouds to the layer # add some clouds to the layer
Gimp.get_pdb().run_procedure('plug-in-plasma', [ Gimp.get_pdb().run_procedure('plug-in-plasma', [
GObject.Value(Gimp.RunMode, Gimp.RunMode.NONINTERACTIVE), GObject.Value(Gimp.RunMode, Gimp.RunMode.NONINTERACTIVE),
GObject.Value(Gimp.Image, image), GObject.Value(Gimp.Image, image),
GObject.Value(Gimp.Drawable, mask), GObject.Value(Gimp.Drawable, mask),
GObject.Value(GObject.TYPE_INT, int(time.time())), GObject.Value(GObject.TYPE_INT, int(time.time())),
GObject.Value(GObject.TYPE_DOUBLE, turbulence), GObject.Value(GObject.TYPE_DOUBLE, turbulence),
]) ])
# apply the clouds to the layer
fog.remove_mask(Gimp.MaskApplyMode.APPLY)
fog.set_visible(True)
# apply the clouds to the layer
fog.remove_mask(Gimp.MaskApplyMode.APPLY)
fog.set_visible(True)
Gimp.displays_flush() Gimp.displays_flush()
image.undo_group_end() image.undo_group_end()
Gimp.context_pop() Gimp.context_pop()
config.end_run(Gimp.PDBStatusType.SUCCESS)
return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error()) return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
_color = Gimp.RGB()
_color.set(240.0, 0, 0)
class Foggify (Gimp.PlugIn): class Foggify (Gimp.PlugIn):
## Parameters ## ## Parameters ##
__gproperties__ = { __gproperties__ = {
"name": (str, "name": (str,
_("Layer name"), _("Layer _name"),
_("Layer name"), _("Layer name"),
_("Clouds"), _("Clouds"),
GObject.ParamFlags.READWRITE), GObject.ParamFlags.READWRITE),
"color": (Gimp.RGB,
_("Fog color"),
_("Fog color"),
GObject.ParamFlags.READWRITE),
"turbulence": (float, "turbulence": (float,
_("Turbulence"), _("_Turbulence"),
_("Turbulence"), _("Turbulence"),
0.0, 10.0, 1.0, 0.0, 10.0, 1.0,
GObject.ParamFlags.READWRITE), GObject.ParamFlags.READWRITE),
"opacity": (float, "opacity": (float,
_("Opacity"), _("O_pacity"),
_("Opacity"), _("Opacity"),
0.0, 100.0, 100.0, 0.0, 100.0, 100.0,
GObject.ParamFlags.READWRITE), GObject.ParamFlags.READWRITE),
} }
# I use a different syntax for this property because I think it is
# supposed to allow setting a default, except it doesn't seem to
# work. I still leave it this way for now until we figure this out
# as it should be the better syntax.
color = GObject.Property(type =Gimp.RGB, default=_color,
nick =_("Fog _color"),
blurb=_("Fog color"))
## GimpPlugIn virtual methods ## ## GimpPlugIn virtual methods ##
def do_query_procedures(self): def do_query_procedures(self):
@ -112,6 +140,8 @@ class Foggify (Gimp.PlugIn):
Gimp.PDBProcType.PLUGIN, Gimp.PDBProcType.PLUGIN,
foggify, None) foggify, None)
procedure.set_image_types("RGB*, GRAY*"); procedure.set_image_types("RGB*, GRAY*");
procedure.set_sensitivity_mask (Gimp.ProcedureSensitivityMask.DRAWABLE |
Gimp.ProcedureSensitivityMask.DRAWABLES)
procedure.set_documentation (N_("Add a layer of fog"), procedure.set_documentation (N_("Add a layer of fog"),
"Adds a layer of fog to the image.", "Adds a layer of fog to the image.",
name) name)
@ -122,8 +152,7 @@ class Foggify (Gimp.PlugIn):
procedure.add_menu_path ("<Image>/Filters/Decor") procedure.add_menu_path ("<Image>/Filters/Decor")
procedure.add_argument_from_property(self, "name") procedure.add_argument_from_property(self, "name")
# TODO: add support for GBoxed values. procedure.add_argument_from_property(self, "color")
#procedure.add_argument_from_property(self, "color")
procedure.add_argument_from_property(self, "turbulence") procedure.add_argument_from_property(self, "turbulence")
procedure.add_argument_from_property(self, "opacity") procedure.add_argument_from_property(self, "opacity")
return procedure return procedure