Whats is camera.viewExtents realy describing?

Whats is camera.viewExtents realy describing?

m0d0
Contributor Contributor
903 Views
2 Replies
Message 1 of 3

Whats is camera.viewExtents realy describing?

m0d0
Contributor
Contributor

Hi,

 

I am trying to zoom via the API while the camera type is orthographic type.

According to this blog post  and some trial and error the only way to accomplish this is using the camera.viewExtents property. The documentation on the viewExtents property states: "Defines the area that's visible by the camera. This value is the radius of a sphere centered at the target point. The camera will display everything within that sphere and everything in front of and behind the sphere. [...]"

This is a script creating a unit sphere at the origin, setting camera.target to the origin and using 1 as viewExtents.

 

 

import adsk.core, adsk.fusion, adsk.cam, traceback


def run(context):
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        des = adsk.fusion.Design.cast(
            adsk.core.Application.get().activeProduct)
        des.designType = adsk.fusion.DesignTypes.DirectDesignType
        root = des.rootComponent

        root.bRepBodies.add(
            adsk.fusion.TemporaryBRepManager.get().createSphere(
                adsk.core.Point3D.create(0, 0, 0), 1))

        cam = app.activeViewport.camera
        cam.cameraType = adsk.core.CameraTypes.OrthographicCameraType
        cam.target = adsk.core.Point3D.create(0, 0, 0)
        cam.eye = adsk.core.Point3D.create(0, -1, 0)
        cam.upVector = adsk.core.Vector3D.create(0, 0, 1)
        cam.viewExtents = 1
        app.activeViewport.camera = cam

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

  I exspected that the sphere is displayed in a way so that it just fits into the viewport. That is defintivly not the case. (Using perspective camera type wont make a difference regarding viewport scale.)

 

Setting viewExtents to 3.25 will fit the sphere into the viewport at the vertical scale. However, when using a vertical algined screen the sphere wont fit into the viewport in the horizontal scale.

 

So what does the viewExtents property realy mean? Is it the vertical dimension of the viewport in cm mulitplied by 3.25??? This seems rather unlikely to me.

 

I would be very thankful if someone could point me in the right direction.

 

 

 

0 Likes
Accepted solutions (1)
904 Views
2 Replies
Replies (2)
Message 2 of 3

m0d0
Contributor
Contributor
Accepted solution

I was finaly able to figure out the meaning of this attribute. In case someone else is having trouble with this parameter, thats what I found out:

The camera.viewExtens states the AREA of the largest circle, centered at the target point that can be fit completely into the viewport. So contrary to the documentation its NOT "the radius of a sphere centered at the target point". I think, this should be changed in the documentaion.

To make live easier wotking with this parameter I wrote these helper functions.

 

def view_extents_by_measure(measure: float,
                            is_horizontal_measure: bool = True):
    """Returns the viewExtents parameter so the given model measure fits exactly 
    into the viewport.

    Args:
        measure (float): measure of the model to diplay
        is_horizontal_measure (bool, optional): if the given measure is describing 
            horizontal or vertical distance. Defaults to True.

    Returns:
        float: the viewExtents parameter to apply 
    """
    viewport = adsk.core.Application.get().activeViewport
    is_horizontal_viewport = viewport.width > viewport.height

    if is_horizontal_viewport and is_horizontal_measure:
        factor = viewport.height / viewport.width
    elif not is_horizontal_viewport and not is_horizontal_measure:
        factor = viewport.width / viewport.height
    else:
        factor = 1

    radius = factor * measure * 0.5
    extent = math.pi * (radius**2)
    return extent


def view_extent_by_rectangle(horizontal: float, vertical: float):
    """Returns theviewExtens parameter so that a rectangle ith the given design 
    measures will fit into the viewport.

    Args:
        horizontal (float): horizontal dimension of the model to fit
        vertical (float): vertical dimension of the model to fit

    Returns:
        float: the viewExtents parameter to apply 
    """
    return max(view_extents_by_measure(horizontal, True),
               view_extents_by_measure(vertical, False))

 

This is especially useful if you try to zoom via the API while the camera type is ortographic.

Message 3 of 3

kandennti
Mentor
Mentor

Sorry to bring up an old topic.

 

Around November of last year, I created an add-in using the above information as a hint and it was working fine.
Recently, we received a report from a user that the add-in was not working properly.

 

I checked and it seems that the current version (Ver. 2.0.16265) corrects the behavior to "the radius of a sphere centered at the target point" as described in the documentation.

0 Likes