1
$\begingroup$

Can I assign a module to an empty or an object in my scene and when I run it to be able to define all the scenes, all their objects, all their sensors, their controllers and their actuators?

If yes, how? and if not, why?

thanks.

$\endgroup$
2
  • $\begingroup$ do you want to just access them ? $\endgroup$
    – Chebhou
    May 24, 2015 at 22:24
  • $\begingroup$ I wanted to set them all to variables so I can access them in any given time. And make it as automated as possible.I managed it not exactly as I originaly thought(I did not set the script to a cube). Gonna post now. $\endgroup$
    – Lev
    May 24, 2015 at 23:19

2 Answers 2

1
$\begingroup$

The following code will list all loaded scenes, objects and thier sensors; controllers and actuators :

scenes = bge.logic.getSceneList()
for scene in scenes :
    print("scene : %s"%scene.name)
    for obj in scene.objects :
        print("   object : %s"%obj.name)
        for sensor in obj.sensors :
            print("      sensor : %s"%sensor.name)  
        for cont in obj.controllers :
            print("      controller : %s"%cont.name)      
        for actu in obj.actuators :
            print("      actuator : %s"%actu.name) 
$\endgroup$
1
$\begingroup$
import bge
import bpy
import sys

def initialization(name):
    """
    The function "initialization" is responsible for:
    1.Loading all scenes.
    2.Setting all scene objects, object sensors and object actuators to variables with corresponding(where the dot "." is replaced with "_").
    These variables are created into the function but are for global use. 
    3.The variable "cont" is created with its content being the controller.
    """ 
    sys.modules[name].cont = bge.logic.getCurrentController()
    for s in bpy.data.scenes.keys():
        bge.logic.addScene(s)
    for scene in bge.logic.getSceneList():
        for obj in scene.objects:
            exec("sys.modules[\"%s\"].%s=scene.objects[\"%s\"]"%(name,str(obj).replace(".","_"),obj))
            for sensor in obj.sensors:
                exec("sys.modules[\"%s\"].%s_%s= obj.sensors[\"%s\"]"%(name,str(obj).replace(".","_"),str(sensor).replace(".","_"),sensor))
    for act in sys.modules[name].cont.actuators:
        exec("sys.modules[\"%s\"].%s=sys.modules[\"%s\"].cont.actuators[\"%s\"]"%(name,str(act).replace(".","_"),name,act))

So,this is a module(with the name "Initialization.py" that I have saved in the same folder with the other modules I use and the .blend file. At the start of each module I want to use I just write this:

import bge
from initialization import initialization
initialization(__name__)

Now every sensor,object,etc. that I mentioned above can be retrieved and used.

Edit: Ignore these lines :

for s in bpy.data.scenes.keys():
    bge.logic.addScene(s)

since they won't work in standalone game.On the other hand if you have more than 1 scenes, use bge.logic.addScene(name, overlay=1)

$\endgroup$
3
  • $\begingroup$ is it obj or obj.name also the other lines $\endgroup$
    – Chebhou
    May 25, 2015 at 0:13
  • $\begingroup$ Since the obj variable is used where a string is expected, it automatically returns a string representation of its name.The same goes for act and sensor variables. $\endgroup$
    – Lev
    May 25, 2015 at 0:34
  • $\begingroup$ "Bpy is the blender api. It will not available when you run the blenderplayer without blender." a quote from Monster of blenderartists.I 'll try to find another way or change it to manual, only for the scenes. $\endgroup$
    – Lev
    May 25, 2015 at 11:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .