Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threading / Loops in Python Plugin
#1
Hello all.

I have been using Gimp for work for a while now, but never got into building plugins for it - though I do code Python for other programs and plugins.  Sorry for this long post, but it is complicated (to me at least)

Want I am trying to do is to integrate Gimp into my graphics pipeline.  This entails making a plugin so that my outside pipeline software can communicate and execute some simple Gimp functions such as .xcf save and file export.  And inside Gimp, I would like my menu items to be able to execute functions in my pipeline software such as call other scripts.

I understand Gimp 2.10 uses Python 2.7, but I need to use Py3.9 for my external software .  I have managed to insert a menu and items into Gimp, and can get it to execute functions--uphill battle but woohoo!

The issue is I need the Gimp plugin to communicate with my pipeline.  So the first issue I have is the Py mismatch.  My solution was to use a bridge script, and it works statically.  Basically, Gimp loads the plugin, and the plugin launches the bridge script in a Py3.9 process and I am able to execute functions both ways.  But I need dynamic comms between the two.  I tried sockets but could not get that to work in Gimp.  So I then fell back to the old .json file, but I need a non-blocking way to check for the command file inside Gimp.  I then tried using a thread for the checking loop which works, but that thread cannot then execute anything in the main Gimp thread (at least that is where I am at).

So the question is:  can I get it so Gimp will have comms with an outside script?  Is there a way to have a thread launched under the Gimp plugin to be able to execute in the main Gimp thread?

Hopefully that makes sense, and I can provide code if needed.  I did not include it since it is not working anyway . . . 

Thanks in advance,

JBreckeen. Huh
Reply
#2
As far as my experience goes, there is no obligation for a plugin to return, especially if it invoked without any indication of a specific image (ie, no image as argument). So you can launch a plugin from Gimp that will wait in the background (it is another process, after all). But my plugin that does this doesn't issue any commands against existing images (so can't tell what happens in this case), and you would also have to identify the images.
Reply
#3
I came across something interesting regarding coms using python2.7.18.

There are 2 versions of socket.py and ssl.py.

I saw this when I was putting the python2 appimages together. These files cannot 'phone home' because that was turned-off for security reasons. I used the versions that could not communicate because I didn't want to open a back door into Gimp.
Reply
#4
(01-05-2024, 09:34 PM)Tas_mania Wrote: I came across something interesting regarding coms using python2.7.18.

There are 2 versions of socket.py and ssl.py.

I saw this when I was putting the python2 appimages together. These files cannot 'phone home' because that was turned-off for security reasons. I used the versions that could not communicate because I didn't want to open a back door into Gimp.

On my Ubuntu I use the native ssl.py and it uses the try/except.

Note sure why you want to prevent a backdoor... the Gimp scripts are open source, it is not hard to see them doing things) while possibly exposing your user to vulnerabilities that have been fixed?
Reply
#5
Thanks ofnuts. This looks like the last python2 here.


I found this in the 'News'.

bpo-1676823: Added create_connection() to socket.py, which may be called
  with a timeout, and use it from httplib (whose HTTPConnection and
  HTTPSConnection now accept an optional timeout).

Maybe an earlier socket.py didn't have this and that is what is missing. I thought python developers didn't want to create a connection. Most Gimp users don't need a python plugin net connection.
Reply
#6
(01-06-2024, 02:43 AM)Tas_mania Wrote: Most Gimp users don't need a python plugin net connection.

My most used plugin (several times a day..., 7/7), uses a network connection. It uploads the current image to Imgur. Sharing images on the net is a very frequent activity.
Reply
#7
Thanks Ofnuts, looking for socket.py and ssl.py
I looked here:
https://synocommunity.com/package/python

but they seem the same as the files I have.
Do you have a better link or would I compile them?
Reply
#8
(01-06-2024, 10:48 AM)Tas_mania Wrote: Thanks Ofnuts, looking for socket.py and ssl.py
I looked here:
https://synocommunity.com/package/python

but they seem the same as the files I have.
Do you have a better link or would I compile them?

No link, I use the one installed with my Python 2.7 (unless it was dragged in by another package like requests:


$ll /usr/lib/python2.7/ssl.py
-rw-r--r-- 1 root root 37455 Jul  1  2022 /usr/lib/python2.7/ssl.py
$wc -l /usr/lib/python2.7/ssl.py
1035 /usr/lib/python2.7/ssl.py
$sha1sum /usr/lib/python2.7/ssl.py
11a5a0e290925f6e1cf59749d8d90e9f77b29b7c  /usr/lib/python2.7/ssl.py
Reply
#9
Thanks Ofnuts. I checked the files in my python2 drop-in pack with
ls -l ./ssl.py
I got the same date stamp as yours. (July 1 2022) so all good.
Reply
#10
Thanks for the discussion.

I stepped away from this plugin for a bit - had to do some work.  I have gotten the "outbound" part of my integration working by having the Gimp plugin write a command .json and have my bridge script pick it up and do its thing.

But the issue I am having is trying to get the "looping" script to be able to execute an inbound command in Gimp.  The simple version would be that I want to have the Gimp plugin (running under Gimp's py2.7) constantly checking the command directory for a .json, and when it finds one it executes something such as

 pdb.gimp_file_save(image, drawable, savePath, savePath).




If I setup a static function that checks the directory once during the plugin load, it works.  But as soon as I put in checking loop (which is blocking of course), then Gimp will not continue the load.  So then I move the checking loop to a thread, but whenever I try to connect the threads, I get a crash.  I believe it comes from the two processes being in separate threads.



So what I really need is to have some way that the checking loop can execute the Gimp commands in Gimp's thread.



Hope that makes sense,





JBreckeen.
Reply


Forum Jump: