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: 

Trying to create a Python script to animate Fusion 360 camera

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
george
4044 Views, 2 Replies

Trying to create a Python script to animate Fusion 360 camera

Hi there,

 

I am trying to write a Python script to animate the camera so that it circles the currently selected model. I can programmatically manipulate the camera in Python, but I can't figure out how to do it within a loop, because I keep locking up the program (running an infinite loop in a script seems to hog execution from doing anything in the Fusion 360 UI, so it locks up). I'm wondering if there are multithreading/delegate solutions that may work. I took a few minutes to try calling a method to move the camera via threading.Timer, but it's not working. I am not a Python master by any means, so there might be something simple I'm missing

 

I ran a few Google searches and looked through this forum, the example code, and the API documentation, and I am still a bit stumped.

 

Here's the last script I tried (I was just trying to update the camera once from a separate thread at this point, and by the way, I couldn't even get the timer executed method to print to the console. I do understand if the console stream is a resource only available from the main thread though):

import adsk.core, adsk.fusion, traceback, threading

def move_camera():
    global app
    global camera
    eye = camera.eye
    eye.x += 1.0
    eye.y += 1.0
    eye.z += 1.0
    camera.eye = eye
    
    target = camera.target
    target.x += 1.0
    target.y += 1.0
    target.z += 1.0
    camera.target = target
    
    camera.isSmoothTransition = False
    app.activeViewport.camera = camera
    app.activeViewport.refresh()
    
    print('hello from a timer call')
  

def main():
  ui = None
  
  global app
  global camera
  try:
    print('hello, world')
    app = adsk.core.Application.get()
    camera = app.activeViewport.camera
    t = threading.Timer(0.1, move_camera)
    t.start()
    
  except:
    if ui:
      ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
      
main()

 

Fusion 360 is a really neat program by the way. ...and thanks for reading!

2 REPLIES 2
Message 2 of 3
ekinsb
in reply to: george

Here's a modified version of your program that behaves as I would expect.  I changed it so the move_camera now has a loop so the camera is being animated and the big change, (which was probably causing your problem), is that I added a call to the doEvents function.  Python runs in-process to Fusion and apparrently Fusion isn't able to keep up with the event queue as these API calls are being made.  Calling doEvents gives it a chance to catch up each time the camera is set.  If I comment out the doEvents call it functions correctly for about half the rotation and then Fusion freezes up until it's finished the full rotation and then the view refreshes. 

 

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

def move_camera(app, view):
    try:
        camera = view.camera
        
        target = adsk.core.Point3D.create(0,0,0)
        up = adsk.core.Vector3D.create(0,0,1)
        steps = 1000
        
        dist = camera.target.distanceTo(camera.eye)
    
        for i in range(0, steps):
            eye = adsk.core.Point3D.create(dist * math.cos((math.pi*2) * (i/steps)), dist * math.sin((math.pi*2) * (i/steps)), 10)
            
            camera.eye = eye
            camera.target = target
            camera.upVector = up
        
            camera.isSmoothTransition = False
            view.camera = camera
            adsk.doEvents()
            view.refresh()
    except:
        ui = app.userInterface
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def main():
    ui = None
  
    try:
        print('hello, world')
        app = adsk.core.Application.get()
        move_camera(app, app.activeViewport)
    
    except:
        ui = app.userInterface
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

      
main()

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3
george
in reply to: ekinsb

Wow, that really solves my problem! Thanks. That adsk.doEvents call was exactly what I was looking for.

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