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: 

getting, setting, changing the camera view

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
FireBallFab
1901 Views, 12 Replies

getting, setting, changing the camera view

Hi all,

 

I am not a programmer, but I need the help of one today!

 

My problem: I am making renders of a large number of files. Ideally they will be all from the same viewpoint - but that viewpoint is not the default "home" position.

 

My solution: I was thinking of a script that could do one of two things: Capture the current view of the current file, OR change the view of the current file to the last "captured view".  The zoom level to "fit" would be great. Any ideas?

 

If anyone is willing to help me out with this, it would be greatly appreciated! 

12 REPLIES 12
Message 2 of 13
marshaltu
in reply to: FireBallFab

Hello,

 

You could change the attributes of "Camera" to change model position in Viewport. I attached a sample script as below for your reference. Please create a new Python script and copy the following codes to that script to have a try.

 

Thanks,

Marshal

-------------------------

import adsk.core, adsk.fusion, traceback

 

def main():

    ui = None

    try:

        app = adsk.core.Application.get()

        camera = app.activeViewport.camera

        camera.viewOrientation = adsk.core.ViewOrientations.FrontViewOrientation

        camera.isFitView = True

        app.activeViewport.camera = camera

 

    except:

        if ui:

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

 

main()

-------------------------



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 3 of 13
FireBallFab
in reply to: marshaltu

marshaltu,

 

Thanks for the reply. With your script I was able to change to the default orientations (FrontViewOrientation, TopViewOrientation, etc) very well. Like I said, I'm not a programmer. You may have to speak slowly.

 

I am hoping for a view that is not a standard orentation, and it seems I would have to set the "Camera.eye" to a "Point.3D".  How would one manually code in the coordinates for the equivalent of "adsk.core.ViewOrientations.IsoTopRightViewOrientation" ?

Message 4 of 13
marshaltu
in reply to: FireBallFab

Hello,

 

It could be difficult to tell what camera attributes should be given for "arbitrary" view. A proposal would be as below:

1. Set view to what you want in UI.

2. Run the following script to get the attributes of camera.

3. Set those values to camera by the previous script codes.

 

Thanks,

Marshal

-----------------

import adsk.core, adsk.fusion, traceback

 

def main():

    ui = None

    try:

        app = adsk.core.Application.get()

        ui = app.userInterface

        camera = app.activeViewport.camera

        eye = camera.eye

        target = camera.target

        upvector = camera.upVector

        ui.messageBox('Eye: {0}, {1}, {2} target: {3}, {4}, {5} upvector: {6}, {7}, {8}'.format(eye.x, eye.y, eye.z, target.x, target.y, target.z, upvector.x, upvector.y, upvector.z))

 

    except:

        if ui:

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

 

main()

-----------------



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 5 of 13
FireBallFab
in reply to: marshaltu

Thanks for the reply. This is getting somewhere! I'll post my results...

Message 6 of 13
FireBallFab
in reply to: marshaltu

Ugh. I am not good at this. 

 

I think I can set the data, but I can't get the view to change. No errors, but no movement.

 

Here is what I have. Any help is appreciated.

import adsk.core, adsk.fusion, traceback
 
def main():
    ui = None
    try:
        
        app = adsk.core.Application.get()
        camera = app.activeViewport.camera
        #camera.viewOrientation = adsk.core.ViewOrientations.IsoTopLeftViewOrientation
        camera.eye.x = -28.61
        camera.eye.y = 13.98
        camera.eye.z = -28.74

        camera.target.x = 0.0
        camera.target.y = -7.23
        camera.target.z = -1.23

        camera.upVector.x = 0.34
        camera.upVector.y = 0.88
        camera.upVector.z = 0.33

        camera.isFitView == True
        camera.isSmoothTransition = True
        app.activeViewport.camera = camera
        
 
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
 
main()

 

Message 7 of 13
ekinsb
in reply to: FireBallFab

What you have is good.  You're just missing a viewPort.refresh() call after you set the camera.

 

Unfortunately, I've identified a problem where when setting the camera of a viewport the up vector is being ignored.  I've reported the problem and expect it to be fixed in a future version.

 

Where's what I was playing with.  Python is new to me too so there is probably a more direct way of accomplishing some of this, but it does seem to work. The script below captures the current view information and saves it to a file.

 

import adsk.core, adsk.fusion, traceback, ast

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

        view = app.activeViewport
        cam = view.camera
        
        eye = cam.eye
        target = cam.target
        up = cam.upVector
        if cam.cameraType != adsk.core.CameraTypes.OrthographicCameraType:
            perspAngle = cam.perspectiveAngle
        else:
            perspAngle = 0
        cameraData = dict(eye=str(eye.x) + ',' + str(eye.y) + ',' + str(eye.z),
                          target=str(target.x) + ',' + str(target.y) + ',' + str(target.z),
                          up=str(up.x) + ',' + str(up.y) + ',' + str(up.z),
                          cameraType=str(cam.cameraType),
                          perspectiveAngle=str(perspAngle))

        f = open('c:\\temp\\Fusion360Camera.dat', 'w')
        f.write(str(cameraData))
        f.close()
                
        ui.messageBox('Finished saving the view information.')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

main()

 

This next script then reads the file and applies it to the current active view port.  This works except for the up vector issue.

import adsk.core, adsk.fusion, traceback, ast

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

        f = open('c:\\temp\\Fusion360Camera.dat', 'r')
        newCameraString = f.readlines()
        newCamera = ast.literal_eval(newCameraString[0])
        f.close()

        newEyeString = newCamera['eye'].split(',')
        newTargetString = newCamera['target'].split(',')
        newUpVectorString = newCamera['up'].split(',')
        newCameraTypeString = newCamera['cameraType']
        newPerspectiveAngleString = newCamera['perspectiveAngle']
        
        newEye = adsk.core.Point3D.create(float(newEyeString[0]), float(newEyeString[1]), float(newEyeString[2]))
        newTarget = adsk.core.Point3D.create(float(newTargetString[0]), float(newTargetString[1]), float(newTargetString[2]))
        newUp = adsk.core.Vector3D.create(float(newUpVectorString[0]), float(newUpVectorString[1]), float(newUpVectorString[2]))
        newCameraType = int(newCameraTypeString)
        if newCameraType != adsk.core.CameraTypes.OrthographicCameraType:
            newPerspective = float(newPerspectiveAngleString)

        view = app.activeViewport
        cam = view.camera
        
        cam.up = newUp
        cam.eye = newEye
        cam.target = newTarget
        if newCameraType != cam.cameraType:
            cam.cameraType = newCameraType
            
            if newCameraType != adsk.core.CameraTypes.OrthographicCameraType:
                cam.perspectiveAngle = newPerspective
        
        cam.isFitView = True
        cam.isSmoothTransition = True        
        
        view.camera = cam
        view.refresh()
        
        cam = view.camera
        
                
        ui.messageBox('View restored.')

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


main()

 

 

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 8 of 13
ThilaknathRao
in reply to: FireBallFab

Your code is  just fine. Immediately after executing your code, invoking the orbit command crashes Fusion.

 

-Thilak

Message 9 of 13
marshaltu
in reply to: FireBallFab

Hello,

 

I changed your codes a little bit and it seemed to work as expected.

 

--------------------

import adsk.core, adsk.fusion, traceback

 

def main():

    ui = None

    try:

        app = adsk.core.Application.get()

        camera = app.activeViewport.camera

        #camera.viewOrientation = adsk.core.ViewOrientations.IsoTopLeftViewOrientation

        eye = camera.eye

        eye.x = -28.61

        eye.y = 13.98

        eye.z = -28.74

        camera.eye = eye

 

        target = camera.target

        target.x = 0.0

        target.y = -7.23

        target.z = -1.23

        camera.target = target

 

        upVector = camera.upVector

        upVector.x = 0.34

        upVector.y = 0.88

        upVector.z = 0.33

        camera.upVector = upVector

 

        #camera.isFitView == True

        camera.isSmoothTransition = True

        app.activeViewport.camera = camera

        app.activeViewport.fit()

 

    except:

        if ui:

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

 

