Is there in Sketchup some variable ( or something else) which holds actual zoom factor for the viewport of the model?
Camera should store the info you are looking for.
camera_position = Sketchup.active_model.active_view.camera.eye camera_target = Sketchup.active_model.active_view.camera.target
I need more help.
p Sketchup.active_model.active_view.camera.eye p Sketchup.active_model.active_view.camera.target Sketchup.active_model.active_view.zoom 2.0 p Sketchup.active_model.active_view.camera.eye p Sketchup.active_model.active_view.camera.target
nothing changed except zoom factor. Eye and target are the same. Then how can I obtain zoom factor using eye and target?
Sketchup’s zoom and zoom window tools, as well a mouse scroll wheel change the eye position, and do not perform actual camera zoom.
Your statement of “…zoom 2.0” changed the field of view to half of the default (from 35 degrees to 17.5) when I typed it into the Ruby console, but you need to be in perspective mode. If you are in parallel projection, it looks like the eye position changes. If you have repeatedly been using the zoom method during development in perspective mode, then the field of view will eventually limit to minimum of 1 degree or maximum of 120 degrees.
I’m not that familiar with method zoom, but it appears that its reciprocal is being applied to field of view (within fov’s limits). Directly changing the camera’s field of view or focal length also affect zoom in perspective view. The following statement sets the field of view to the default of 35 degrees:
Sketchup.active_model.active_view.camera.fov = 35.0
When in perspective view, changing the camera’s field of view (method “fov=” units degrees) or focal length (method “focal_length=” units mm) changes actual camera zoom, keeping the camera at the same eye position.
Note: Sketchup’s API http://ruby.sketchup.com/Sketchup/Camera.html#fov%3D-instance_method
says that field of view is in millimeters between 1 and 120. Decreasing the value zooms in and increasing the value zooms out, so it seems to be in units of degrees, while focal length is in millimeters.
In perspective views the mouse wheel moves the camera forwards or backwards as BruceYoung said but in parallel projections it actually changes zooms without moving the camera (this is why clipping planes may be un-inuitive when switching between projection modes).
However, what you are looking for is probably the pixels_to_model method. The following code example returns the scale factor at the model origin in inches/px (SketchUp always uses inches internally no matter the display unit set in Model Info).
In a parallel projection view this method should return the same value for all possible points but in a perspective view the scale obviously differs within the view.
If your goal is to to show a scale factor for the screen (e.g. 1:100) you also need to determine the screen DPI and take the scale factor into account.
pixels_to_model uses logical pixels since SU2017. SU2017 also added UI.scale_factor which can be used to calculate the physical pixels. Getting the screen DPI however is not supported by the API but would need some sort of external script or program call.
Okay, I see that mouse wheel scrolling in parallel projection does not affect the camera eye. What I was observing was it being a bit affected because I don’t scroll exactly on the center pixel and I am affecting the camera direction, causing small updates to camera eye and target.
When using the view class’s zoom method in perspective mode, camera focal_length, fov, and image_width are affected.
When using the view class’s zoom method in in parallel projection, it affects the size of the image plane, and camera height is changed. There is no camera width method for parallel projection (method image_width only works in perspective). It could be determined from height and aspect ratio.
After having tried a few more examples, the zoom method is rejecting my attempts at using integers (i.e. “2” is raising an exception, but “2.0” does not).