Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Camera movements & matrix3D

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
jacob.l.charron
362 Views, 3 Replies

Camera movements & matrix3D

My goal is to design a CAD mouse similar to the 3Dconnexions Spacemouse. So what I want to do is to move the scenery & not the origin of the part. But other than the camera, I don't see what variable I have to work with.
I'm running some tests to understand how the camera movements are working, but I encounter a few issues with the rotations & translations.

For example, if I'm trying to rotate 360deg on the Z-axis while it's at the horizontal. The object will rotate around the Z-axis about 180deg, then flip (mirror) the camera & then execute the other 180deg. The only time it works fine is when the axis of rotation is vertical.

The translations & the scaling don't seem to work when you play in the matrix3D. The scaling can be dealt with the camera viewExtents. But the translation just doesn't seem to work. Any ideas on how to deal with this
Thanks in advance!

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

rotationX = 1
rotationY = 1
rotationZ = 0.02
scalingX = 0.0
scalingY = 0.0
scalingZ = 0.0
translationX = 10
translationY = 0
translationZ = 0
        
rotateX = [1.0, 0.0, 0.0, 0.0,
        0.0, math.cos(rotationX), math.sin(rotationX), 0.0,
        0.0, -1*math.sin(rotationX), math.cos(rotationX), 0.0,
        0.0, 0.0, 0.0, 1.0]
        
rotateY = [math.cos(rotationY), 0.0, math.sin(rotationY), 0.0,
        0.0, 1.0, 0.0, 0.0,
        -1*math.sin(rotationY), 0.0, math.cos(rotationY), 0.0,
        0.0, 0.0, 0.0, 1.0]

rotateZ = [math.cos(rotationZ), math.sin(rotationZ), 0.0, 0.0,
        -1*math.sin(rotationZ), math.cos(rotationZ), 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0]

scale = [scalingX, 0.0, 0.0, 0.0,
        0.0, scalingY, 0.0, 0.0,
        0.0, 0.0, scalingZ, 0.0,
        0.0, 0.0, 0.0, 1.0]
        
translation = [1.0, 0.0, 0.0, translationX,
        0.0, 1.0, 0.0, translationY,
        0.0, 0.0, 1.0, translationZ,
        0.0, 0.0, 0.0, 1.0]

test = [0.707, 0.707, 0.0, 5.0,
        0.707, 0.707, 0.0, 10.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0]

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

        vi = app.activeViewport
        camera = vi.camera 
        eye = camera.eye
        # tgt = camera.target
        # up = camera.upVector
        zoom = camera.viewExtents
        camera.isSmoothTransition = False

        mat = adsk.core.Matrix3D.create()
        print(mat.asArray())
        # print(mat.translation)

        # vector = adsk.core.Vector3D.create(4.0, 0.0, 0.0)
        # show
        for n in range(360):
            # camera.viewExtents = zoom + n        

            mat.setWithArray(translation)
            # mat.tanslation = vector
            eye.transformBy(mat)
            print(mat.asArray())
            
            camera.eye = eye
            # camera.upVector = vector

            vi.camera = camera
            vi.refresh()
            adsk.doEvents()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
3 REPLIES 3
Message 2 of 4

I created a couple of blog posts several years ago that describe the concepts of the camera.  The posts were specific to Inventor but the concepts also apply to Fusion.  Hopefully, that will help.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 4
kandennti
in reply to: jacob.l.charron

Hi @jacob.l.charron .

 

We have created a rotation-only sample.

Matrix3D was obtained using the setToRotation method.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-d2e83d0b-781c-4faf-91ff-6cd31cc2174d 

The axis can be rotated in the current viewed direction by using camera.upVector.

 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
import math

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        deg = 5 #Angle of one step - degree

        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface

        # viewport
        vp: adsk.core.Viewport = app.activeViewport
        cam: adsk.core.Camera = vp.camera
        vecUp: adsk.core.Vector3D = cam.upVector
        target: adsk.core.Point3D = cam.target

        # matrix3d
        mat: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
        rad = math.radians(deg)
        mat.setToRotation(rad, vecUp, target)

        # update camera
        cam.isSmoothTransition = False
        for _ in range(360 // deg):
            eye: adsk.core.Point3D = vp.camera.eye
            eye.transformBy(mat)
            
            cam.eye = eye

            vp.camera = cam
            vp.refresh()
            adsk.doEvents()

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

Hi @BrianEkins@kandennti 

Thanks for the reply! The explanations help a lot.

My issue with the rotations was due to the upVector not being set.

For the translations, the matrix need to modify the target.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report