How to import and run external scripts?

Hello, I´m returning to blender GE after a lot of time, and now I have understood a little better python progamming, I would like to achieve some things without the use of logic bricks and such, I´m wanting to do it all in python programming. My current problem is the following:

I was thinking of adding only one script in blender called “main”; from this script I wanted to load and run other scripts, and unloads scripts which were not used anymore using python. These scripts would be contained in external files.

The problem is, how do I do that in blender GE? Also, do i need to be inside blender for its GE imports to work, or I could I simply open a blank document in, for example, Geany( an IDE ), and import blender GE stuff there, would it still work?

Thanks for any help

I read an old blender GE tutorial by social. It looks python in blender GE needs the logic bricks to work, and as such i can´t import blender GE stuff elsewhere. I´m still looking after the first question thought

You are right there is no pure python logic. If you need that I suggest to switch to Panda3d.

In Blender you can use the Python controller in module mode. With that you can call functions in that form:

<modulename>.<function>
e.g.
testmodule.main

calls:
main() from file testmodule.py

The file can be internal or external. The module search path includes the current folder. If both are present the external file will be used. So it is important to keep them synchronized (<ALT-S> in text window).

If you want to keep the scripts in one folder there are two options:

  1. place your file in the directory “scripts” (within the blend directory ). Add a python file “init.py” that imports all modules from that directory . Set the python module controller to “scripts.testmodule.main”

  2. expand the module search path by placing this code on top of a script:

import sys
scriptFolder = GameLogic.expandPath('//scripts')
if not scriptFolder in sys.path:
    sys.path.append(GameLogic.expandPath('//scripts'))

Make sure the script is executed before importing any other module from the scripts directory.

I hope it helps

1 Like

I have used Panda3d for some weeks, but to set up collision and to show correctly a simple 2d image was too much pain, blenderGE is my last hope of making a 3D game easily, here on Linux Ubuntu.

I will try to put what you said in pratice, thanks a lot for the help Monster!