prc model directories

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:

game/
    models/
    2d/
    audio/
    other/
    src/

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?
I thought

model-path    $MAIN_DIR/../models

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:

print ExecutionEnvironment.getEnvironmentVariable("MAIN_DIR")

Or this to see what the model-path ends up getting set to:

print getModelPath()

I understand that, Im asking how to get to the parent folder of the MAIN_DIR?

Exactly as you did.

$MAIN_DIR/../

?
No way :astonished:

Yes way.

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."""
		
		return self.part[name].node().getFullpath().getDirname();

self.part[] contains a list of models.

filename = getModelPath().findFile('text/textfile.txt')
file = open(filename.toOsSpecific(), 'r')

nice

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[0], "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[0] 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[0]).

(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:

loader.loadModel("$MAIN_DIR/models/blah")

$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')

Interesting, thanks.

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.