Editing Lamp Properties with Python in Game Engine

I am trying to cast spotlights with the same spot size towards the same moving object. This requires modifying both the Distance variable and the Spot Size variable of each of the spotlights in real time. It’s very easy to do this with bpy calls, but these don’t affect the properties in real time. How does one go about changing these properties in real time?

Included is a .blend showing some spot lights tracking an object with my script:

import bpy
import bge
import mathutils
import math

co = bge.logic.getCurrentController()
obj = co.owner

track = co.actuators["Track"]
trackobj = track.object

objpos = obj.position
trackobjpos = trackobj.position

colorvec = mathutils.Vector((1,1,1))


dist = obj.getDistanceTo(trackobj)

# lampobj = bpy.data.lamps.get(obj.name)
# lampobj.distance = dist
# ^ How do I do this in real time in the game engine?

spotRadius = 1
ang = math.atan(spotRadius/dist)
# lampobj.spot_size = ang


How can I apply the commented out code in real time?

Also, as you can see in my script, I am drawing lines from the lights to the object, however if you run the game engine these lines only appear for a split second at the beginning, why is this?

Another quick question, is there any light source besides spot light that will do real time shadows?


TrackLightingExample.blend (389 KB)

If the object is a light object, you have access to these methods and properties:


Okay, that was simple enough, not sure how I missed that earlier. That said, it still doesn’t seem to be working, no matter how I change the spot radius it doesn’t seem to actually affect anything. Also, my lines are only drawing for the first instant, then they disappear. Shouldn’t they be drawing constantly?

Are you sure you’re running the script every frame? Also, the attribute is spotsize, not spot_size.
EDIT: Also, Spotlights are the only lights with GLSL shadows, currently.

It’s set to an always sensor. How do I check if I am? I’ve kinda been trying to run before walking with blender so there’s probably a lot of basics I have missed.

For the game lamp object yes, but for the bpy lamp object no. Not sure why.

Make sure that the always sensor’s Pulse setting (f:0) is set to 0 (to run every frame, not skipping any in-between), and that the true pulse button [***] is set.

Also, you probably shouldn’t mix Blender’s python library (bpy) and the BGE’s library (bge), as the bpy module is only for use in Blender (it won’t transfer when you make a runtime executable).

Oddly, I tried to change the lamps’ properties myself, but for some reason, changing lamp properties doesn’t work anymore…? Something in the recent 2.5 update must have broken that feature. Someone needs to submit a bug report… Anyway, here’s the fixed blend file.

TrackLightingExampleFixed.blend (309 KB)