Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gimp Python Fu Problems
#1
I'm interested in accessing the plugins from the Procedural Database outside of a registered function and am wondering if this is possible and if it isn't, why? Thanks.
Reply
#2
Yes, you can, but the plugin has to be called from Gimp.  If you are using the script in batch mode you don't need to register anything. See this example.
Reply
#3
To explain my problem more clearly. This is just a example.
Code:
register(
    "abc",
    "",
    "",
    "Andrew Sears",
    "Andrew Sears",
    "2018",
    "abc",
    "",
    [
    (PF_IMAGE,"CurrentImage","Image", None),
    (PF_DRAWABLE, "Drawable", "Input drawable", None),
    (PF_OPTION, "Selector", "", 0, [""])
    ],
    [
    (PF_OPTION, "Selection", "")
    ],
    abc,
    menu="<Image>/Filters",
    domain=None,
    on_query=None,
    on_run=None
)

main()

with (open("Procedure List.txt", "a+")) as Text:
    Text.write(str(dir(gimp.pdb)))   
                                  

From what I understand gimp goes over this at startup and registers the function and runs anything else. What i'm interested in is accessing the procedure database at the end of the file when gimp first runs , but when I try to access pdb anywhere outside of my registered function within the plugin, the plugin crashes. I could have a secondary procedure that is called by this script and does this for me, but I would rather have a different solution. Hope this helps helps explain more clearly what i'm trying to achieve.
Reply
#4
On my system, there is no crash but the code hangs indefinitely in the dir(pdb) call. Note that the PDB doesn't really exists until all the scripts and plugins have been registered, which isn't the case when your code runs.

In addition, the whole Gimp environment may not be complete at that point since Gimp itself is initializing.

What are you trying to achieve?

Edit: Changing the code to this:

Code:
def hook(name):
    print "Running the hook..."
    print dir(pdb)
    return name

register(
    hook(abc),
    "",
    "",
    "Andrew Sears",
    "Andrew Sears",
    "2018",

to sneak a function inside the register() code elicits:

Quote:(process:14143): LibGimpBase-ERROR **: gimp_wire_write_msg: the wire protocol has not been initialized

so obviously you can't expect complete Gimp functionality here.
Reply
#5
(09-15-2018, 08:12 PM)Ofnuts Wrote: On my system, there is no crash but the code hangs indefinitely in the dir(pdb) call. Note that the PDB doesn't really exists until all the scripts and plugins have been registered, which isn't the case when your code runs.

In addition, the whole Gimp environment may not be complete at that point since Gimp itself is initializing.

What are you trying to achieve? I

Edit: Changing the code to this:

Code:
def hook(name):
    print "Running the hook..."
    print dir(pdb)
    return name

register(
    hook(abc),
    "",
    "",
    "Andrew Sears",
    "Andrew Sears",
    "2018",

to sneak a function inside the register() code elicits:

Quote:(process:14143): LibGimpBase-ERROR **: gimp_wire_write_msg: the wire protocol has not been initialized

so obviously you can't expect complete Gimp functionality here.


I would like to be able to gather all procedure names before the plugin runs so that I can have a option widget for the plugin gui which lists procedures which you can run.
Reply
#6
What prevents you from gathering the data while building the dialog? Keep in mind that each execution of the plugin is going to be a new process, so you cannot keep this across calls; each time the plugin is started, it has to go through that code again.

In any case you don't need a secondary procedure, if you want to keep the code distinct, it just has to be in a Python function.

It seems possible to run the PDB scan in the __main__ if you find a way to distinguish the registration execution from the plugin calls and avoid running the code in the first case. There lay be a way with the more explicit registration functions, but IMHO this is going to great lengths to do something which isn't such a good idea.
Reply
#7
(09-15-2018, 09:38 PM)Ofnuts Wrote: What prevents you from gathering the data while building the dialog? Keep in mind  that each execution of the plugin is going to be a new process, so you cannot keep this across calls; each time the plugin is started, it has to go through that code again.

In any case you don't need a secondary procedure, if you want to keep the code distinct, it just has to be in a Python function.

It seems possible to run the PDB scan in the __main__  if you find a way to distinguish the registration execution from the plugin calls and avoid running the code in the first case. There lay be a way with the more explicit registration functions, but IMHO this is going to great lengths to do something which isn't such a good idea.

If your referring to the register function, when you ask what prevents me from gathering procedure names when building the dialog, I lack the knowledge about gimp and python to do such a thing. If your interested, could you fill some pot holes in my knowledge for me? If not, ignore the rest of this. As far as I can tell from testing, when gimp first loads it goes through all plugins and only calls functions which are written in the script such as Im_A_Function(). Because of this, from what I understand If I define a function and don't call it in the script, gimp wont run it. That being the case, I don't see why my code has any issues with unnecessary calls of functions. But what I am unsure of is that if I break a script up and place smaller portions of the code in different py files, would this be faster and considered proper technique for optimization? As for not having a second procedure, from attempting to access pdb outside of a registered function in my case my plugin always crashes, and so having a second file with pdb procedures in it seems impossible, am I misunderstanding something? Finally, it does seem what i'm attempting to do is complicated and difficult unfortunately, would you have recommendations for simpler ways of generating all procedure names? Sorry for long paragraph.
Reply
#8
(09-16-2018, 06:54 PM)ajs52698 Wrote: If your referring to the register function, when you ask what prevents me from gathering procedure names when building the dialog, I lack the knowledge about gimp and python to do such a thing. If your interested, could you fill some pot holes in my knowledge for me? If not, ignore the rest of this. As far as I can tell from testing, when gimp first loads it goes through all plugins and only calls functions which are written in the script such as Im_A_Function(). Because of this, from what I understand If I define a function and don't call it in the script, gimp wont run it. That being the case, I don't see why my code has any issues with unnecessary calls of functions. But what I am unsure of is that if I break a script up and place smaller portions of the code in different py files, would this be faster and considered proper technique for optimization?

No it wouldn't. Breaking up things will not make them run faster.

(09-16-2018, 06:54 PM)ajs52698 Wrote: As for not having a second procedure, from attempting to access pdb outside of a registered function in my case my plugin always crashes, and so having a second file with pdb procedures in it seems impossible, am I misunderstanding something?

The problem isn't accessing the PDB outside of a registered function. It is accessing the PDB outside of a plugin call context. The plugin entry point can call other functions that can themselves all the Gimp API without restriction. But if you put your code in the _main__ it is executed outside of the plugin context during registration.

(09-16-2018, 06:54 PM)ajs52698 Wrote: Finally, it does seem what i'm attempting to do is complicated and difficult unfortunately, would you have recommendations for simpler ways of generating all procedure names? Sorry for long paragraph.

You can call `dir(pdb)| in your plugin code:
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *

def dumpPDB():
   entryPoints=[ep for ep in dir(pdb) if not ep.startswith('__')]
   gimp.message('Gimp PDB contains %s entry points:\n%s' % (len(entryPoints),',\n'.join(entryPoints)))

def testPDB():
   dumpPDB()
   
name='Dump PDB'
register('test-pdb',name,name,'','','',name,'',[],[],testPDB,menu='<Image>/Test')

main()
Reply


Forum Jump: