Bump!
This may be better in its own thread but it could be such a niche thing that it's fine here:
I've been updating the above for Gimp 3 (currently on RC3)
Got as far as setting the brush parameters and creating a new layer. I browsed from Gimp's Python console, but each thing seemed to need an awful lot of lines. The brush codes work, but for the layer code I may have been using wrong parameters as that didn't work. The only thing that did work was finding and copying how it was done in spyro-plus.py (just two lines of code...)
Here's what I have so far (started with the python example with used the GEGL colour invert function):
Notes:
The set_brush_value function was copied from Gimp's Python browser and works fine (note it gets called 4 times from set_brush_sate due to wanting to be thorough about which brush parameters are being set)
The draw_brush function does not work, and there's probably something much simpler, but I've no idea where to look. It'll be Gimp.something but apart from that I'm stumped.
This may be better in its own thread but it could be such a niche thing that it's fine here:
I've been updating the above for Gimp 3 (currently on RC3)
Got as far as setting the brush parameters and creating a new layer. I browsed from Gimp's Python console, but each thing seemed to need an awful lot of lines. The brush codes work, but for the layer code I may have been using wrong parameters as that didn't work. The only thing that did work was finding and copying how it was done in spyro-plus.py (just two lines of code...)
Here's what I have so far (started with the python example with used the GEGL colour invert function):
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))
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)
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 (WIP)"))
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:
## Test Section ##
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))
draw_brush(drawable, width, height)
## End of Test Section ##
return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
Gimp.main(AddScaledBrush.__gtype__, sys.argv)
Notes:
The set_brush_value function was copied from Gimp's Python browser and works fine (note it gets called 4 times from set_brush_sate due to wanting to be thorough about which brush parameters are being set)
The draw_brush function does not work, and there's probably something much simpler, but I've no idea where to look. It'll be Gimp.something but apart from that I'm stumped.