Dynamic Light Manager

Here is my script for dynamic Light Manager! You now can support up to 10 Lamps! Spot or Point!.. Now in vrs3 u can add sun lamps!

One thing is a problem though… I need help optimizing it… A lot!!!(hint hint wink wink…BluePrintRandom;))
If you can It would Really Help!

I think this could be a great asset to the blender community! If you do improve it I’ll credit you in the script.
Thanks in advance!
DLM.blend (658 KB)
DLM2.blend (663 KB) this has a few improvements… Manly not add extra "dead"lamps to the scene… There for chewing up frames…
DLM3.blend (577 KB) vrs 3 redone code… A lot easer on the computer than be for… Still has some bugs…look at the note.text

Also to note there seems to be a bug were you try to change a lamp type and it won’t Change… Example lamp.type = 0 this should Chang it to spot but it doesn’t…

OK! It looks useful as it uses kinda camera frustum replication to calculate light culling, however - if I get very closer to light, it disappears although it should be still visible. My reccomendation is to move the frustum like 5 or 10 meters behind player;)

Well that isn’t the problem… In the script I have the reverse of the hit list… So first in last out… But I need to make it work with distance not first in and last out… Is there a way to test if an object is in the camera frustum?

I am not sure. I will try to make a bit different workflow.

just parent a cone to the camera.

if the object is in collision.hitObjectList:
its in the camera?

Well that’s what I did with the radar… But I was wondering if I call the camera frustum can I see if something is inside of it?..

There is pointInsideFrustum that can check if a point is within the camera frustum. I use it like this in my project, go ahead and tweak it as needed;


from bge import logic

