reusing scripts

Hello.
I haven’t found a solution to this question on the internet.
I want to have generic functions and variables in separated scripts, not phisically separated, but in blender (no .py files), and call them when I need them.
¿how can I do this?

Please explain in more detail.

What do you mean by “no .py files”? The Blender Game Engine can only run python scripts, so whatever code you want to run will have to be in python, and also it will have to be linked in blender, to either an “always” or some other brick that will send a read pulse through.

Here, a tutorial on python in the BGE:

http://socialstorage.googlepages.com/beginningbgepy

With “no ,py files” I mean no python files, and with “no python files” I don’t mean “without using python”.
In a normal python project pieces of code are imported using the statement “from pythonScript import *”. but I want to have my python scripts embeded in the .blend file. In that case ¿how do I import them?

go to the text interface in blender there is a load button
then you have to connect it to a logic brick to run it

Hmm, never had the need to do that before, I just declare my functions in the blenderscript itself, I don’t see why you can’t do the same.

Anyway, take your module (because that’s what you need to have to import afaik), and stick it where your python installation modules reside.

I think blender should be able to recognize it from there, because it could always import from the python modules on the system.

I think he knows that already, he’s looking to import from his own module.

ya I should have noticed that, sorry.

Thanks!, that’s a solution :yes: … but it’s a pain, and not easily transferable to another computer :frowning:
What do you do when you have for example, functions that do stuff with vectors, like adding them or scaling them, and you want to use thous functions in multiple elements?
You could copy the text in each element in a controller and then expand each controller with what is supposed to do using the functions.
But what if you want to change the functions or add new ones?, or make global variables that every object should know, and these variables should be changeable in one part and affect behavior everywhere in different scripts.

so, ¿how do I get around of this?

1 Like

You can do this:


# Always sensor ---------> Script
# This is the main script where your common function/s would reside
# Global variables are assigned: "GameLogic.variablename"

g = GameLogic

def addition(a, b):
    c = a + b
    return c

g.add = addition(g.a_in, g.b_in)

Then you can do this in any other script:


g.a_in = 3
g.b_in = 2

print g.add # Should return 5

I think that should do fine for a few large functions, but for smaller functions you’re probably better off just copy pasting them.

You can build your own modules. They have to be *.py otherwise Python will not recognice them.
e.g.

from <module> import <function>

<module> is the file name without .py

The import statement must be placed before you call the function.
If the module is not file on disc but in text window, you will get en warning that it is loaded from text buffer.

Heh, it was that simple. You can just make modules in blenders text editor, and load them from buffer.

Right, so it’s:


# main.py
def addition(a, b):
    c = a + b
    return c

Then just like any other regular import:


import main

print main.addition(2, 3)

Thanks monster! I didn’t know that could be done like that.

I had tried with an import statement and it did´t work, now I tried again and it did…maybe a typo.

I like both solutions, I’ll be using them. Thanks