Pan, orbit, zoom, camera in Fusion 360 api

Pan, orbit, zoom, camera in Fusion 360 api

vareznikovk
Community Visitor Community Visitor
970 Views
2 Replies
Message 1 of 3

Pan, orbit, zoom, camera in Fusion 360 api

vareznikovk
Community Visitor
Community Visitor

I get 6 numbers from a 3D mouse.

(x, y, z, yaw, roll, pitch)

How do I change the camera coordinates in the python fusion 360 api in order to do pan, orbit, zoom operations?

My code addin python code bellow:

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

import os, sys  

from . import commands 

from .lib import fusion360utils as futil 

 

install\_ = sys.path\[0\] +'\\Python\\python.exe -m pip install pyserial'  

os.system('cmd /c "' + install\_ + '"')  

 

import serial 

ser = serial.Serial('COM8', 9600) 

def move_camera(app, view):  

    while(True): 

        try:  

            camera = view.camera  

             

            target = camera.target  # Use current camera target  

            up = adsk.core.Vector3D.create(0, 1, 0)  

            data = ser.readline().decode('utf-8').strip().split(' ')  

            if len(data) == 6:

                \# x y z yaw pitch roll 

                dist = camera.target.distanceTo(camera.eye) 

                start_eye = camera.eye  

                \# Pan  

                new_target = adsk.core.Point3D.create(target.x, target.y, target.z)  

                camera.target = new_target  

     

                \# Orbit  

                new_eye = adsk.core.Point3D.create(dist \* math.cos((math.pi\*2) \* data\[3\]), dist \* math.sin((math.pi\*2) \* data\[4\]), dist \* math.sin((math.pi\*2) \* data\[5\])) 

                camera.eye = new_eye  

 

                #zoom 

                zoom_factor = float(data\[0\]) 

                new_target = adsk.core.Point3D.create(target.x \* zoom_factor, target.y \* zoom_factor, target.z \* zoom_factor) 

                camera.target = new_target 

 

                camera.upVector = up  

                camera.isSmoothTransition = True  

                view.camera = camera  

            else: 

                break 

            adsk.doEvents()  

            view.refresh()  

            time.sleep(0.005)  

                     

        except:  

            ui = app.userInterface  

            if ui:  

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

                stop() 

 

 

 

def run(context): 

    try: 

        \# This will run the start function in each of your commands as defined in commands/\__init_\_.py 

        commands.start() 

        main() 

    except: 

        futil.handle_error('run') 

 

 

def stop(context): 

    try: 

        \# Remove all of the event handlers your app has created 

        futil.clear_handlers() 

        commands.stop() 

 

    except: 

        futil.handle_error('stop') 

 

 

def main(): 

    ui = None 

   

    try: 

        app = adsk.core.Application.get() 

        move_camera(app, app.activeViewport) 

     

    except: 

        ui = app.userInterface 

        if ui: 

            ui.messageBox('Failed:\\n{}'.format(traceback.format_exc()))
971 Views
2 Replies
Replies (2)
Message 2 of 3

ukdavewood6ZZPU
Observer
Observer

I do panning and zooming using named views - and then code like this to switch the Camera between views - with some delays in between.

 

 

for search in search2.split(';'):

                if search.upper() == "C":
                    CenterAndShowInWindowAllVisibleObjects(app)
                elif search.upper() == "V1":
                    app.executeTextCommand(u'NamedView.RestoreCamera View1')
                elif search.upper() == "V2":
                    app.executeTextCommand(u'NamedView.RestoreCamera View2')
                elif search.upper() == "V3":
                    app.executeTextCommand(u'NamedView.RestoreCamera View3')
                elif search.upper() == "P":
                    time.sleep(1)

 

Screenshot 2024-07-19 at 12.30.00.png

 

(view in My Videos)

0 Likes
Message 3 of 3

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

I can see in you script that you are running the move_camera() function from the main function in the script.

Running it from there blocks the Fusion main thread which it needs to make all the operations and update the results in the UI.

 

I'd suggest to implement it this way:

- start a thread from the addIn which listens to the mouse events and sends custom events to the main thread (this way it wont block the main thread)

- implement an event handler which listen from custom events and update the UI (remind that only the main thread could do so).

This two blocks need to be started from the addIn, not from the main() function as a script.

 

Regarding the data you receive from you mouse: could you post some samples of what it sends?

 

I hope this could help.

 

Regards,

Jorge Jaramillo

 

 

0 Likes