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()