How do I debug GIMP plugins? Where is the output?

7.4k views Asked by At

I am making a plug in in Python for GIMP on Windows. This page suggests running it from the shell or looking at ~/.xsession-errors. Neither work. I am able to run it from the cmd shell as:

gimp-2.8.exe -c --verbose

From here. This causes the output from pdb.gimp_message(...) to go to a terminal, but only works when everything is running as expected. I get no output on crashes. I tried print statements, they go nowhere. This is a similar problem.

Running it from Python-Fu console gets me nowhere. I need to comment out import gimpfu as it raises errors and I don't get GTK working. Even if the plugin registers and shows on the menu, when there is some error I don't know where to look for hints.

  1. Can I refresh a plugin without restarting GIMP?
  2. Can I run plugins from the Python-Fu shell as opposed to importing them to make sure they parse?
  3. Is there an error-log?
  4. How to run GIMP on Windows from a shell to see output? Am I better off under Cygwin or VirtualBox?
  5. How to connect Winpdb to a Python process that runs in GIMP?
3

There are 3 answers

0
Omid Raha On BEST ANSWER

1- can i refresh a plugin without restarting gimp ? (so at least my slow-morph will be faster )

You must restart GIMP when you add a script or change register(). No need to restart when changing other parts of the script -- it runs as a separate process and will be re-read from disk each time.

helpful source: http://gimpbook.com/scripting/notes.html

2- can i run plug-ins from the python-fu shell. (as opposed to just importing them to make sure they parse.)

Yes, you can access to your registered plug-in in python-fu console as:

>>> pdb.name_of_registerd_plug-in

And can call it like:

>>> pdb.name_of_registerd_plug-in(img, arg1, arg2, ...)

Also in python-fu dialog console, you can click to Browse .. option and find your registered plug-in, and then click Apply , to import it to python-fu console.

helpful source: http://registry.gimp.org/node/28434

3- is there an error-log i am missing, or something to that effect?

To log, you can define a function like this:

def gimp_log(text):
    pdb.gimp_message(text)

And use it in your code, whenever you want.

To see log of that, in gimp program, open Error Console from Dockable Dialogs in Windows menu, otherwise a message box will be pop up on every time you make a log.

Also you can redirect stdin and stdout to a file,:

import sys
sys.stderr = open('er.txt', 'a')
sys.stdout = open('log.txt', 'a')

When you do that, all of exceptions will go to err.txt and all of print out will be go to log.txt Note that open file with a option instead of w to keep log file.

helpful sources:

How do I output info to the console in a Gimp python script?

http://www.exp-media.com/content/extending-gimp-python-python-fu-plugins-part-2

4- is there a way to run gimp on windows from a shell to see output ? (am i better off under cygwin (or virtualbox.. ))?

I got some error for that, but may try again ...

5- i haven't yet looked up how to connect winpdb to an existing process. how would i go about connecting it to a python process that runs inside gimp?

First install winpdb , and also wxPython ( Winpdb GUI depends on wxPython)

Note that Gimp has own python interpreter, and may you want to install winpdb to your default python interpreter or to gimp python interpreter.

If you install winpdb to your default python interpreter, then you need to copy rpdb2.py installed file to ..\Lib\site-packages of gimp python interpreter path.

After that you should be able to import pdb2 module from Python-Fu console of gimp:

GIMP 2.8.10 Python Console
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
>>> import rpdb2
>>>

Now in your plug-in code, for example in your main function add following code:

import rpdb2 # may be included out side of function.
rpdb2.start_embedded_debugger("pass") # a password that will asked by winpdb

Next, go to gimp and run your python plug-in, when you run your plug-in, it will run and then wait when reach to above code.

Now to open Winpdb GUI go to ..\PythonXX\Scripts and run winpdb_.pyw.

(Note that when using Winpdb for remote debugging make sure any firewall on the way has TCP port 51000 open. Note that if port 51000 is taken Winpdb will search for an alternative port between 51000 and 51023.)

Then in Winpdb GUI from File menu select attach and give pass as password to it, and then you can see your plug-in script on that list, select it and start your debug step by step.

debug python gimp plugin with Winpdb

helpful resource: Installing PyGIMP on Windows

Useful sources:

http://wiki.gimp.org/index.php/Hacking:Plugins

http://www.gimp.org/docs/python/index.html

http://wiki.elvanor.net/index.php/GIMP_Scripting

http://www.exp-media.com/gimp-python-tutorial

http://coderazzi.net/python/gimp/pythonfu.html

http://www.ibm.com/developerworks/opensource/library/os-autogimp/os-autogimp-pdf.pdf

0
vish On

as noted in How do I output info to the console in a Gimp python script?

add

import sys
sys.stderr = open( 'c:\\temp\\gimpstderr.txt', 'w') 
sys.stdout = open( 'c:\\temp\\gimpstdout.txt', 'w')

at the beginning of the plug in file.

0
John_L On

I am a newbie to python, but I would like to give a shout-out, first to winpdb, and then to this comment for integrating winpdb into GIMP. This same procedure works as well for LibreOffice 4.

If I may be allowed to vent a little; I have a moderate amount of experience with Visual Basic, more or less at a hobbiest level, but I decided a few years ago to get into OpenOffice when MicroSoft threatened to abandon VB for the Mac. I don't want to say that VB in OpenOffice was onerous, but the lack of anything resembling an IDE is tedious. Now, with winpdb, I will never be looking back. It's python from here on out, baby.

Steps taken:

-- As suggested by Omid above, I first got winpdb running out of GIMP (relatively painless).

-- I copied the rpdb2.py file to C:\Program Files\LibreOffice 4\program\python-core-3.3.3\lib\site-packages\rpdb2.py. (Win 7, LibreOffice 4.4.03)

-- I edited the HelloWorld.py file in C:\Program Files\LibreOffice 4\share\Scripts\python directory (saved in WinPDb_HelloWorld.py to same directory).

# HelloWorld python script for the scripting framework
# This file is part of the LibreOffice project.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. blah, blah, blah ...

import rpdb2
#rpdb2.start_embedded_debugger("Your Password Here")    # << DON'T start debugger here.
    # It only gets you lost in the LO python wrapper when debugging.

def HelloWorldPython( ):
    """Prints the string 'Hello World(in Python)' into the current document"""

    # start debugger INSIDE function, where it will be called from LO Macros -- Duh!!
    rpdb2.start_embedded_debugger("YourPasswordHere") 

    #get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop() 
    #... etc., see HelloWorld.py

WinPDb_HelloWorld appears under LibreOffice Macros in the Macro Selector (see https://wiki.openoffice.org/wiki/Python_as_a_macro_language for more on that).

(can't show you a picture - posting as a guest)