How to debug a GIMP python-fu plugin in VS Code
People all over the world seem to have trouble in GIMP script debugging
Hello! How are you guys doing?
We are the "MUD"*1 scientist team somewhere in the world.
Are you in trouble with the poor environment of GIMP's python plugin development?
YES!Rejoice humanity, the time has come!
You can now debug a Python-Fu pulgin in VS Code. This method is not formal and might be a hack. But if you're sick of GIMP's python debugging, it's worth a try.
Issue
- We want to set breakpoints, step through, and check variables in the Python plugin in GIMP.
- We want to do a Lint check of Python Code in GIMP. Nothing is displayed even if there is an error, so I have no idea what is wrong.
Required environment
- Windows 10*2
- Python 2.7.18 (Last version of Python2) : Not anaconda
- GIMP 2.10 (We checked on Ver. 2.10.24)
- Visual Studio Code (We checked on Ver. 1.56.2)
Preparation
Let's assume that you are ready for:
- Python 2.7.18 installed on [C:\Python27]
- GIMP 2.10 installed on [C:\Program Files\GIMP 2]
- Visual Studio Code installed on somewhere
And the system environment variables are set for Python 2.7.
Abstract
Python 2.7 included in GIMP 2.10 does not have a pip executable.
There are too many restrictions to install into this GIMP python environment.
So, once you install it in another Python 2.7 environment with pip, copy that packages to the GIMP environment. Debug with VS Code using a common remote debugger package that does not cause Gimp to error.
Attention!
However, there are many packages that do not work just by copying.(Since the python included in GIMP is customized for handling with GIMP, errors may occur when executing other packages.)
In order for the python package installed in GIMP to operate stably, Developers have set restrictions on purpose.
But we really want to install another package in the GIMP environment and debug with the IDE.
Make sure that you can undo this procedure, before performing this . Because it may destroy your precious environment.
Setup steps
Since it is a sample program for explanation, we will also introduce some unrelated packages. But it will be a reference for introducing various packages.
Core package is ptvsd, needed for remote debugging of VS Code.
- Step. 1: cd c:\Python27\Scripts
- Step. 2: ..\python -m pip install --upgrade pip
- Step. 3: pip install ptvsd
- Step. 4: pip install pyyaml (*This is for use in the sample program.)
- Step. 5: copy C:\Python27\Lib\site-packages { \ptvsd, \ptvsd-4.3.2.dist-info} to C:\Program Files\GIMP 2\lib\python2.7\site-packages (*with administrator permission)
- Step. 6: copy C:\Python27\Lib\site-packages { \yaml, \_yaml, \PyYAML-5.4.1.dist-info} to C:\Program Files\GIMP 2\lib\python2.7\site-packages (*with administrator permission)
- Step. 7: Start GIMP program and Python Console operate with /Filters/Python-Fu/Console menu select. And input "import ptvsd"[Enter] and "import yaml"[Enter]. Check that no error is displayed on the console.
- Step. 8: Install Python extensions for VS Code.(Extensions of left pane/search: Python/Python(ms-python.python)Install
- and Python for VSCode(tht13.python)
- Step. 9: Create a launch.json at VS Code: (Pass) If you have the information, you can create it here, but it is easier to do it only once at runtime.
- Step. 10: Windows [Start]Right button/System/Advanced system settings/System Properties Dialog [Environment Variables...]button/User or System valiables [New...]/Variable name:[PYTHONPATH]/Variable value:[C:\Program Files\GIMP 2\lib\gimp\2.0\python;C:\Program Files\GIMP 2\bin;C:\Program Files\GIMP 2\lib\python2.7;C:\Program Files\GIMP 2\lib\python2.7\site-packages]
Now, You have finished the setup(Except for [launch.json]file definitions). Thank you for your hard work.
Preparation of sample program
Let's prepare a slightly longer program to check the debug.
By the way, "pix2pix" performs image translation.
The following sample program prepares images for "pix2pix" machine learning.
C:\install\pyFu-scripts\pyFu-pix2pix-preprocess.py
C:\install\puFu-scripts\yaml\pyFu-pix2pix-preprocess.yaml
Setting additional folders for developed plugins
/Edit/Preferences menu/[+]Expand the Folders in the left pane/Plug-ins/[+]add a new folder [C:\install\pyFu-scripts]/[^]Move the selected folder up/[^]Move the selected folder up/[OK]button
Restart the GIMP program with --verbose and --console-messages options.
Check the sample plug-in
If there is an error, console display like this:
If OK, no error message is displayed after "Querying plug-in:" message.
And a new menu appears.
If you really don't know the cause of the error, try the following:
Report any errors,
But otherwise the plugin will crash.
Or if you check the source with VSCode, you will be able to suggest what is wrong.
Please fix my bug that I inadvertently forgot to fix. There are at least two mistakes.
Those who copied and pasted may be missing two more [ ] .
Operation
(1) Prepare some 1024x1024 pixcel images in the [C:\work\GIMP_Ope1\Target] folder.
(2) Launch the GIMP program, and /File/New/Create a New Image.
(3) /MyPlugin/Pix2pix preprocess/ launch the dialog.
(4) Click the [OK] button. (wait for socket connection)
(5) If the firewall asks for confirmation, [Allow access] if there is no problem.
(6) Open the [C:\install\pyFu-scripts\pyFu-pix2pix-preprocess.py]file at the Visual Studio Code.
(7) Set break point at the source.
(8) Run and Debug (Left side pane |or| Ctrl + Shift + D) / [Run and Debug]button click/Remote attach/"localhost"[Enter]/"5678"[Enter] or select defined connection.
[Open launch.json]button click/open a folder (left pane)
input:[C:\install\pyFu-scripts\yaml] /]Select Folder]button click (for example)
Up to this point, the operation check has been completed, and the actual registration is yet to come.
Select a Add Configration menu and do the same operation as before.
That will generate a launch.json file.
And you can do remote attach oparation as select Start Debugging dropdown and click the execute triangle icon.
(9) After remote attached, stop at the break point. and you can check the contents of stored variables.
and the color of the bottom of the window turns orange, indicating that it was attached.
If the target dialog is over, an error will be displayed because the connection cannot be made.
(10) If attached, and you can continue, step over, step in, and step out. You should not use restart, because it causes a plugin crash.
We can use these processed image files to train "pix2pix".*3
Now you are a specialist in GIMP's python debugging.
Thank you for reading though it is long. And may happiness come to you.
(CC) KooWells and thy servant 2021.
*1:Why is it "MUD"? Because our boss is a frog or something spiritual being of "Gaia" or something "Chaos" from out of space :-)
*2:We checked on Windows 10, but the theory would be the same on MacOS and Linux or other IDE.
*3:These sample images are from the "This Waifu Does Not Exist." : https://www.gwern.net/TWDNE