How to zoom viewport camera using script?

What's the best way to programmatically zoom in the viewport camera (equivalent to manual viewport controls)?

Comments

  • Richard HaseltineRichard Haseltine Posts: 100,926
    edited October 2017

    Assuming you want to zoom (i.e. adjust teh focal length) then this would work, I think:

    var viewMgr = MainWindow.getViewportMgr();var activeView = viewMgr.getActiveViewport();if ( activeView ) {	var viewport3d = activeView.get3DViewport();	if ( viewport3d ) {		var curCam = viewport3d.getCamera();		if ( curCam ) {			ourCam.focalLength = desiredValue;		}	}}

    If you want to dolly the camera you'd have to get its facing (maybe getFocalPoint(), which returns a vector), I think, then use that as a vector to add to its x,y,z position.

    Post edited by Richard Haseltine on
  • Thanks for the response. Is there a way to do this while keeping focal length constant? When I zoom in viewport, that doesn't change the focal length

  • PraxisPraxis Posts: 254

    Thanks for the response. Is there a way to do this while keeping focal length constant? When I zoom in viewport, that doesn't change the focal length

    Have a look at this post: 87636/a-script-to-control-a-selected-property-with-large-font-size-and-preview-buttons

    The "View" Tab of that dialog has controls to:

    1. Zoom by "Dollying" the Camera towards the focal point (without changing the focal length).
    2. Zoom by changing the focal length (while keeping the focal distance constant).
    3. Vary the perspective by changing the focal length and camera distance, while keeping the apparent image size constant.

    The script attached to that post contains the relevant source code, which is quite well commented.

     

Sign In or Register to comment.