Access external scripts that are not in a children folder?

Hi,

Quick explanation
Let’s say I have a char.blend and want to use an external script called “init.py” (main) contained in a children folder “scripts”

Folder structure :
/mygame/chars/char.blend
/mygame/chars/scripts/init.py

I write “scripts.init.main” in the python controller and it works.

But now, I want to access init.py from another folder.

*************
Question


In the python controller, how to refer :to /mygame/chars/scripts/init.py
for /mygame/props/walls.blend?

Easy way would be to just include a copy of the script in that folder…

Maybe you could look into path manipulation… it’s built into python’s os.path.

http://docs.python.org/library/os.path.html

I’d guess that you’d need to somehow get the path of the current blend file, then manipulate it to get to the base directory. Can you just import the script into the blend file itself?

-Sam

Easy way would be to just include a copy of the script in that folder…
It’s been my temporary fix for few weeks but I want to improve it, now :slight_smile:

I’ll check the docs, thank you.
In fact I thought that it would just be a matter of syntax (…/…/scripts.init.py something like that).
But I don’t know what’s the allowed syntax for the Python controller Module Type).

In python you could import a module from a different folder using:
from … import chars.scripts.init.main

So one solution would be having the controller on walls.blend linking to a simple script that would execute that code.

Also the python module controller will look for files in every library linked in that .blend file. So if props/wall.blend is linking to chars/chars.blend. Then it will look into chars/ as well when looking for a module. It will look in the folder of every .blend file that is linked.

So another solution is to link from props.blend to char.blend then simply use scripts.init.main on the controller at props.blend.

I don’t know how to simply add a library link out of the blue. But you could simply shift+f1, look for the chars.blend and link to any text file there. This works, but is there a better method to add a library link?

if /mygame/chars/char.blend is a linked library youll be able to use “scripts.init.main” since pythons sys.path includes all linked libs.

Otherwise you can do…
import sys
sys.path.append("/mygame/chars/")

And then you can use “scripts.init.main” as well.

Would you know if there is something similar to sys.path for the python open() function? I’m using it to load a config file but I don’t know how to refer to that config file in /game/level/config.ini from the /game/char/char.blend file. So far I’m just doing it like in yoFrankie and using default config vars if I cannot find the config file. But it would be confortable to be able to open char.blend for testing and have it working with the current config.

Thank you all for your help.

is there a way to make that run before the controllers start importing modules?