Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plug-in help
#1
Hello,

I am having an hard time writing my first plus-in in Python.
I wrote a simple "Hello World" plug-in.
It show up in the menus (so the do_create_procedure member function work as intended).
It show up in the python procedures browser (so the do_query_procedures member function work as intended).
But it does not produce anything in the errors window and that leave me completly clueless.
What did I do wrong ?

Here is the source code:
Code:
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

class Plugin (Gimp.PlugIn):
   def do_query_procedures(self):
       return [ "fl-plug-in-Hello-World" ]

   def do_set_i18n (self, name):
       return False

   def do_create_procedure(self, name):
       procedure = Gimp.ImageProcedure.new(self, name,
                                           Gimp.PDBProcType.PLUGIN,
                                           self.run, None)

       procedure.set_image_types("*")

       procedure.set_menu_label("Hello World Python plug-in")
       procedure.add_menu_path('<Image>/Filters/My Scripts/')

       procedure.set_documentation("Hello World",
                                   "Python 3 plug-in for GIMP 3.0",
                                   name)
       procedure.set_attribution("Fabrice", "Lambert", "2025")

       return procedure

   def run(self, procedure, run_mode, image, n_drawables, drawables, config, run_data):
       Gimp.message("Hello world!")
       # do what you want to do, then, in case of success, return:
       return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())

Gimp.main(Plugin.__gtype__, sys.argv)
Reply
#2
(01-07-2025, 05:28 PM)Deedolith Wrote: Hello,

I am having an hard time writing my first plus-in in Python.
I wrote a simple "Hello World" plug-in.
It show up in the menus (so the do_create_procedure member function work as intended).
It show up in the python procedures browser (so the do_query_procedures member function work as intended).
But it does not produce anything in the errors window and that leave me completly clueless.
What did I do wrong ?

...
I haven't tried the script so I'm just guessing - if it works as you have it above the message (being only a single line) will appear (possibly briefly) in the status line at the bottom of the GIMP window. Add a "\n" after the exclamation mark in your Hello world! string to make the message multi-line - it will then appear as a message dialog. Also run GIMP from a terminal window to see any error messages caused by the script.
Reply
#3
No luck,
Still nothing after adding a carriage return (\n), and nothing after launching Gimp from the command line (--verbose --console-messages).
Reply
#4
When I start Gimp in a terminal (that would be using "gimp-console.exe" for you) I see this:

TypeError: Plugin.run() missing 1 required positional argument: 'run_data'

This is because you have one parameter too many: n_drawables). Maybe you saw two arguments described somewhere because in other languages such as C you have to give explicitly the size of arrays that you pass, but in Python these are implicit in the type so you just pass the array.


Otherwise, the implementation is a method of the Plugin object (self.run). But in practice the Plugin is only used for registration, and since your plugin object can define several rather unrelated procedures,  even if you have some object class that implements the functionality of the plugin it doesn't need to be a derived Gimp.Plugin (and this could even be a hindrance), instead you would define the plugin implementation as a regular function, that can instantiate such object when Gimp calls i to execute the plugin.

If you want to make your life simpler you can have a look here.
Reply
#5
Hi,
You can create an error file and use the sys pipeline to get runtime feedback. Obviously you'll want to delete this code after the plug-in is stable and finished. You can put the code anywhere in your plug-in. I like to put the code at the beginning of the plug-in.
Code:
# _______________________________________________________________
# Open an error file. It will capture error output.
# The file will also take 'print' output.
import time                         # noqa
error_file = sys.stdout = sys.stderr = open("C:\\error.txt", 'a')
print("\nStart debug,", time.ctime(), "_" * 30, "\n")
# ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯


It's not entirely necessary to close the error file, but I've had trouble with it closing with certain errors. Try closing the file before exiting the plug-in.
Code:
# This is better than letting the OS close the file.
error_file.close()

I don't know your coding experience, but VS Code will help you catch frustrating errors and teach useful code habbits.
Reply
#6
as a side question:

Quote:but VS Code will help you catch frustrating errors and teach useful code habbits

What addons / plugins do you prefer there for python coding?
Reply
#7
Thanks for the help,
Indeed it was an argument error.

Guess the tutorial I followed is outdated: https://testing.docs.gimp.org/2.10/en/gi...-in-basics
Any up to date links suggested ?

(01-08-2025, 08:45 AM)gasMask Wrote: I don't know your coding experience, but VS Code will help you catch frustrating errors and teach useful code habbits.
I have very good experience with languages such as C++ or VB.Net, but I am fairly "green" with python.
Also, I am using Visual Studio, alas it does not support Gimp nativelly (unsure if I can configure it to do so).
Reply
#8
That tutorial you found is for GIMP3, won't work in 2.10.

Best I've found is here:
https://www.gimp.org/docs/python/index.html
It's quite old, though.

And there is a playlist on YT by Jackson Bates:
https://www.youtube.com/watch?v=cPQRxZo9...LV08wxdnWt-
Reply
#9
Erm ...

I am using Gimp 3.00RC2 (as shown in my profile),
plus the code I posted is definetly Gimp 3 compliant (minus mistakes).
Reply
#10
Sorry. My fault.
Reply


Forum Jump: