Manipulating the camera view

RogerInHawaii
Collaborator
Collaborator

Manipulating the camera view

RogerInHawaii
Collaborator
Collaborator

If I understand this correctly you can manipulate which portion of you design shows in the window by manipulating the Camera object. I was able to figure out how to get it to do an API "fit view" operation, which is somewhat, but not quite, what a Find In Window browser operation does.

 

Fit View causes the entire design (well, at least the parts of the design that are currently visible, via their Light Bulb icons) to be maximized in the window, In my case the design is taller than it is wide, so the top of the design just touches the top of the display window and the bottom of the design just touches the bottom o f the display window.

Prior to doing the Fit View my window looks like this:

camera.jpg

 

AFTER the Fit View operation it looks like this:

Camera 1.jpg

 

BUT, if instead of doing the Fit View (via my C++ script) I just click on the entire project in the browser and select to Find In Window, I get this.

 

Camera 2.jpg

 

Which does NOT maximize the image but rather leaves some room at the top and bottom. It's kind of like the "camera" has been moved back from the object.

I want to be able to do something like that but via my C++ script. I tried writing some code that uses the Camera eye object. I made sure that the Z axis of the object is pointing directly perpendicular to the screen and modified the z component of the eye object, as in this code:

bool CenterAndShowInWindowAllVisibleObjects()
{
	Ptr<Viewport> viewPort = theFusion360Application->activeViewport();
	if (!viewPort)
		return false;
	Ptr<Camera> TheCamera = viewPort->camera();
	if (!TheCamera)
		return false;
	
	TheCamera->isFitView(true);
	viewPort->camera(TheCamera);

	return TRUE;
}

bool ZoomOutToShowInWindowAllVisibleObjects()
{
	// This is different than CenterAndShowInWindowAllVisibleObjects in that
	// we want to move the camera back a bit so that everything shown doesn't
	// simply extend to the outer edges of the windo. In particular we
	// want to assure that any movement or, for example, landing gear deployments
	// are fully visible.
	Ptr<Viewport> viewPort = theFusion360Application->activeViewport();
	if (!viewPort)
		return false;
	Ptr<Camera> TheCamera = viewPort->camera();
	if (!TheCamera)
		return false;

	// Get the existing CameraPosition
	Ptr<Point3D> CameraPosition = TheCamera->eye();
	Show("EXISTING Camera Position X,Y,Z = " + std::to_string(CameraPosition->x()) + "," + std::to_string(CameraPosition->y()) + "," + std::to_string(CameraPosition->z()));

	// Modify the CameraPosition, effectively pulling it back a bit, perpendicular to the center of the window.
	// It's the Z axis (for the overall project) that is perpendicular to the surface of the screen, i.e. towards (+) you and away from you (-).
	double CameraPositionZ = CameraPosition->z();
	double AmountToPullBackInCentimeters = 200;
	CameraPosition->z(CameraPositionZ + AmountToPullBackInCentimeters);


	// Set the value of the property, where value_var is a Point3D.
	TheCamera->eye(CameraPosition);
	viewPort->camera(TheCamera);

	UpdateImage();

	Show("NEW Camera Position X,Y,Z = " + std::to_string(CameraPosition->x()) + "," + std::to_string(CameraPosition->y()) + "," + std::to_string(CameraPosition->z()));

	return TRUE;
}


The first method basically does the FitView operation. And that does indeed get the object fitted into view.

The second method is intended to basically "pull back" from the view, which SHOULD (I think) result in the object looking a bit smaller in the window and be more like what you get when you use the Find In Window operation from the Browser. But it doesn't. Instead it seems to re-orient the displayed object.

I've tried lots of different approaches but can't seem to get it to do what I want. Basically, no matter how the object is oriented within the window, I want to be able to zoom-out much like the Browser's Find In Window operation works.

Camera 3.jpg

 Ideally, I'd also like to be able to do, through my C++ script, the operations you get when you click on the various faces of the little cube in the upper right corner.

Camera 4.jpg

 

Is that possible?

Is there any documentation/examples on how to effectively use the Camera object?

0 Likes
Reply
1,291 Views
2 Replies
Replies (2)

kandennti
Mentor
Mentor

Hi @RogerInHawaii .

 

Since I do not understand C ++, it will be explained in Python. I'm sorry.

Here is @BrianEkins  Python 'FindInWindow' sample.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/find-in-window/m-p/8614696#M7431 


However, 'FindInWindow' will not be performed if the selection is canceled at the end.
Probably because commandDefinition.execute () is processed after script execution.
I experienced a similar phenomenon here.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/unexpected-behavior-with-ui-activeselectio... 

Therefore, I think that it cannot be used during processing.

 


The Camera object has a viewExtents property, so why not increase this size?

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-1962e74e-2fc2-4821-887f-7abe2bc4c09a 

After a little trial, the value was about 1.4 ~ 1.5 times that of the isFitView method.
They don't exactly match, but I don't think there is a big difference.

#Fusion360API Python script

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app  :adsk.core.Application = adsk.core.Application.get()
        ui   :adsk.core.UserInterface = app.userInterface

        vp = app.activeViewport
        vp.fit()
        cam :adsk.core.Camera = vp.camera
        cam.viewExtents = cam.viewExtents * 1.45
        vp.camera = cam
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes

BrianEkins
Mentor
Mentor

Here's an overview of cameras in a blog post I wrote a while ago.  It was written for Inventor but the concepts apply the same to Fusion 360 and the post just discusses the concepts.  I'm sure your problem with zooming out is because you were using an orthographic camera, which is discussed in the post.

https://modthemachine.typepad.com/my_weblog/2013/09/working-with-cameras-part-1.html

 

Here's a video that was an early experiment where I used a program to modify parameters and move the camera.

https://youtu.be/Z4SyKEcO72s

 

The intro to this video was done entirely with Fusion 360 and some customization. 

https://youtu.be/fj4lZdQKQ4w

 

 

 

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
1 Like