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?
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It only takes a minute to sign up.Sign up to join this community
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)
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)