Posts: 6,515
Threads: 284
Joined: Oct 2016
Reputation:
572
Gimp version:
Operating system(s): Linux
The API you can use is documented here: https://developer.gimp.org/api/3.0/libgimp/index.html
The resulting code is a lot less verbose than the PDB (or the examples auto-generated in the Python console). Of course the doc linked is C-oriented and the Python API is slightly more usable (instead of passing pointer to memory locations for results, you get the results in a returned tuple). For instance, your
Code:
def draw_brush(canvas, width, height):
procedure = Gimp.get_pdb().lookup_procedure('gimp-paintbrush-default')
config = procedure.create_config()
config.set_property('drawable', canvas)
config.set_property('strokes', [width/2, height/2])
result = procedure.run(config)
success = result.index(0)
is (using the documentation from here):
Code:
success=Gimp.paintbrush_default(canvas,[width/2,height/2])
Going through the PDB/config raindance is still necessary when you call plugins, but even then there are ways to make it a lot shorter by delegating all the boilerplate code to a hgeneric function.
Posts: 10
Threads: 2
Joined: Sep 2022
Reputation:
0
Gimp version:
Operating system(s): Windows 10
02-16-2025, 11:31 PM
(This post was last modified: 02-16-2025, 11:34 PM by R Soul.)
Many thanks for your help again Ofnuts.
Got rid of that dud function and am now using this:
Code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import gi
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp
gi.require_version('GimpUi', '3.0')
from gi.repository import GimpUi
gi.require_version('Gegl', '0.4')
from gi.repository import Gegl
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gio
import os
import sys
def N_(message): return message
def _(message): return GLib.dgettext(None, message)
def set_brush_value(procedure_name, property_name, property_value):
procedure = Gimp.get_pdb().lookup_procedure(procedure_name)
config = procedure.create_config()
config.set_property(property_name, property_value)
result = procedure.run(config)
success = result.index(0)
def set_brush_state(width, height):
set_brush_value('gimp-context-set-brush-aspect-ratio', 'aspect', 0)
set_brush_value('gimp-context-set-brush-angle', 'angle', 0)
set_brush_value('gimp-context-set-brush-hardness', 'hardness', 1) #values are 0 to 1, not 0 to 100
set_brush_value('gimp-context-set-brush-size', 'size', max(width, height))
class AddScaledBrush (Gimp.PlugIn):
## GimpPlugIn virtual methods ##
def do_query_procedures(self):
return [ "add-scaled-brush" ]
def do_create_procedure(self, name):
procedure = Gimp.ImageProcedure.new(self, name,
Gimp.PDBProcType.PLUGIN,
self.run, None)
procedure.set_image_types("*")
procedure.set_sensitivity_mask (Gimp.ProcedureSensitivityMask.DRAWABLE)
procedure.set_menu_label(_("Add Scaled Brush"))
procedure.set_icon_name(GimpUi.ICON_GEGL)
procedure.add_menu_path('<Image>/Image/')
procedure.set_attribution("Ofnuts", "Ofnuts", "2023")
return procedure
def run(self, procedure, run_mode, image, drawables, config, run_data):
if len(drawables) != 1:
msg = _("Procedure '{}' only works with one drawable.").format(procedure.get_name())
error = GLib.Error.new_literal(Gimp.PlugIn.error_quark(), msg, 0)
return procedure.new_return_values(Gimp.PDBStatusType.CALLING_ERROR, error)
else:
drawable = drawables[0]
if run_mode == Gimp.RunMode.INTERACTIVE:
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk
GimpUi.init("add-scaled-brush.py")
intersect, x, y, width, height = drawable.mask_intersect()
set_brush_state(width, height)
if intersect:
new_layer = Gimp.Layer.new(image, "Scaled Brush", width, height, Gimp.ImageType.RGBA_IMAGE, 100, Gimp.LayerMode.NORMAL)
image.insert_layer(new_layer, drawable.get_parent(), image.get_item_position(drawable))
Gimp.paintbrush_default(new_layer, [width/2,height/2])
return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
Gimp.main(AddScaledBrush.__gtype__, sys.argv)
Posts: 6,515
Threads: 284
Joined: Oct 2016
Reputation:
572
Gimp version:
Operating system(s): Linux
Note sure it is proper to indicate me as the author ( procedure.set_attribution("Ofnuts", "Ofnuts", "2023"))
|