Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Try to transform a python script to go/back/and forth between Darktable and GIMP
#3
(01-31-2024, 09:08 AM)Ofnuts Wrote: The culprit code is like this:
Code:
 # put it as a new layer in the opened image
 try:
    darktablefile = os.path.join(tempfile.gettempdir(), "ShellOutDTTempFile.tif")    
    copyfile( darktablefile, tempfilename )
    newlayer2 = pdb.gimp_file_load_layer(tempimage, tempfilename)
 except:
   RuntimeError
    
 tempimage.add_layer(newlayer2,-1)


The message you have is clear: variable newlayer2 doesn't exist, and in Python this means it has never been assigned.

Since it seems to have been assigned in the few lines above, WTF?

Ok, I don't feel alone anymore to have thought "WTF it's here" Big Grin

(01-31-2024, 09:08 AM)Ofnuts Wrote: Notice that the lines above are bracketed by a try/except, so if anything bad happens in the first three lines, instead of stopping with an exception, the Python code jumps to the code after the except. So, something bad happened in the three lines, but where?

  • first line not too likely (join() is mostly about splicing string, it doesn't check if the name is valid)
  • copyfile is a candidate if the path created in the previous line is bad, or there are other systems problems (no enough space, write permissions)
  • pdb.gimp_file_load_layer is another candidate if the tempimage doesn't exist or if the file cannot be loaded for some reason (not an image file...)
And why does the code continue executing? It turns out that the code after the except does nothing. RuntimeException is the name of a class, if it is executed by the code, nothing happens (in the console if just prints information on the class). I believe it should have been raise RuntimeException.

But even then... this code has a major flaw: it replaces the exception raised by the copy or load steps (that could have explanatory messages), by an anonymous exception (that doesn't tell you anything). So  it is pointless... you can just as well remove it, this won't prevent errors but at least you will have a better idea of where and what they are.

Code:
 # put it as a new layer in the opened image
 darktablefile = os.path.join(tempfile.gettempdir(), "ShellOutDTTempFile.tif")    
 copyfile( darktablefile, tempfilename )
 newlayer2 = pdb.gimp_file_load_layer(tempimage, tempfilename)
 tempimage.add_layer(newlayer2,-1)

(although why you need to copy the file to a new file to load it is beyond me)

Also do yourself a favor and replace line #81 by
Code:
   raise RuntimeError('Cannot create temporary image')

And there are still a few weird bits left Big Grin

Thanks a lot Ofnuts to have take time to read that code, and help.
I'm gonna do the changes you suggest and see further what's going on,

Also maybe I need to search for the 3 previous version (0.7 to 0.8c) for comparison... I'll see if I get stuck (maybe it's too buggy)
Again thank you very much.
Patrice
Reply


Messages In This Thread
RE: Try to transform a python script to go/back/and forth between Darktable and GIMP - by PixLab - 02-01-2024, 02:33 AM

Forum Jump: