Activating an action with python.

Hello.
It’s easy to activate an action from a controller with the BGE or Python, but how to activate actions if we cannot reach the controller ?

This script does not work and I do not know how to activate the actions:
an empty is connected to the following python module


# ***********************************************************************************************
def Eclairage():
    Salles = [["sol pieces.gauche",  "MursIntG",  "VouteEntree.gauche"]]       # Salle de droite, entrée
    Salles.append( ["sol pieces.droite" ,  "MursIntD",  "VouteEntree.droite"])  # Salle de gauche, entrée 
    # I could  add all the other rooms here... 
    # La scene 
    getScene = {}
    for a in G.getSceneList():
        getScene[a.name] = a         
    scn = getScene['05_MyScene']
    object = scn.objects   
    i=0
    
    while i<2:
         j=0
         SalleEnCours = Salles[i]         
         while j<3:
             MeshEnCours=SalleEnCours[j]             
             element = object [MeshEnCours]    # An object 
             k=element.actuators                    # it's actuator
             l = element.controllers                 # it's controller   
             l.activate(k,1)                             # error generated here the controller is the Python file and has no activate attribute                
             j=j+1
         i=i+1

I could remove hundreds of connections in my game engine .blend file.
And it would be very easy to add new meshes to this script to control the ligtning of many new rooms.

Thanks in advance.

I an not sure but I think this here is wrong:


k=element.actuators                    # it's actuatorl = element.controllers                 # it's controller   
l.activate(k,1)                             # error generated here the controller is the Python file and has no activate attribute 

because object.actuators is a list of actuators and object.controllers is a list of controllers.
Try taking just one controller like this:


cont = element.controllers[0] # 0 is just an index, you can use the name of the controller
for act in con.actuators:
    cont.activate(act)

This code controls lights? Interesting, I can’t see it.

In general you can and should activate connected actuators only.

If you want to interact with other game objects you can notify them by property or messages.

you can do this:
LightManager
The purpose is to switch lamps on and off. The LightManager knows how to switch the lights.
-> looks for any controllable light (e.g. lights with property “lamp”)
-> and interacts with them by setting the property “switch” to “on” or “off”

Each Light:
the purpose is to animate lamps on and off. The lamp knows how to do that.
-> has the property “lamp”
-> reads the property “switch”, if “on” -> animates light on, if “off” animates light off
this is even possible with one sensor, one controller and one action actuator.

Thanks, but

Josip, I get the error "object has no attribute ‘activate’ "
Probably only the controler has this ‘activate’ attribute, but I cannot use something like
contr = G.getCurrentController() because this refers to the empty that activates the Python module.

Monster, It’s not a light but walls, doors and ceilings that I darken with an action on the color curve made from 0% to 100 %
I’m using the SingleTexture option of the game.
I have applied that action curve to concerned objects, and when I press L, the room becomes dark/ clear, the outside does not change.
As number of rooms is important, I think python can do the job easily
This makes the scene more realistic. Without shadows of course but fast !
If I animate the lamp, the full layer is animated, not only the rooms, And i’m already using all layers.
Another reason, with only one image file applied as UvMap, I can use as many colors I want with that action. My project will be less memory consuming.

So you are animating the material, or object color?

what version of Blender are you using?

I’m using Blender 2.66, and yes, the color is animated, I’m using it as an Action.
from black to white, to black using 125 frames.
I can call what I need: frame 20 to 75 gives an illusion of a lamp growing in intensity, and 75 to 125 -> dark again smoothly… I like the effect: it’s like a candle light as used in ancient times.

Logic bricks do work, but I’ve only animated 3 rooms !
There are exactly 127 rooms in the (ancient) place I’m trying to model. This will be very heavy with logic bricks using the same action.

Solved:

while i<2: j=0 SalleEnCours = Salles[i] while j<3: MeshEnCours=SalleEnCours[j] element = object [MeshEnCours] # An object element.playAction(“yyCourbeCouleur”, 4, 5) j=j+1 i=i+1

Answer was found reading the Blender API.

Again, thank you for the answers.