Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python-Fu - os.path not working
#5
(01-15-2024, 04:15 PM)BaconWizard17 Wrote: I was able to get the script to work, but now I have another issue: how do I properly return two variables from a script? I've been able to have one return variable work successfully in the past, but I'm having trouble with two. Here's the revised code:
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from gimpfu import*

def getPathSave(image, layer):
   filePath = pdb.gimp_image_get_filename(image)  
   pdb.gimp_file_save(image, layer, filePath, filePath)
   folderName = os.path.dirname(filePath)
   fileName = os.path.basename(filePath)
   pdb.gimp_message("folderName = " + str(folderName))
   pdb.gimp_message("fileName = " + str(fileName))
   return folderName, fileName

register(
   "python_fu_marvelmods_basic_get_path_save",
   "Saves the file and collects file path information. Returns the folder that the file is in, as well as its name (without the extension).",
   "Saves the file and collects file path information. Returns the folder that the file is in, as well as its name (without the extension).",
   "BaconWizard17",
   "BaconWizard17",
   "January 2024",
   "Get File Path and Save",
   "*",
   [
       (PF_IMAGE, "image", "Input image", None),
       (PF_DRAWABLE, "drawable", "Layer, mask, or channel", None)
   ],
   [
       (PF_STRING, "folderName", "The folder that the file is in"),
       (PF_STRING, "fileName", "The file name")
   ],
   getPathSave,
   menu='<Image>/Marvel Mods/Basic Procedures'
)

main()

The code works fine on its own, with no errors and the messages displaying appropriately in the error console. But when I call it from another script like so:
Code:
folderName, fileName = pdb.python_fu_marvelmods_basic_get_path_save(image, layer)

I get a "RuntimeError: execution error" from Gimp. The second script so far only contains that line of code within its main function.

I have also tried putting the variables into a list within both scripts:
Code:
[folderName, fileName]
which got me the same results. I've also tried running it with only one return variable, and it worked fine with returning either variable on its own. So I would assume that the issue is however I'm setting up the two return variables. The documentation doesn't cover this, so I'm at a loss.

Your code works:


➤> image=gimp.image_list()[0]
➤> pdb.python_fu_marvelmods_basic_get_path_save(image,image.active_layer)
('/tmp/screenshots', 'MapToSphere2.png')


You may be passing a bad image or layer argument, or working with an image that has no filename yet...

You can improve you code a bit by catching exceptions:

Code:
# -*- coding: utf-8 -*-

import os, traceback
from gimpfu import*

def getPathSave(image,layer):
    try:
        filePath = pdb.gimp_image_get_filename(image)  
        folderName = os.path.dirname(filePath)
        fileName = os.path.basename(filePath)
        pdb.gimp_message('folderName: "%s", filename: "%s"' % (folderName,fileName))
    except Exception as e:
        print e.args[0]
        gimp.message(e.args[0]+'\n'+traceback.format_exc())
        return None
    return folderName, fileName

desc="Image file info"
register(
    "python_fu_marvelmods_basic_get_path_save",
    desc,desc,
    "BaconWizard17",
    "BaconWizard17",
    "January 2024",
    desc,
    "*",
    [
        (PF_IMAGE, "image", "Input image", None),
        (PF_DRAWABLE, "drawable", "Layer, mask, or channel", None)
    ],
    [
        (PF_STRING, "folderName", "The folder that the file is in"),
        (PF_STRING, "fileName", "The file name")
    ],
    getPathSave,
    menu='<Image>/Test'
)

main()

Then when you call your script on a new image:


Image file info Warning
'NoneType' object has no attribute 'rfind'
Traceback (most recent call last):
  File "/home/me/Code/Gimp/Foreign/Activated/return2.py", line 10, in getPathSave
    folderName = os.path.dirname(filePath)
  File "/usr/lib/python2.7/posixpath.py", line 122, in dirname
    i = p.rfind('/') + 1
AttributeError: 'NoneType' object has no attribute 'rfind'

and since the only object of type NoneType is None itself this means that you got a None where you thought you had an object.

Btw, since your description states "as well as its name (without the extension)" you may want to use an os.path.splitext() somewhere.
Reply


Messages In This Thread
RE: Python-Fu - os.path not working - by Ofnuts - 01-11-2024, 08:47 AM
RE: Python-Fu - os.path not working - by Ofnuts - 01-15-2024, 10:03 PM

Forum Jump: