Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plugin won't show in menu
#1
Hello.  First off, I am not a coder, but years ago I managed to write a plugin in GIMP 2.10 to do some basic repetitive tasks like adding solid color layers with masks, in groups and set other various parameters.  The first thing that happens is the original image layer is copied and desaturated.  Honestly, I am struggling with rewriting this simple plug-in in GIMP 3 format...I can't even get anything to show up in the GIMP menus even after copying the code from another GIMP 3 plugin example.  Running it line by line through the python console produces no error.  Could someone look at this basic code to copy a layer, create a new layer and desaturate it and help me understand why I can't even get the plugin to show up in the GIMP menus?  I wish there was a GIMP 2 to 3 converter or a forum group that would help rewrite code specifically for us non-coders.  I just don't understand enough on how to write python code when I cannot find examples to do basic tasks.  Thanks for any feedback you can provide.

The code is attached 
[attachment=13224]
Reply
#2
When I start Gimp in a terminal:
Code:
Traceback (most recent call last):
 File "/home/me/Code/Gimp/Foreign3/Desat/Desat.py", line 50, in <module>
   Gimp.main(AddDesaturateLayerPlugin.__gtype__, None)
TypeError: Argument 2 does not allow None as a value

... but that's only the beginning Big Grin Big Grin .  

A version that works (see the ### for explanations)

Code:
#!/usr/bin/env python3
# GIMP plugin to add a new desaturated layer

import sys

import gi
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp
gi.require_version('GimpUi', '3.0')
from gi.repository import GimpUi
from gi.repository import GLib

def add_desaturate_layer(procedure, run_mode, image, drawables, config, data): ### Separate function not a method of the plugin
    # Duplicate the current layer
    source_layer=drawables[0] ### Drawables is a list (with a single element here)
    new_layer = source_layer.copy()
    new_layer.set_name("Desaturate")
    image.insert_layer(new_layer,
                       source_layer.get_parent(),
                       image.get_item_position(source_layer)) ### technically, you should insert in the layers's parent, above the source layer

    # Apply the desaturation
    new_layer.desaturate(Gimp.DesaturateMode.LIGHTNESS)  ### desaturate is method of Gimp.Drawable

    return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error()) ### Procedure has to return something

class AddDesaturateLayerPlugin(Gimp.PlugIn):

    def do_query_procedures(self):
        return ['python-fu-add-desaturate-layer']

    def do_activate(self):
        pass

    def do_create_procedure(self, name):
        if name == 'python-fu-add-desaturate-layer':
            print(f'------------- Creating {name}')
            procedure = Gimp.ImageProcedure.new(    ### Gimp.ImageProcedure, not just plain Gimp.Procedure
                self, name, Gimp.PDBProcType.PLUGIN, add_desaturate_layer, None)
            procedure.set_sensitivity_mask (Gimp.ProcedureSensitivityMask.DRAWABLE) ### Only enable if there is exactly one selected drawable
            procedure.set_menu_label("Add Desaturate Layer")
            procedure.add_menu_path('<Image>/Filters/')
            procedure.set_attribution("YourName", "YourContact", "Adds a desaturated layer")
            procedure.set_documentation(
                "Adds a new desaturated layer to the active image.",
                "Adds a new desaturated layer to the active image.",
                name,
            )
            return procedure

Gimp.main(AddDesaturateLayerPlugin.__gtype__,sys.argv) ### 2nd arg normally the sys.argv
Reply
#3
Ofnuts, thank you very much for the help and explanations. It works great and is much appreciated. But it seems this new python 3 structure is just beyond my old brain's capabilities to understand the coding logic. So for now I'm going to have to hang on to GIMP 2 to run my full plugin to prep my projects then take the project over to GIMP 3 to complete until I can figure out how to completely convert the basic tasks code to GIMP 3 format in my plugin. I do appreciate you taking your time to help me out with this basic desaturate function.
Reply
#4
(03-23-2025, 11:38 PM)n3306tx Wrote: Ofnuts, thank you very much for the help and explanations.  It works great and is much appreciated.  But it seems this new python 3 structure is just beyond my old brain's capabilities to understand the coding logic.  So for now I'm going to have to hang on to GIMP 2 to run my full plugin to prep my projects then take the project over to GIMP 3 to complete until I can figure out how to completely convert the basic tasks code to GIMP 3 format in my plugin.  I do appreciate you taking your time to help me out with this basic desaturate function.

 My brain is probably older than yours.
Reply


Forum Jump: