Manipulate the view cube by 45 degrees

Manipulate the view cube by 45 degrees

jonathan_lafontaine
Contributor Contributor
528 Views
2 Replies
Message 1 of 3

Manipulate the view cube by 45 degrees

jonathan_lafontaine
Contributor
Contributor

Hi everyone, since I am not a programmer, I use ChatGPT to build a Python Script to rotate the view cube (camera) by 45 degrees.

 

It almost works perfectly but it also flips the view 180 degrees from left to right, I try changing all the values Z_axis, X and y_axis but it still do a flip. 

 

I put on YouTube a video explaining my problem. 

 

Thank you very much

 

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

def rotate_view_cube():
    try:
        # Get the Fusion 360 application and user interface
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Get the active viewport
        viewport = app.activeViewport

        # Get the current camera
        camera = viewport.camera

        # Set the rotation angle (in degrees) for the view cube
        rotation_angle = 45

        # Calculate the camera's Z-axis direction (view direction)
        y_axis = camera.eye.vectorTo(camera.target)
        y_axis.normalize()  # Ensure it's a unit vector

        # Create a rotation matrix for the eye position and up vector
        rotation_matrix = adsk.core.Matrix3D.create()
        rotation_matrix.setToRotation(math.radians(rotation_angle), y_axis, camera.target)

        # Rotate the camera's eye position around the Z-axis
        eye_vector = camera.eye.vectorTo(camera.target)
        eye_vector.transformBy(rotation_matrix)

        # Update the camera's eye position
        new_eye = camera.target.copy()
        new_eye.translateBy(eye_vector)
        camera.eye = new_eye

        # Update the up vector using the same rotation matrix
        up_vector = camera.upVector
        up_vector.transformBy(rotation_matrix)
        camera.upVector = up_vector

        # Apply the modified camera to update the viewport
        viewport.camera = camera

        ui.messageBox(f'View cube rotated by {rotation_angle} degrees around the x-axis.')

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

rotate_view_cube()

 

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

BrianEkins
Mentor
Mentor
Accepted solution

The code was close, but it was doing more than it needed to and was messing up the camera direction. Here's a modified version that only changes the up direction.

 

def rotate_view_cube():
    try:
        # Get the Fusion 360 application and user interface
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Get the active viewport
        viewport = app.activeViewport

        # Get the current camera
        camera = viewport.camera

        # Set the rotation angle (in degrees) for the view cube
        rotation_angle = 45

        # Calculate the camera's Z-axis direction (view direction)
        y_axis = camera.eye.vectorTo(camera.target)
        y_axis.normalize()  # Ensure it's a unit vector

        # Create a rotation matrix for the eye position and up vector
        rotation_matrix = adsk.core.Matrix3D.create()
        rotation_matrix.setToRotation(math.radians(rotation_angle), y_axis, camera.target)

        # Update the up vector using the same rotation matrix
        up_vector = camera.upVector
        up_vector.transformBy(rotation_matrix)
        camera.upVector = up_vector

        # Apply the modified camera to update the viewport
        viewport.camera = camera

        ui.messageBox(f'View cube rotated by {rotation_angle} degrees around the x-axis.')

    except Exception as e:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 3

jonathan_lafontaine
Contributor
Contributor

Thanks!  Works A1 I posted it on my GitHub and commented you as a co-author. 

 

https://github.com/jomixlaf/f360rotateviewcube

0 Likes