Gimp-Forum.net
Troubleshooting Python plugin to load an XCF file - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Troubleshooting Python plugin to load an XCF file (/Thread-Troubleshooting-Python-plugin-to-load-an-XCF-file)



Troubleshooting Python plugin to load an XCF file - TimorousMe - 03-19-2021

In connection with troubleshooting my bigger project (thread here: https://www.gimp-forum.net/Thread-Gimp-Python-script-file-load-question) I'm trying to create a really simple plugin to open an XCF file in the GUI. Eventually I will make this non-interactive and make it a wrapper to launch my other function, but for now I just want it to open the GUI and use pdb.gimp_xcf_load to open my XCF. 

I know definitions are not completely needed, but having them seems to make it easier to call a function from the command line, so I have a definition for now. 

The command line opens the Gimp UI, but doesn't open my XCF, and throws some errors. Any idea what I'm doing wrong here in this very simple plugin?

Command line argument:
Code:
gimp --batch-interpreter python-fu-eval -b '(python_fu_open_xcf "/Users/TimB/Desktop/template.xcf")'



Plugin:
Code:
#!/usr/bin/env python
#
# Script to open .XCF file

from gimpfu import *

def open_xcf(file):
   
   try:
       # Open XCF file
       image = pdb.gimp_xcf_load(file, file)
       layer = pdb.gimp_image_get_active_layer(image)

   except Exception as err:
       gimp.message("Unexpected error: " + str(err))

register(
   "python_fu_open_xcf",
   "Add image to layer",
   "Add image to layer and flatten.",
   "TCB",
   "TCB",
   "2021",
   "<Image>/Filters/Tim/Open XCF",
   "*",
   [
       (PF_FILE, "file", "XCF file to open", ""),
   ],
   [],
   open_xcf)

main()

Terminal output:
Code:
GIMP is started as MacOS application
2021-03-18 23:02:16.722 gimp[9241:136602] *** WARNING: Method userSpaceScaleFactor in class NSView is deprecated on 10.7 and later. It should not be used in new applications. Use convertRectToBacking: instead.
Cannot spawn a message bus without a machine-id: Unable to load /var/lib/dbus/machine-id or /etc/machine-id: Failed to open file “/var/lib/dbus/machine-id”: No such file or directory
/Applications/GIMP-2.10.app/Contents/MacOS/gimp: LibGimpBase-WARNING: gimp: gimp_wire_read(): error
gimp_check_updates_callback: loading of https://www.gimp.org/gimp_versions.json failed: Operation not supported
Traceback (most recent call last):
 File "/Applications/GIMP-2.10.app/Contents/Resources/lib/gimp/2.0/python/gimpfu.py", line 827, in _run
   return apply(func, params[1:])
 File "/Applications/GIMP-2.10.app/Contents/Resources/lib/gimp/2.0/plug-ins/python-eval.py", line 25, in code_eval
   exec code in globals()
 File "<string>", line 1
   (python_fu_open_xcf "/Users/TimB/Desktop/template.xcf")
                                                        ^
SyntaxError: invalid syntax
batch command experienced an execution error



RE: Troubleshooting Python plugin to load an XCF file - Ofnuts - 03-19-2021

  • The outer parentheses are not necessary (this is script-fu syntax....)
  • If you want to use your script via the registration, then it's called as pdb.python_fu_open_xcf("your file") (your script is added to the PDB, using the name specified as the first argument of the registration (a.k.a. "the "atom")). You can check it registered properly using the PDB function browser in the Python-fu console) 



RE: Troubleshooting Python plugin to load an XCF file - TimorousMe - 03-19-2021

(03-19-2021, 07:39 AM)Ofnuts Wrote:
  • The outer parentheses are not necessary (this is script-fu syntax....)
  • If you want to use your script via the registration, then it's called as pdb.python_fu_open_xcf("your file") (your script is added to the PDB, using the name specified as the first argument of the registration (a.k.a. "the "atom")). You can check it registered properly using the PDB function browser in the Python-fu console) 


Thanks for the command line syntax tips. That was helpful. When I try to run the plugin to open the XCF I get this message:

batch command experienced an execution error:
Error: eval: unbound variable: pdb.python_fu_open_xcf 

My plugin is not showing up in the PDB so there must be something wrong with it. I tried updating my code slightly, as shown below, to define drawable and to add the dummy parameter of 0 as suggested by the PDB, but that doesn't help and I get the same message.  

Code:
#!/usr/bin/env python
#
# Script to open .XCF file

from gimpfu import *

def open_xcf(file):
   image = pdb.gimp_xcf_load(0, file, file)
   drawable = pdb.gimp_image_get_active_layer(image)

register(
   "python_fu_open_xcf",
   "Add image to layer",
   "Add image to layer and flatten.",
   "TCB",
   "TCB",
   "2021",
   "<Image>/Filters/Tim/Open XCF",
   "*",
   [
       (PF_FILE, "file", "XCF file to open", ""),
   ],
   [],
   open_xcf)

main()



RE: Troubleshooting Python plugin to load an XCF file - Ofnuts - 03-19-2021

See the debug tips, in particular:

  1. Run the script in a terminal: python youscript.py. This won't run (ImportError: No module named gimpfu) but should report the more blatant syntax errors
  2. Add a big visible output line (print "***********************") at the top of your script
  3. Start Gimp in a terminal and
    • Check that you get the "*********************" line
    • Check for errors immediately after

This said, your plugin registers correctly for me and appears in the PDB and in the menus...

Code:
➤> [x for x in dir(pdb) if 'open_xcf' in x]
['python_fu_open_xcf']



RE: Troubleshooting Python plugin to load an XCF file - TimorousMe - 03-21-2021

(03-19-2021, 04:56 PM)Ofnuts Wrote: See the debug tips, in particular:

  1. Run the script in a terminal: python youscript.py. This won't run (ImportError: No module named gimpfu) but should report the more blatant syntax errors
  2. Add a big visible output line (print "***********************") at the top of your script
  3. Start Gimp in a terminal and
    • Check that you get the "*********************" line
    • Check for errors immediately after

This said, your plugin registers correctly for me and appears in the PDB and in the menus...

Code:
➤> [x for x in dir(pdb) if 'open_xcf' in x]
['python_fu_open_xcf']

This line ended up working in my script:
  • image = pdb.gimp_xcf_load(file, file)

This line I deleted: 
  • layer  = pdb.gimp_image_get_active_layer(image)

Thanks everyone.