I want to add info to the Config file so I wont have to set the full model path each time.
My game folder structure is basically like this:
the scripts are in the src folder and i want to load models from ‘models’ folder, which is not it’s subfolder.
How can you set this in panda?
would work, as it would go to the parent folder of the main.py file folder (’/…’) and goo to the model subfolder of that folder. But it doesnt work
$MAIN_DIR expands to the location of the main python file that is invoked. So if you run it like “python main.py”, then $MAIN_DIR is the directory containing main.py.
You can always use this to see what it is set to:
Or this to see what the model-path ends up getting set to:
I understand that, Im asking how to get to the parent folder of the MAIN_DIR?
I just tried that before posting.
ill see if i can find the problem
You can start to find out by trying the commands I sent you in my first reply.
Okay, i found out the problem.
I load a text file with standard library,
file = open('text/textfile.txt', 'r')
the prc file of course affects only panda modules. I wonder what to do for non-panda loaded file’s paths.
I use the following code to open files in the same directory as the model:
def GetDirPath(self, name='hull'):
"""Returns the directory path the model was loaded from."""
self.part contains a list of models.
filename = getModelPath().findFile('text/textfile.txt')
file = open(filename.toOsSpecific(), 'r')
The method I’m loading my configs is independent of model-path. It looks up the file you have invoked to start your app:
file_path = os.path.abspath(os.path.join(sys.path, "etc/some_file"))
WARNING: this is rather unflexible and most probably won’t work from p3d’s. I only wanted to show an alternative.
sys.path will only work in a limited number of environments, that excludes the .p3d environment. It’s much more reliable to use os.path.dirname(sys.argv).
(Furthermore, using forward slashes won’t work on Windows, so you’ll either have to use os.path.join or use Panda’s path mechanism.)
But it’s even more reliable to use ExecutionEnvironment.getEnvironmentVariable(“MAIN_DIR”),or load models like this:
$MAIN_DIR is automatically set to the location of the main.py file by Panda3D.
If you want to use it for a non-Panda function, you can always use:
from panda3d.core import ExecutionEnvironment as EE
f = Filename(EE.expandString("$MAIN_DIR/etc/some_file"))
handle = open(f.toOsSpecific(), 'r')
And thanks for mentioning the slash-backslash issue. I just noticed that it’s the module I give the path, which accepts mixed case paths.