Python Folder Linking - very easy access to your python folders

Hi Blenderheads,

Monster discovered a new method to easily access the python folders without additional logic or additional code. At least it is new to me ;).

You know you can access all python modules that reside in the same folder as the blend the game was started with. This might be fine for small projects.

For a professional look the python files should live in one or more separate folders and not the main folder of the game.

Unfortunately the Python controller does not necessarily find these files without a view tricks. Monsters preferred method to do that was to update the Python search path via Python (see Prof. Monster’s script folder organizer).

There is a better method to do that:

Python Folder Linking

Indeed when you link to another file the location of this file is added to the Python search path by the BGE. You do not need additional logic nor any code for it. The BGE does it for you. It works since 2.60.
The best: You do not even need the linked data in your scene.
It is sufficient enough to

  • link a group without instantiating
  • place a linked object to an inactive scene.

Helper

To make this even more easier to you Monster created the LinkedPythonFolder.blend for you

  • copy it into your Python folder (where the .py files reside)
  • link the group LinkedPythonFolder to your own blend file.

Now you can access all modules from this folder.

before distributing your files to another PC/location ensure

  • all paths are relative
  • linked files are delivered

Attachments

LinkedPythonFolder.blend (74.8 KB)

1 Like

To show you how you can do this I prepared a small demo for you. It is a zip file as it contains multiple files. Thanks to Josip Kladaric for his cute little red Alien.

Download the zip.
Unpack it to any folder you like.
Navigate into “My game”.
Start start.blend with the blenderplayer or from within Blender.

After the loading screen the game switches to the MyLevel.blend.
You will see a screen similar to this:


It shows you that only the RedAlien module is loaded. This is because the other modules (residing in other folders) are not linked.

Structure
Lets look at the game’s structure:


This is an example. You can use whatever structure you think is best for you.
Here I added three folders to keep Python files:

  • in game folder to keep common python modules
  • in myLibrary to keep modules specific for this library
  • in littleRedAlien to keep modules of the alien

In each of these folders I placed a copy of LinkedPythonFolder.blend. This means the files are redundant but they are pretty tiny too.
Now I can link from any location to these files. I recommend to link the group from it.

Have a look at “My game\characters\litteRedAlien\LittleRedAlien.blend”:
You will see this file knows about the “Common module” too. This folder is linked to LittleRedAlien.blend by linking an object.
But this object is not part of a group and not linked to MyLevel.blend. Therefore LittleRedAlien.blend knows about but not MyLevel.blend

To avoid “missing” Python simply include an instance of the LinkedPythonFolder group in your group.

Hint:
you can even link to Python folders outside of your project. Just make sure to correct this before publishing the game.

Exercise:
Make the other two modules available to MyLevel.blend.
You can do this with a direct link as described above or
with an indirect link by including a link into the linked alien groups.

I hope you like it
Monster

Attachments

My game.zip (629 KB)

Thanks! Just what I was looking for.

Thanks! This is a more straight forward and less messy way to get things done(at least to me).

Thanks !
Very usefull, useless to set path with module and sub-module.

Thank you for sharing this methode! Just what I was looking and it works really great! :slight_smile:
Awesome!

Hi Monster, the link is broken [https://blenderartists.org/forum/showthread.php?196543](http://Prof. Monster’s script folder organizer)

Just create a .blend file with a group in it (put some random empty and add it to a group). Then all you have to do is link any of your .blend files to this group and the paths will be patched like Monster described.

thanks a lot

Hi Mauzz,

I just updated the link.

CU

thanks a lot Mr. @Monster