[SOLVED] Lights disregarding colors & materials

Hello,

Im running into a bit of a problem where as soon as I put lights in place in my scene, it completely removes all color/material information from my models.

My code is as follows :

# -*- coding: utf-8 -*-

#Panda3D
from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
from pandac.PandaModules import VBase4

class VoxelApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        self.cube = loader.loadModel('cube')
        self.root = render.attachNewNode("Root node")
        self.root.reparentTo(render)

        self.material = Material()
        self.material.setShininess(5.0)
        self.material.setAmbient(VBase4(0,0,1.0,1))
        self.material.setDiffuse(VBase4(1.0,0,1.0,1))
        self.cube.setMaterial(self.material)

        self.createGeom();
        self.createLights()

        render.setShaderAuto()

    def createGeom(self):
        size_x = 10
        size_y = 10
        for x in range(size_x):
            for y in range(size_y):
                dummy = self.root.attachNewNode("Voxel")
                dummy.setPos(x*0.1, 1+y*0.1, -0.2)
                dummy.setColor(1.0/size_x*x, 1.0/size_y*y, 0.5,1.0)
                self.cube.instanceTo(dummy)

        for x in range(size_x/2):
            for y in range(size_y/2):
                dummy = self.root.attachNewNode("Voxel")
                dummy.setPos(x*0.1, 1+y*0.1, -0.1)
                dummy.setColor(1.0/size_x*x, 1.0/size_y*y, 0.0)
                #dummy.setMaterial(self.material)
                self.cube.instanceTo(dummy) 

    def createLights(self):
        dlight = DirectionalLight('dlight')
        dlight.setColor(VBase4(0.8, 0.8, 0.8, 1))
        dlnp = render.attachNewNode(dlight)
        dlnp.setPos(0, -10, 10)
        dlnp.lookAt(0,0,0)
        render.setLight(dlnp)

        alight = AmbientLight('alight')
        alight.setColor(VBase4(0.2, 0.2, 0.2, 1))
        alnp = render.attachNewNode(alight)
        render.setLight(alnp)

        plight = PointLight('plight') 
        plight.setColor(VBase4(0.2, 0.2, 0.2, 1)) 
        plnp = render.attachNewNode(plight) 
        plnp.setPos(0.5, 1.5, 1.0) 
        #render.setLight(plnp)

loadPrcFileData("", "show-frame-rate-meter #t")
app = VoxelApp()
app.run()

The model can be found here
http://dl.dropbox.com/u/17113774/cube.egg (it’s simply an untextured cube of size 0.10.10.1)

If you run this code, you will see a flat-shaded, grey geometry. If, however, you comment out the two lights, you will see the expected colors that I set on each object (though not the material color, but that’s normal).

When lit, neither the colors applied with setColor() nor the one from the material show. How come ?

Thanks in advance!

Your cube.egg already includes its own Material specification, which by default overrides the Material you apply at runtime. If you want your runtime Material to take precedence, you have to supply a priority value, like this:

self.cube.setMaterial(self.material, 1)

David

That did the trick (that, or removing the material from the egg). I had forgotten that Blender added a material to the default cube…

And that trick with priorities is good to know, so thanks, marking as solved!