setColor priority

I am new to panda3D, and I have an issue with a small piece of code.

I am trying to implement a function which changes the color of an object to green when the cursor is on it, and to blue when it is not (all models are initially red)

I used a piece of code found on the forum or in the manual, and adaptated it.

This is what I’ve made :

    def getObjectHit(self, task):
        a = render.getChildren()
        for i in range(1,a.getNumPaths()) :
            print task.time," node path : ",a.getPath(i) 
            a.getPath(i).setColor(0,0,1,1)
            
            
        if(base.mouseWatcherNode.hasMouse()):
            mpos = base.mouseWatcherNode.getMouse()
            self.pickedObj = None #be sure to reset this
            self.pickerRay.setFromLens(base.camNode, mpos.getX(),mpos.getY())
            self.picker.traverse(render)
            for i in range(self.queue.getNumEntries()):
                self.pickedObj = self.queue.getEntry(i).getIntoNodePath()
                self.pickedObj.setColor(0,1,0,1)
        return Task.cont

When it starts, the models are all blue, thanks to the first for loop.
The thing is, it does change from blue to green when the cursor goes on the model, but it does not goes back to blue after.

If I set the priority of the first (blue) color change to 1, it does not turn into green.

Can you explain me what is going wrong ?

It’s because you color it at different level. You paint objects direcly under render to blue, while the object turn to green is not directly under render, it must be a child of your blue object. So by setting priority on the parent, the color of the child is overridden.
To solve this, you can remember what is the last picked object. So if other object is hit, you can turn back the last picked object to blue, and then paint the currently picked one to green. It would save you from resetting everything to blue everytime you pick an object.

Thanks a lot ! It works very fine.