def checkPoint()
    scene = logic.getCurrentScene()
    cam = scene.active_camera

        if cam.pointInsideFrustum(#put your points here):
            #do something here

:smiley: Good call :smiley:

Still need help on optimizing this any help is greatly welcomed

import bge

cont = bge.logic.getCurrentController()

Manager=cont.owner

if 'LightList' not in own:
    Lamps=[]
    Hooks =[]
    for objects in bge.logic.getCurrentScene().objects_inactive.
        if 'Lamp' in object:
            Lamps.append(objects)
    own['LightList']=Lamps
    for objects in scene.objects:
         if ' hook ' in objects:
             Hooks.append(objects)
       
else:

is what I have so far, I’ll take a look,at finishing it and having my team use it and test/refine it.

Got it!

here is the working system, not fully featured yet but it works great!

Attachments

LightingManagerTake3.blend (822 KB)

Ok update - this version has a Spot lamp list, and Spot lamp hooks, as a separate list.

apparently lamp.type is broken in 2.75a , so setting it does nothing,

this is a work around for that bug.

Attachments

LightingManagerTake4.blend (922 KB)

Yea I found out about that but also… It sucks.

This isn’t directly related, but I have a script/blender addon stored away on my computer that converts lights in a scene into empties containing various properties. Here’s the script:

#This is a blender addon that will convert lights into empties and visa-versa
#It will store the information about the lights in game properties, so they
#Can be used by a game engine light managing script.
#
#
# Written by sdfgeoff
# Updated: 14-02-2015
# Script Version: 2.0

import bpy

scene = bpy.context.scene
print('------------------ New Runthrough --------------------')
def create_string_property(ob, prop, value):
    override = {'active_object': ob}
    #bpy.ops.object.game_property_new(override, type='FLOAT', name="my_float")
    bpy.ops.object.game_property_new(override, type='STRING', name=prop)
    prop = ob.game.properties[prop]
    prop.value = value

def create_float_property(ob, prop, value):
    override = {'active_object': ob}
    bpy.ops.object.game_property_new(override, type='FLOAT', name=prop)
    prop = ob.game.properties[prop]
    prop.value = value
    
def get_property_value(ob, prop):
    '''Returns a properties value'''
    prop = ob.game.properties[prop]
    value = prop.value
    return value

def create_empty(lam):
    '''Creates an empty with all the properties of the lamp lam'''
    #print(dir(lam.data))
    #bpy.ops.object.add( type='EMPTY', location=lam.location)        
    #ob = bpy.context.object
    name = lam.name
    lam.name = 'Wastenot'
    ob = bpy.data.objects.new(name, None)
    
    ob.location = lam.location
    ob.rotation_euler = lam.rotation_euler
    ob.parent = lam.parent
    
    
    create_string_property(ob, 'Managed Light', '')
    create_string_property(ob, 'type', lam.data.type)
    create_string_property(ob, 'light color', str(list(lam.data.color)))
    create_float_property(ob, 'energy', lam.data.energy)
    create_float_property(ob, 'distance', lam.data.distance)
    create_float_property(ob, 'lin_attenuation', lam.data.linear_attenuation)
    create_float_property(ob, 'quad_attenuation', lam.data.quadratic_attenuation)
    
    if lam.data.type == 'SPOT':
        create_float_property(ob, 'spotsize', lam.data.spot_size)
        create_float_property(ob, 'spotblend', lam.data.spot_blend)
        #xdelete_object(lam)
    bpy.context.scene.objects.link(ob)
    
    
def create_lamp(emp):
    '''Creates a lamp with all the properties of the empty emp'''
    name = emp.name
    emp.name = 'Wastenot'
    data = bpy.data.lamps.new("la_" + name,get_property_value(emp, 'type'))
    ob = bpy.data.objects.new(name, data)  
    
    ob.location = emp.location
    ob.rotation_euler = emp.rotation_euler
    ob.parent = emp.parent
    
    ob.data.color = eval(get_property_value(emp, 'light color'))
    ob.data.energy = get_property_value(emp, 'energy')
    ob.data.distance = get_property_value(emp, 'distance')
    ob.data.linear_attenuation = get_property_value(emp, 'lin_attenuation')
    ob.data.quadratic_attenuation = get_property_value(emp, 'quad_attenuation')
    
    if ob.data.type == 'SPOT':
        ob.spot_size = get_property_value(emp, 'spotsize')
        ob.spot_blend = get_property_value(emp, 'spotblend')
        
    bpy.context.scene.objects.link(ob)

def convert_to_empties():
    #print(bpy.context.selected_objects)
    for ob in bpy.context.selected_objects:
        if ob.type == 'LAMP':
            if ob.data.type in ['SPOT', 'POINT']:
                create_empty(ob)
                
    for ob in bpy.context.selected_objects:
        if ob.type == 'LAMP':
            if ob.data.type in ['SPOT', 'POINT']:
                ob.select = True
            else:
                ob.select = False
        else:
            ob.select = False
    bpy.ops.object.delete()
    
def convert_to_lights():
    '''Undoes convert_to_empties to allow visualization'''
    for ob in bpy.context.selected_objects:
        if ob.type == 'EMPTY':
            if 'Managed Light' in ob.game.properties:
                create_lamp(ob)
                
    for ob in bpy.context.selected_objects:
        if ob.type == 'EMPTY':
            if 'Managed Light' in ob.game.properties:
                ob.select = True
            else:
                ob.select = False
        else:
            ob.select = False
    bpy.ops.object.delete()
    
    
class ObjectPanel(bpy.types.Panel):
    bl_label = "Lighting Manager"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "render"
 
    def draw(self, context):
        self.layout.operator("lighting.convert_to_empties", text='Convert Selected To Empties')
        self.layout.operator("lighting.convert_to_lights", text='Convert Selected To Lights')
        
class OBJECT_OT_LightingButton(bpy.types.Operator):
    bl_idname = "lighting.convert_to_empties"
    bl_label = "Convert Selected To Empties"
 
    def execute(self, context):
        print('Converting to Empties')
        convert_to_empties()
        return{'FINISHED'}   
     
class OBJECT_OT_LightingButton(bpy.types.Operator):
    bl_idname = "lighting.convert_to_lights"
    bl_label = "Convert Selected To Lights"
 
    def execute(self, context):
        print('Converting to Lights')
        convert_to_lights()
        return{'FINISHED'}    


bpy.utils.register_module(__name__)

It creates a small dialog in the world properties with some buttons on it for converting objects to/from empties and lights

I could definitely adapt this :smiley:

Can empties store a color ? (object color?)

what about a mesh that is a single vert?

Together the two systems could make a addon quite well, I think,

(I really should try bpy some time more seriously, it’s not as fun as bge api though)

Add this to Blender in C++. Commit the update and hope that it’ll be part on Blender Master, maybe 2.76 release:D

if we got something like that in the addons i would love it…

I was thinking that somehow ‘Ramping’ the energy value up to the level it’s supposed to be at over time, and ramping it down over time before moving a lamp would be a good idea, so lamps don’t ‘pop’ .

and Sdfgeoff, have you confirmed that the resources from lamps are released on libFree?

I have been wanting to play with libLoad/libFree for a while now, but just have not had a level complex enough to warrant it yet,

The color is stored in a game property in the empty as a python list (the property is ‘light color’).

Don’t bother with libfreeing lights. They stop displaying, but from memory it stays compiled in to the shader source, so it still uses resources/graphics ram.
Libfree works on meshes though.

Ramping lamps is definitely worth doing. It makes it look much much better.

ok, so now I need to make sure I am not shuffling lights needlessly

I should build a list of the last populated set of lights and run it against the closest set, and any that are not on the closest set get there energy lowered until it’s zero, and then are added to a available list somehow?

then during the same check (is light hook on closest list, already populated, and energy lower then max?)

if it’s energy is lower then max, and it’s populated and in the closeset lights list , add energy.