Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plugin won't show in menu
#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


Messages In This Thread
Plugin won't show in menu - by n3306tx - 03-23-2025, 03:54 AM
RE: Plugin won't show in menu - by Ofnuts - 03-23-2025, 10:30 AM
RE: Plugin won't show in menu - by n3306tx - 03-23-2025, 11:38 PM
RE: Plugin won't show in menu - by Ofnuts - 03-24-2025, 01:04 AM

Forum Jump: