Sorry if I’m being very slow here, but what is lens.fov = lens.fov
meant to do? Aren’t you assigning a reference to itself, which changes nothing?
Did you by any chance mean to say:
fov = lens.fov
lens.focal_length = (base.cam.get_pos(render) - self.ralph.get_pos(render)).length()
calculated_film_value_for_ortho_lens = lens.film_size
lens.fov = fov # change FOV back to original value
?
Here is the simplest code snippet we can use to see if what you suggest works or not. This is everything else, for setting camera lens type and zooming in/out, before adding your solution:
import panda3d.core
from direct.showbase.ShowBase import ShowBase
base = ShowBase()
model = base.loader.load_model("panda")
model.reparent_to(base.render)
orbit_center = base.render.attach_new_node("orbit_center")
orbit_center.set_hpr(45,-20,0)
base.camera.reparent_to(orbit_center)
base.camera.set_y(-30)
orthographic_lens = panda3d.core.OrthographicLens()
perspective_lens = base.cam.node().get_lens()
def mouse_wheel_zoom_in():
base.camera.set_y(orbit_center, base.camera.get_y() + 2)
# set a zooming range
if base.camera.get_y(orbit_center) > -5: base.camera.set_y(orbit_center, -5)
def mouse_wheel_zoom_down():
base.camera.set_y(orbit_center, base.camera.get_y() - 2)
# set a zooming range
if base.camera.get_y(orbit_center) < -60: base.camera.set_y(orbit_center, -60)
base.accept("wheel_up", mouse_wheel_zoom_in)
base.accept("wheel_down", mouse_wheel_zoom_down)
cam_lens_state = 1
def toggle_cam_lens_type():
global cam_lens_state
if cam_lens_state == 0:
base.cam.node().set_lens(perspective_lens)
cam_lens_state = 1
elif cam_lens_state == 1:
base.cam.node().set_lens(orthographic_lens)
cam_lens_state = 0
base.accept("a", toggle_cam_lens_type)
def camera_task(task):
# put the orthographic film adjustment code here?
return task.cont
base.taskMgr.add(camera_task, "camera_task")
base.disable_mouse()
base.run()
And this is what I think you mean, with the relevant lines of code added to the above template code snippet:
import panda3d.core
from direct.showbase.ShowBase import ShowBase
base = ShowBase()
model = base.loader.load_model("panda")
model.reparent_to(base.render)
orbit_center = base.render.attach_new_node("orbit_center")
orbit_center.set_hpr(45,-20,0)
base.camera.reparent_to(orbit_center)
base.camera.set_y(-30)
orthographic_lens = panda3d.core.OrthographicLens()
perspective_lens = base.cam.node().get_lens()
def mouse_wheel_zoom_in():
base.camera.set_y(orbit_center, base.camera.get_y() + 2)
# set a zooming range
if base.camera.get_y(orbit_center) > -5: base.camera.set_y(orbit_center, -5)
def mouse_wheel_zoom_down():
base.camera.set_y(orbit_center, base.camera.get_y() - 2)
# set a zooming range
if base.camera.get_y(orbit_center) < -60: base.camera.set_y(orbit_center, -60)
base.accept("wheel_up", mouse_wheel_zoom_in)
base.accept("wheel_down", mouse_wheel_zoom_down)
cam_lens_state = 1
def toggle_cam_lens_type():
global cam_lens_state
if cam_lens_state == 0:
base.cam.node().set_lens(perspective_lens)
cam_lens_state = 1
elif cam_lens_state == 1:
base.cam.node().set_lens(orthographic_lens)
cam_lens_state = 0
base.accept("a", toggle_cam_lens_type)
def camera_task(task):
fov = perspective_lens.fov
perspective_lens.focal_length = (base.camera.get_pos(render) - orbit_center.get_pos(render)).length()
calculated_film_value_for_ortho_lens = perspective_lens.film_size
perspective_lens.fov = fov # change FOV back to original value
orthographic_lens.film_size = calculated_film_value_for_ortho_lens
return task.cont
base.taskMgr.add(camera_task, "camera_task")
base.disable_mouse()
base.run()