Clicking on 3D objects

If you really need a picking function, here’s some untested code that may work. There’s no guarantees.

def pickRay(rootNP, origin, dir):
    "Returns a collision entry for the closest object colliding with the ray."

    traverser = CollisionTraverser("")
    chq = CollisionHandlerQueue()
    rayNode = CollisionNode("")
    ray = CollisionRay()

    # Make a collision node with one collision ray.
    ray.setOrigin(origin)
    ray.setDirection(dir)
    rayNode.addSolid(ray)

    # Attach the collision node to the provided node path.
    rayNP = rootNP.attachNewNode(rayNode)

    # This will let the ray detect collisions with normal geometry.
    rayNode.setFromCollideMask(GeomNode.getDefaultCollideMask())

    # Add the queue to a traverser, which will do the traversal
    # on the provided root node path.
    traverser.addCollider(rayNP, chq)
    traverser.traverse(rootNP)

    # Get the first node path in the sorted queue and discard the rest.
    if chq.getNumEntries() < 1:
        return None
    chq.sortEntries()
    return chq.getEntry(0)