Light-creation func:could anyone tell what's it's problem?

Hi, I created a function to shorten my time to create lights, suddenly occured lots of problems and i was unable to find where are they. Here is the original:

def amb_lght(name, cla, clb, clc, cld, count):
    name = AmbientLight('name2')
    name.setColor(VBase4(cla, clb, clc, cld))
    name2 = string(name+count)
    name2 = render.attachNewNode(self.name)
    render.setLight('name2')

and latest function defintion:

def amb_lght(cla, clb, clc, cld):
    _name = random.uniform(0, -1)
    _count = random.uniform(0, 10)
    _nm = _name+random.uniform(0, 10)
    _nm = AmbientLight('name2')
    _nm.setColor(VBase4(cla, clb, clc, cld))
    _name2 = render.attachNewNode(_nm)
    render.setLight('_name2')

It’s been written to make it really easy to use, so here’s a “should-work” example:

amb_lght(0.7,0.7,0.81,1,)

And you get an ambient light…

Be gentle please :slight_smile:.

def amb_lght(cla, clb, clc, cld):
    # write the code that generates the name for this light (as string):
    name = ...
    alight = AmbientLight(name)
    alight.setColor(VBase4(cla, clb, clc, cld))
    alnp = render.attachNewNode(alight)
    render.setLight(alnp)
    return alnp # it can be useful to get the light's nodepath

Generally, be careful with quotes: _name2 and ‘_name2’ are NOT the same.

Then:

    _nm = _name+random.uniform(0, 10)
    _nm = AmbientLight('name2') 

In the first line you assign some value to the variable _nm, but in the next line you re-assign completely new value to it. This just doesn’t make sense, I believe :slight_smile:

OK, ok, but there is no other way to do it!

:confused: No way to do what? Sorry, I don’t get it…
Is something wrong about the code I proposed?

because the only way to make a NEW light is to make a new UNIQUE name. It can be defined by user OR be generated. But I can not assign a name before it’s generated, and when I try to assign it the user’s name it makes an exception. and when I do it that way it makes another exception!

count = 0
def amb_lght(cla, clb, clc, cld):
    # write the code that generates the name for this light (as string):
    name = "ambient_light %d" % count
    count += 1 # increment count by 1 so that the name remains unique
    alight = AmbientLight(name)
    alight.setColor(VBase4(cla, clb, clc, cld))
    alnp = render.attachNewNode(alight)
    render.setLight(alnp)
    return alnp # it can be useful to get the light's nodepath

Is this fine for you?

PS:I think it threw exceptions because you was trying to assign integer (or float) as light’s name. Name must be string. You can always convert numbers to strings using str(number) function.

:open_mouth: :open_mouth: :open_mouth:

Fine? It’s amazing! I am so thankful to you that I could post you a free elephant from local ZOO with 1kg of gold on his head :smiley:

btw: I wonder why always when I post my problem here someone will write down the completed/repaired code so I never get to repairing the code on my own :smiley: but I learned that I forgot 2 the most useful things in Python here :wink:

EDIT:
I will post the whole lighting solution later.