Light-creation shortening.

Inspired by GML(Game Maker Language) from Game Maker. [size=59]I know it’s a piece of **** and *** and s***s so much that ***** * *** *******.[/size]

-POST WAS LOST-
So I will just quickly write down the class. Put it in your game(to the top) or pu it in a separate file and import it after that:

class Light:
    count = 0
    def amb_lght(cla, clb, clc, cld):
        counta = 0
        name = "ambient_light %d" % counta 
        counta += 1
        alight = AmbientLight(name) 
        alight.setColor(VBase4(cla, clb, clc, cld)) 
        alnp = render.attachNewNode(alight) 
        render.setLight(alnp)
        return alnp
    def pnt_lght(x, y, z, cla, clb, clc, cld):
        countp = 0
        name = "point_light %d" % countp 
        countp += 1
        plight = PointLight(name) 
        plight.setColor(VBase4(cla, clb, clc, cld))
        plnp = render.attachNewNode(plight)
        plnp.setPos(x, y, z)
        render.setLight(plnp) 
        return plnp
    def dir_lght(h, p, r, cla, clb, clc, cld):
        countd = 0
        name = "directional_light %d" % countd
        countd += 1
        dlight = DirectionalLight(name) 
        dlight.setColor(VBase4(cla, clb, clc, cld)) 
        dlnp = render.attachNewNode(dlight)
        dlnp.setHpr(h, p, r)
        render.setLight(dlnp) 
        return dlnp
    def spot_lght_lookat(x, y, z, cla, clb, clc, cld, lens, obj):
        counts = 0
        name = "directional_light %d" % counts
        counts += 1
        slight = Spotlight(name)
        slight.setColor(VBase4(cla, clb, clc, cld)) 
        slight.setLens(lens)
        slnp = render.attachNewNode(slight)
        slnp.setPos(x, y, z)
        slnp.lookAt(obj)
        render.setLight(slnp) 
        return slnp

It’s really easy to use:

tpt = loader.loadModel("teapot")
tpt.reparentTo(render)
ambientlight1 = amb_lght(0.7, 0.7, 0.81, 1)
ambientlight2 = amb_lght(1, 1, 1, 1)
pointlight = pnt_lght(0, 0, 100, 0.9, 0.9, 10, 0.1000)
directionallight = dir_lght(0, 0, 1, 1, 1, 1, 1)
spotlight = spot_lght_lookat(0, 10, 4, 1, 1, 1, 1, PerspectiveLens(), tpt)

Rate&report bugs.

EDIT:
Only spotlight has been not reported to be working. If you want to help, then try it, please!

Hey, that’s actually pretty useful. It does make the code get a little bit less readable, but at least shorter.
Very useful for when one quickly wants to make a short program with lights in it but doesn’t want to bother typing all those long commands. :slight_smile:
Thanks for sharing!

Thanks :slight_smile:
Could you try out the spot light please?

Works fine for me, except that its named “directional_light” instead of “spot_light”. Copy&paste mistake? :slight_smile:

Also, I find this code a bit odd: (applies to all lights)

    count = 0
    def amb_lght(cla, clb, clc, cld):
        counta = 0
        name = "ambient_light %d" % counta
        counta += 1 

Meaning the ambient light will always be named “ambient_light 0”, since counta gets reset just before you use it every time.

Oh, haha, i forgot to remove it :slight_smile:
here, repaired:

class Light: 
    counta, countp, countd, counts = 0, 0, 0, 0
    def amb_lght(cla, clb, clc, cld): 
        name = "ambient_light %d" % counta 
        counta += 1 
        alight = AmbientLight(name) 
        alight.setColor(VBase4(cla, clb, clc, cld)) 
        alnp = render.attachNewNode(alight) 
        render.setLight(alnp) 
        return alnp 
    def pnt_lght(x, y, z, cla, clb, clc, cld): 
        name = "point_light %d" % countp 
        plight = PointLight(name) 
        plight.setColor(VBase4(cla, clb, clc, cld)) 
        plnp = render.attachNewNode(plight) 
        plnp.setPos(x, y, z) 
        render.setLight(plnp) 
        return plnp 
    def dir_lght(h, p, r, cla, clb, clc, cld): 
        name = "directional_light %d" % countd 
        countd += 1 
        dlight = DirectionalLight(name) 
        dlight.setColor(VBase4(cla, clb, clc, cld)) 
        dlnp = render.attachNewNode(dlight) 
        dlnp.setHpr(h, p, r) 
        render.setLight(dlnp) 
        return dlnp 
    def spot_lght_lookat(x, y, z, cla, clb, clc, cld, lens, obj): 
        name = "directional_light %d" % counts 
        counts += 1 
        slight = Spotlight(name) 
        slight.setColor(VBase4(cla, clb, clc, cld)) 
        slight.setLens(lens) 
        slnp = render.attachNewNode(slight) 
        slnp.setPos(x, y, z) 
        slnp.lookAt(obj) 
        render.setLight(slnp) 
        return slnp

EDIT:
Oh, I get:
UnboundLocalError: local variable ‘counta’ referenced before assignment
so opening it again… :angry:

Oh, you either need to define the counta, countp, etc. variables globally, or, you need to reference them with “Light.” since they are in a class.
Also, since you put the methods in a class, you must put a @staticmethod in front of them (since they are static functions and not methods)
Like:

class Light:
    counta, countp, countd, counts = 0, 0, 0, 0
    @staticmethod
    def amb_lght(cla, clb, clc, cld):
        name = "ambient_light %d" % Light.counta
        Light.counta += 1 

this gives an error(but what is that @staticmethod?)
this gives “black screen” but no errors:

countab, countpb, countdb, countsb = 0, 0, 0, 0
class Light: 
    def amb_lght(cla, clb, clc, cld):
        counta = countab
        name = "ambient_light %d" % counta 
        counta += 1 
        alight = AmbientLight(name) 
        alight.setColor(VBase4(cla, clb, clc, cld)) 
        alnp = render.attachNewNode(alight) 
        render.setLight(alnp) 
        return alnp 
    def pnt_lght(x, y, z, cla, clb, clc, cld):
        countp = countpb
        name = "point_light %d" % countp 
        plight = PointLight(name) 
        plight.setColor(VBase4(cla, clb, clc, cld)) 
        plnp = render.attachNewNode(plight) 
        plnp.setPos(x, y, z) 
        render.setLight(plnp) 
        return plnp 
    def dir_lght(h, p, r, cla, clb, clc, cld):
        countd = countdb
        name = "directional_light %d" % countd 
        countd += 1 
        dlight = DirectionalLight(name) 
        dlight.setColor(VBase4(cla, clb, clc, cld)) 
        dlnp = render.attachNewNode(dlight) 
        dlnp.setHpr(h, p, r) 
        render.setLight(dlnp) 
        return dlnp 
    def spot_lght_lookat(x, y, z, cla, clb, clc, cld, lens, obj):
        counts = countsb
        name = "directional_light %d" % counts 
        counts += 1 
        slight = Spotlight(name) 
        slight.setColor(VBase4(cla, clb, clc, cld)) 
        slight.setLens(lens) 
        slnp = render.attachNewNode(slight) 
        slnp.setPos(x, y, z) 
        slnp.lookAt(obj) 
        render.setLight(slnp) 
        return slnp

you meant that?: