In a program that I’m working on, I’m struggling to determine whether or not a given file within a mounted multifile exists. (Although it looks like actually reading such a file does work, presuming that it is present…)
Here follows a small example-program demonstrating the issue, as well as a simple multifile containing the text-file that the example-program looks for:
The program:
from direct.showbase.ShowBase import ShowBase
from panda3d.core import VirtualFileSystem, Filename
from direct.stdpy.file import *
import math
from panda3d import __version__ as pandaVersion
print (pandaVersion)
import sys
print (sys.version)
class Game(ShowBase):
def __init__(self):
ShowBase.__init__(self)
vfs = VirtualFileSystem.getGlobalPtr()
vfs.mount(Filename("./mew.mf"), Filename("./Mew"), VirtualFileSystem.MFReadOnly)
fileName = Filename("Mew/cat.txt")
print ("Does the file exist?", fileName.exists())
fileObj = open("Mew/cat.txt")
print("File contents:", fileObj.read())
fileObj.close()
app = Game()
app.run()
And the multifile (zipped, as the forum objects to “.mf” files):
Update: This has been answered by rdb on GitHub, and a bring the answer here for any who might in future encounter a similar issue:
In short, it turns out that the Filename class is unaware of the Virtual File System, and thus its version of the “exists” function doesn’t observe the contents of multifiles.
However, the version of the “exists” function that is found in the VirtualFileSystem class does observe the contents of multifiles, and can thus be used for this purpose.
Indeed, in the example-program above, replacing “fileName.exists()” with “vfs.exists(fileName)” does I find produce the desired output of “True”!