Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Bug. Impossible to save named view with perspective camera. Resulting camera has very different eye.

soswow
Advocate

Bug. Impossible to save named view with perspective camera. Resulting camera has very different eye.

soswow
Advocate
Advocate

Here is the simplest code:

 

 

 

 

#Author-
#Description-

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

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

        design = Design.cast(app.activeProduct)
        camera = app.activeViewport.camera
        camera.perspectiveAngle = math.radians(35)

        design.namedViews.add(camera)

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

 

 

 

 

 

When I execute this, it creates named view for the current camera position and orientation. But when I click on that view it moves far away from where camera was/is.

 

So it that was the original position when script is ran:

soswow_0-1728555105911.png

then after clicking on saved named view it goes way back:

soswow_1-1728555148659.png

 

Did some debugging and logging.

 

 

 

 

 

# Example of current camera when saving as named view:

current camera details. eye: x: 209.2645 mm, y: -27.2470 mm, z: 97.5599 mm target: x: -8.3656 mm, y: -10.8403 mm, z: 15.7369 mm up_vector: x: -2.7197 mm, y: 4.9824 mm, z: 8.2328 mm

# CAmera that is attached to saved named view
saved view camera details. eye: x: 3442.8070 mm, y: -271.0171 mm, z: 1313.2829 mm target: x: -8.3656 mm, y: -10.8403 mm, z: 15.7369 mm up_vector: x: -2.7197 mm, y: 4.9824 mm, z: 8.2328 mm

 

 

 

 

Even if I construct camera myself from scratch:

 

 

camera = app.activeViewport.camera

        new_camera = Camera.create()
        new_camera.cameraType = CameraTypes.PerspectiveCameraType
        new_camera.perspectiveAngle = math.radians(40)

        new_camera.isFitView = False
        new_camera.isSmoothTransition = True
        new_camera.eye = camera.eye.copy()
        new_camera.target = camera.target.copy()
        new_camera.upVector = camera.upVector.copy()
        
        app.activeViewport.camera = new_camera
        
        design.namedViews.add(new_camera)

 

 

It would still stores much farther camera

0 Likes
Reply
333 Views
5 Replies
Replies (5)

soswow
Advocate
Advocate

Still haven't found a workaround. Except for maybe creating it manually in UI which is not what I incision as a workflow for my plugin..

0 Likes

soswow
Advocate
Advocate

It's something to do with it being perspective ... 

Look at this:

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

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

        design = Design.cast(app.activeProduct)
        
        while design.namedViews.count > 0:
            design.namedViews.item(0).deleteMe()
        
        for i in range(10):
            fov = 20 + i * 5
            new_camera = Camera.create()
            new_camera.cameraType = CameraTypes.PerspectiveCameraType
            new_camera.perspectiveAngle = math.radians(fov)
            new_camera.isFitView = False
            new_camera.isSmoothTransition = False
            new_camera.eye = Point3D.create(0, 0, 10)
            new_camera.target = Point3D.create(0, 0, 0)
            new_camera.upVector = Vector3D.create(0, 1, 0)
            
            new_named_view = design.namedViews.add(new_camera)
            app.log(f'fov: {fov} with camera.eye.z == 10 named view cameras becomes {new_named_view.camera.eye.z}')

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

 

This generates this:

 fov: 20 with camera.eye.z == 10 named view cameras becomes 8040.859369381589
 fov: 25 with camera.eye.z == 10 named view cameras becomes 5086.622801252298
 fov: 30 with camera.eye.z == 10 named view cameras becomes 3482.0508075688767
 fov: 35 with camera.eye.z == 10 named view cameras becomes 2514.7533975943365
 fov: 40 with camera.eye.z == 10 named view cameras becomes 1887.1580426032576
 fov: 45 with camera.eye.z == 10 named view cameras becomes 1457.1067811865469
 fov: 50 with camera.eye.z == 10 named view cameras becomes 1149.7274830283475
 fov: 55 with camera.eye.z == 10 named view cameras becomes 922.5430830356662
 fov: 60 with camera.eye.z == 10 named view cameras becomes 750.0000000000005
 fov: 65 with camera.eye.z == 10 named view cameras becomes 615.9782027526672

new z changes depending on camera's FOV

in this relationship:

soswow_0-1728643429568.png

 

0 Likes

soswow
Advocate
Advocate

I am more then sure it is a bug.

0 Likes

kandennti
Mentor
Mentor

@soswow -San.

 

I was curious so I tested it out to try a few things.

# Fusion360API Python script

import traceback
import adsk.core as core

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

        vp: core.Viewport = app.activeViewport
        cam: core.Camera = vp.camera

        dumpCam(cam)

        cam.cameraType = core.CameraTypes.OrthographicCameraType
        vp.camera = cam
        dumpCam(cam)

        cam.cameraType = core.CameraTypes.PerspectiveCameraType
        vp.camera = cam # <-error
        dumpCam(cam)

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

def dumpCam(cam: core.Camera) -> None:
    dump("","")
    dump("type", cam.cameraType)
    dump("eye",cam.eye.asArray())
    dump("target",cam.target.asArray())
    dump("dist",cam.eye.distanceTo(cam.target))
    dump("perspectiveAngle",cam.perspectiveAngle)
    try:
        returnValue, width, height = cam.getExtents()
        dump(f"{returnValue}:{width}:{height}","")
    except:
        pass

def dump(msg, x) -> None:
    core.Application.get().log(f"{msg}:{x}")

 

I got an error when switching the camera to Perspective. (ver 2.0.20476)
This used to work before. There seems to be another bug here.

0 Likes