main()

---------------------

 



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 10 of 13
marshaltu
in reply to: ThilaknathRao

Hello,

I gave a try to Brian's codes and didn't see the crash. Did you send crash report? If yes, please tell me the report number which display in web page after you send the report. Or please tell me your email address which you filled in Autodesk Error Report dialog. I can check what could be the problem?

Thanks,
Marshal


Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 11 of 13
FireBallFab
in reply to: marshaltu

Thanks for all of your help everyone. This has been painful, but learning new things sometimes is!

 

I have modified and bastardized the above examples into something that works for me for now - it doesn't seem to need the other angle thing that was mentioned.

 

Capture Camera State:

#Title-Capture_Camera_with_File.py
#Author-NE
#Description-Captures the open file's Camera state to a text file to be read and applied to other files. Use Change_Camera_with_File.py

import adsk.core, adsk.fusion, traceback

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

        # CHANGE TO APPROPRIATE PATH
        f = '/Users/Name/Fusion360_SaveCamera.txt'
        
        view = app.activeViewport
        cam = view.camera

        eye = cam.eye
        target = cam.target
        upvector = cam.upVector

        cameraData = dict(eye=str(eye.x) + ',' + str(eye.y) + ',' + str(eye.z),
                          target=str(target.x) + ',' + str(target.y) + ',' + str(target.z),
                          upvector=str(upvector.x) + ',' + str(upvector.y) + ',' + str(upvector.z),
                          cameraType=str(cam.cameraType))#, perspAngle=str(cam.perspectiveAngle))

        with open(f, 'wt') as io:
            io.write(str(cameraData))

        ui.messageBox('Finished saving the view information to ' +f+ '\n\n'
            'eye.x={0} \n'
            'eye.y={1} \n'
            'eye.z={2} \n\n'
            'target.x={3} \n'
            'target.y={4} \n'
            'target.z={5} \n\n'
            'upVector.x={6} \n'
            'upVector.y={7} \n'
            'upVector.z={8}'.format(eye.x, eye.y, eye.z, target.x, target.y, target.z, upvector.x, upvector.y, upvector.z),
            'Capturing The Current Camera View...')

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

main()

 Change Camera State:

#Title-Change_Camera_with_File.py
#Author-NE
#Description-Changes the camera view to the saved view in the text file. Used with Capture_Camera_with_File.py
import adsk.core, adsk.fusion, traceback, ast

def main():
    ui = None

    # CHANGE TO APPROPRIATE PATH
    f = '/Users/Name/Fusion360_SaveCamera.txt'

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

        with open(f, 'rt') as io:
            newCameraString = io.readlines()

        camera = app.activeViewport.camera
        newCamera = ast.literal_eval(newCameraString[0])

        newEyeString = newCamera['eye'].split(',')
        newTargetString = newCamera['target'].split(',')
        newUpVectorString = newCamera['upvector'].split(',')

        newEye = adsk.core.Point3D.create(float(newEyeString[0]), float(newEyeString[1]), float(newEyeString[2]))
        newTarget = adsk.core.Point3D.create(float(newTargetString[0]), float(newTargetString[1]), float(newTargetString[2]))
        newUp = adsk.core.Vector3D.create(float(newUpVectorString[0]), float(newUpVectorString[1]), float(newUpVectorString[2]))

        camera.target = newTarget
        camera.eye = newEye
        camera.upVector = newUp

        app.activeViewport.camera = camera
        app.activeViewport.fit()

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

main()

 

 

Message 12 of 13
matt
in reply to: FireBallFab

Wow - is there a way to do this without scripts?  If not is this something that could be implemented?

Message 13 of 13
i.fanariotis
in reply to: FireBallFab

Hello

 

Could I use this with OS X or is it for windows only?

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