Write PNMImage with full path

I’m having a bit of a problem.

I want to write a image to the disc at a specific directory and I want it to work just the same after packing it to a .p3d file.

import os
from panda3d.core import Filename
from direct.showbase.AppRunnerGlobal import appRunner
if appRunner: 
    path=Filename.fromOsSpecific(appRunner.p3dFilename.getDirname())
else: 
    path=Filename.fromOsSpecific(os.getcwd())
from panda3d.core import *

saveName="/maps/map/"
p=PNMImage(512, 512)
p.write(Filename(path+saveName+"test.png").toOsSpecific())        #no output
file(Filename(path+saveName+"test.txt").toOsSpecific(), 'w')        #txt file made

if I skip the “path” part

p.write(Filename(saveName+"test.png").toOsSpecific())

a image is saved… but it will be saved in the wrong place after packing it to a .p3d file.

In other words:
I have a valid path e.g: path=Filename("/c/MyGame/maps/map/")
I have a valid image e.g: image=PNMImage(512, 512)
Why can’t I save my image at my path?

What am I doing wrong this time? …or is this a bug?

I imagine that the problem is likely a matter of what directory the program considers to be its “working” or “current” directory, and that all saves are being made relative to that – perhaps the “fromOsSpecific” call is ending up with something like “//”.

Have you tried, both with and without the “path” element, putting your Filename objects – those used in the actual saving operation – in to a temporary variable and printing out the result of “getFullPath”, something like this:

fName = Filename(path+saveName+"test.png") # or Filename(saveName+"test.png") -- sans "path"
osSpecificName = fName.toOsSpecific()
print fName
print osSpecificName
p.write(osSpecificName)        #no output

Where is the image ending up when you save from a P3D?

I’ve yet to find the file saved from a .p3d file :mrgreen:
The text file is where it should be.
writing just:

 
p.write(Filename("test2.png").toOsSpecific()) 

pops the file into \Documents and Settings…\Local Settings\Application Data\Panda3D\start\

this:

p.write(appRunner.p3dFilename.getDirname()+"\\test5.png")

writes test5.png into the folder where the .p3d file was lunched (on windows )

this:

p.write(appRunner.p3dFilename.getDirname()+"\\maps\\test6.png")

…did nothing (no file saved)

finally this worked:

p.write(appRunner.p3dFilename.getDirname()+"/maps/test6.png")

printing the Filename gives a perfectly fine panda3d filename string i.e: “/c/test_file/maps/map” or"/c/test_file/maps/map/test.png"

Ah wait, I think that I see: the problem was that you had “toOsSpecific” in a file-writing call offered by Panda3D, which was presumably expecting Panda3D’s directory format – forward-slashes between directories – and not the format used by Windows – back-slashes between directories.

Well, one way or another, you seem to have fixed the problem, I’m glad to see. :slight_smile: