I have this little helper function I use at the beginning of a script. It closes the document with the given name, creates and actives a new document with the same name, and then sets the viewport in the new document to match that of the document that was just closed.
I find the viewport thing in particular to be useful when doing iterative design via the API. Otherwise, I find that I'm always having to move the viewport back to whatever I was just looking at, after making a tweak in the script and re-running it.
def setup_document(document_name="ScriptDocument"):
app = adsk.core.Application.get()
preview_doc = None # type: adsk.fusion.FusionDocument
saved_camera = None
for document in app.documents:
if document.name == document_name:
preview_doc = document
break
if preview_doc is not None:
preview_doc.activate()
saved_camera = app.activeViewport.camera
preview_doc.close(False)
preview_doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
preview_doc.name = document_name
preview_doc.activate()
if saved_camera is not None:
is_smooth_transition_bak = saved_camera.isSmoothTransition
saved_camera.isSmoothTransition = False
app.activeViewport.camera = saved_camera
saved_camera.isSmoothTransition = is_smooth_transition_bak
app.activeViewport.camera = saved_camera