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(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.
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
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.
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
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 but I learned that I forgot 2 the most useful things in Python here
EDIT:
I will post the whole lighting solution later.