Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Code for control of the camera

Peter__B
Advocate

Code for control of the camera

Peter__B
Advocate
Advocate

Hi

 

The code below controls the camera in Fusion 360. The code works properly but there are some additional assignments that I have to do which to my knowledge should not be necessary and should be possible to replace them by just one assignment.

 

In the code I have to assign (part of the code below):

 

        cam.eye = adsk.core.Camera.eye
        cam.target = adsk.core.Camera.target
        cam.upVector = adsk.core.Camera.upVector
        cam.isSmoothTransition = adsk.core.Camera.isSmoothTransition

 

It seems to be necessary to make the assignment property by property in order to get the code working. It would be much easier if I could make the assignment for all properties att the same time by

 

cam = adsk.core.Camera

but if I try to execute the code with this assignment I get the error code: Is there anybody that can explain what I am doing wrong ?

 

Error CodeError Code

 

#Author-
#Description-


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

app = adsk.core.Application.get()

def run(x):
    try:
        cam = app.activeViewport.camera        
        target = adsk.core.Point3D.create(0,0,100)
        up = adsk.core.Vector3D.create(0,0,1)
        eye = adsk.core.Point3D.create(500,500,100)

        steps = 500
        
        adsk.core.Camera.eye = eye
        adsk.core.Camera.target = target
        adsk.core.Camera.upVector = up
        adsk.core.Camera.isSmoothTransition = False     

        cam.eye = adsk.core.Camera.eye
        cam.target = adsk.core.Camera.target
        cam.upVector = adsk.core.Camera.upVector
        cam.isSmoothTransition = adsk.core.Camera.isSmoothTransition

        app.activeViewport.camera = cam
        ViewDist = adsk.core.Camera.target.distanceTo(adsk.core.Camera.eye)
        
        for i in range(0, 2*steps):
            eye = adsk.core.Point3D.create(2*ViewDist * math.cos((math.pi*2) * (i/steps)),\
            ViewDist * math.sin((math.pi*2) * (i/steps)), 100)            

            cam.eye = eye                             
            app.activeViewport.camera = cam

            adsk.doEvents()
            app.activeViewport.refresh()
            
    except:
        ui = app.userInterface
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Best Regards

Peter

0 Likes
Reply
Accepted solutions (2)
735 Views
4 Replies
Replies (4)

marshaltu
Autodesk
Autodesk

Hello,

 

I simplify your codes as below. "cam = app.activeViewport.camera" just gets a (temporary)copy of current camera. You can edit its parameters and set it back to active viewport to make the changes take effect.

 

Thanks,

Marshal

 

 

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        cam = app.activeViewport.camera        
        target = adsk.core.Point3D.create(0,0,100)
        up = adsk.core.Vector3D.create(0,0,1)
        eye = adsk.core.Point3D.create(500,500,100)

        steps = 500

        cam.eye = eye
        cam.target = target
        cam.upVector = up
        cam.isSmoothTransition = False

        ViewDist = target.distanceTo(eye)
        
        for i in range(0, 2*steps):
            eye = adsk.core.Point3D.create(2*ViewDist * math.cos((math.pi*2) * (i/steps)),\
            ViewDist * math.sin((math.pi*2) * (i/steps)), 100)            

            cam.eye = eye                             
            app.activeViewport.camera = cam

            adsk.doEvents()
            app.activeViewport.refresh()


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

 



Marshal Tu
Fusion Developer
>
0 Likes

Peter__B
Advocate
Advocate

Thanks Marshal

 

I also did that myself (copying a the current camera), but why do I get an error when I try to make the assignment ?

 

cam = adsk.core.Camera

If I make the assignments it property by property i.e.  cam.eye = adska.core.Camera.eye  etc. it works.

 

Is the assignment, cam = adsk.core.Camera, not allowed ?

 

/Peter

0 Likes

marshaltu
Autodesk
Autodesk
Accepted solution

Hello,

 

 

"adsk.core.Camera" is an API object. We didn't provide assignment operator for it. 

 

Thanks,

Marshal

 



Marshal Tu
Fusion Developer
>
0 Likes

MichaelT_123
Advisor
Advisor
Accepted solution

Hi, Mr.Peter  and Mr.Tu

 

'adsk.core.Camera' is a type of gibberish most programmers commit quite often.

 

Just correct it a little bit to:  'adsk.core.Application.get().activeViewport.camera' and you will be on a solid path for ... for the next one 🙂

 

Regards

MichaelT_123

 

 

 

MichaelT
2 Likes