Picking Issue

I’ve been trying to get picking to work. I’ve found an great example and it seems straight forward enough. However, when I’ve put the code into my program, I get a strange issue. When the program first loads up, the program doesn’t register any clicked model. However, when I move the model with the default mouse movements, the picking then works.

I’m left scratching my head as to what is wrong with my code.


class World(DirectObject):

    def __init__(self):
        self.initializeModels()
        self.initializeLight()	
        #base.disableMouse()

        #base.cam.setHpr(0, -35, 0) 
       # base.cam.setPos(0, -60, 40)

        self.backgroundSetup(0.0, 0.0, 0.0)
        self.initializeImages()
        self.initializePicking2()

    def initializeModels(self):
        self.courseModel = loader.loadModel("Whackamap2.egg")
        self.courseModel.reparentTo(render)
        self.courseModel.setTwoSided(1)
        self.courseModel.setPos( 0, 0, 0)
        self.courseModel.setScale(2.0)
        self.courseModel.setHpr(0, 0, 0)
        self.courseModel.hide()
        self.whiteRabbitModel = loader.loadModel("whiteRabbitModel3.egg")
        self.whiteRabbitModel.reparentTo(render)
        self.whiteRabbitModel.setTwoSided(1)
        self.whiteRabbitModel.setPos(0, 50, -5)
        self.whiteRabbitModel.setScale(1.0)
        self.whiteRabbitModel.setHpr(0, -90, 0)
        self.whiteRabbitMotion = 1

        self.makePickable(self.whiteRabbitModel)
        #self.makePickable(self.courseModel)

    def initializeImages(self):
        self.logoImage = OnscreenImage(image = 'Logo.tga', parent=render2d)
        self.logoLGImage = DirectLabel(image="Logo.tga", scale = 0.35, image_pos = (2.5, 0.0, 0.65))
        self.logoLGImage.hide()
        scaleX = 0.4
        scaleY = 1.0
        scaleZ = 0.2
        scaleValue = 0.6
        scaleValue2 = 0.3
        self.font = loader.loadFont('balloonfont.egg')
        self.startButton = DirectButton(command=self.start, scale = 0.45, image = "StartButton.tga", image_scale=(scaleX, scaleY, scaleZ), image_pos=(0, 0, -0.5))
        self.exitButton = DirectButton(command=self.exitProgram, scale = 0.45, image = "QuitButton.tga", image_scale=(scaleX, scaleY, scaleZ), image_pos=(0, 0, -1.25))
        self.scoreImage=OnscreenImage(image = "ScoreLabel.png", pos = (0, 0, 0.85), scale=(1.35, 0, 0.15))
        self.scoreImage.setTransparency(TransparencyAttrib.MAlpha)
        self.scoreImage.hide()

        
    def initializePicking2(self):
        #setup collision stuff

        self.picker= CollisionTraverser()
        self.queue=CollisionHandlerQueue()

        self.pickerNode=CollisionNode('mouseRay')
        self.pickerNP=camera.attachNewNode(self.pickerNode)

        self.pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask())

        self.pickerRay=CollisionRay()

        self.pickerNode.addSolid(self.pickerRay)

        self.picker.addCollider(self.pickerNP, self.queue)

        #this holds the object that has been picked
        self.pickedObj=None
        self.mouseTask = taskMgr.add(self.mouseTask2, 'mouseTask')
        self.accept('mouse1', self.printMe)
        
    def makePickable(self, newObj):
        newObj.setTag('pickable', 'true')
        
    #this function finds the closest object to the camera that has been hit by our ray
    def getObjectHit(self, mpos): #mpos is the position of the mouse on the screen
        self.pickedObj=None #be sure to reset this
        self.pickerRay.setFromLens(base.camNode, mpos.getX(),mpos.getY())
        self.picker.traverse(render)
        if self.queue.getNumEntries() > 0:
            self.queue.sortEntries()
            self.pickedObj=self.queue.getEntry(0).getIntoNodePath()

            parent=self.pickedObj.getParent()
            self.pickedObj=None

            while parent != render:
                if parent.getTag('pickable')=='true':
                    self.pickedObj=parent
                    return parent
                else:
                    parent=parent.getParent()
        return None
    
    def getPickedObj(self):
        return self.pickedObj
        
    def printMe(self):
        #if base.mouseWatcherNode.hasMouse():
        self.getObjectHit( base.mouseWatcherNode.getMouse())
       	print self.pickedObj
        
 
    def mouseTask2(self, task):
        return Task.cont
    
    
    def exitProgram():
        stop()

    def backgroundSetup(self, red, green, blue ):        
        base.setBackgroundColor( red, green, blue ) # set the color   

    def start(self):
        self.scoreImage.show()
        self.logoImage.hide()
        self.startButton.hide()
        self.exitButton.hide()

    def exitProgram():
        sys.exit(1)
        
    def initializeLight(self):
        # Create Ambient Light
        ambientLight = AmbientLight( 'ambientLight' )
        ambientLight.setColor( Vec4( 0.6, 0.6, 0.6, 1 ) )
        ambientLightNP = render.attachNewNode( ambientLight.upcastToPandaNode() )

        # Directional light 01
        directionalLight = DirectionalLight( "directionalLight" )
        directionalLight.setColor( Vec4( 1.0, 1.0, 1.0, 1 ) )
        directionalLightNP = render.attachNewNode( directionalLight.upcastToPandaNode() )
        # This light is facing backwards, towards the camera.
        directionalLightNP.setHpr(18, 20, 80)

        # Directional light 02
        directionalLight = DirectionalLight( "directionalLight" )
        directionalLight.setColor( Vec4( 1.0, 1.0, 1.0, 1 ) )
        directionalLightNP = render.attachNewNode( directionalLight.upcastToPandaNode() )
        # This light is facing forwards, away from the camera.
        directionalLightNP.setHpr(20, -80, 40)
        render.setLight(directionalLightNP)
        render.setLight(ambientLightNP)


 
	

w = World()
run()