[SOLVED] Make NodePath visible to camera FOV

I’d like to do the reverse of the question asked in this post: “Checking if Model is Visible”.

I have a model, blah blah, and want the camera to move to where it can see everything at once. I am currently doing something like this:

bounds = model.getBounds()
base.camera.setY(base.camera, -5 * bounds.getRadius())

Unfortunately the fact it’s hard-coded means it won’t always get the Y-distance right, and I am hoping there is a cooler way to compute a new field of view for the camera. :slight_smile:

I’ve used this code before:

    def centerTrackball(self):
        bounds = self.dish.getBounds()
        center = bounds.getCenter()
        radius = bounds.getRadius()

        lens = base.camLens
        fov = lens.getFov()
        distance = radius / math.tan(deg2Rad(min(fov[0], fov[1]) / 2.0))

        idealFarPlane = distance + radius * 1.5
        lens.setFar(max(lens.getDefaultFar(), idealFarPlane))

        idealNearPlane = distance - radius
        lens.setNear(min(lens.getDefaultNear(), idealNearPlane))

        trackball = base.trackball.node()
        trackball.setPos(0, distance, 0)
        trackball.setForwardScale(distance * 0.006)

You can modify it to suit your needs. I don’t know whether you care about adjust the near and far planes or not. And this code works by adjusting the trackball controls to center the model when you’re using the mouse to control the camera (like the “c” key in pview); you could just move the model instead.


Awesome, thank you - worked out of the box!

Replacement code:

bounds = model.getBounds()

fov = base.camLens.getFov()
distance = bounds.getRadius() / math.tan(deg2Rad(min(fov[0], fov[1]) * 0.5))

base.camera.setY(base.camera, -distance)