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: 

Exporting A design to multiple STL's with their location ant rotation data.

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
petrikas.m
734 Views, 5 Replies

Exporting A design to multiple STL's with their location ant rotation data.

Hello All,
I'm enjoying Fusion360 so much, I'm wanting to dig deeper and seeing what can be automated.

Right now I'm using Fusion360 for modelling and Blender Cycles for rendering, which means I have to Export to STL.
The most ideal setting for me with the default STL exporter is to export as multiple files. It works well, however, it exports single bodies, and creates rather convoluted filenames, that are a pain to work with.

I've written this simple script that exports occurrences, which means I can have a component with separate bodies, acting as one part. However, they get imported at their own origin, with no location or rotation data. Which means that when I import all into Blender, I get a huge mess of parts (Components), whereas using Fusion's Built-in exporter, I get all parts (Bodies) in their rotated (and moved) positions.

The ideal order of events for this operation are:
1. Press a button "STL Export" (not included in the code)
2. Select folder

3. Files exported in "high" setting, with no extra dialogs (or user input), with their Component names.


Any simple solutions to this? Thank you all in advance.
Here's an excerpt of my plugin so far, it's a very rough draft - apologies for that;

class CommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            app = adsk.core.Application.get()
            ui  = app.userInterface
            
            #msg = ''
            # Set styles of file dialog.
            fileDlg = ui.createFileDialog()
            fileDlg.isMultiSelectEnabled = False
            fileDlg.title = 'Fusion File Dialog'
            fileDlg.filter = '*.*'
                
            # Set styles of file dialog.
            folderDlg = ui.createFolderDialog()
            folderDlg.title = 'Fusion Folder Dialog' 
            
            # Show folder dialog
            dlgResult = folderDlg.showDialog()
            if dlgResult == adsk.core.DialogResults.DialogOK:
                #msg += '\nSelected folder: {}'.
                format(folderDlg.folder) 
            
            #ui.messageBox(msg)
            
            product = app.activeProduct
            design = adsk.fusion.Design.cast(product)
            
            rootComp = design.rootComponent
            
            exportMgr = design.exportManager
            
            # export the component one by one with a specified format
            allOccu = rootComp.allOccurrences
            for occ in allOccu:
            
            # create stl exportOptions
                fileName = folderDlg.folder + "/" + occ.component.name
                stlExportOptions = exportMgr.createSTLExportOptions(occ, fileName)
                stlExportOptions.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementHigh
                stlExportOptions.sendToPrintUtility = False
                exportMgr.execute(stlExportOptions)

        except:
            if ui:
                ui.messageBox('STL Export Failed / Cancelled')

 

 

@MPetrikas
5 REPLIES 5
Message 2 of 6
kandennti
in reply to: petrikas.m

Hi petrikas.m.

 

"isOneFilePerBody"  property seems to be useful.

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        # Set styles of file dialog.
        fileDlg = ui.createFileDialog()
        fileDlg.isMultiSelectEnabled = False
        fileDlg.title = 'Fusion File Dialog'
        fileDlg.filter = '*.*'
            
        # Set styles of file dialog.
        folderDlg = ui.createFolderDialog()
        folderDlg.title = 'Fusion Folder Dialog' 
        
        # Show folder dialog
        dlgResult = folderDlg.showDialog()
        if dlgResult == adsk.core.DialogResults.DialogOK:
            format(folderDlg.folder) 
        
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        
        rootComp = design.rootComponent
        exportMgr = design.exportManager
        
        #export STL
        stlExportOptions = exportMgr.createSTLExportOptions(rootComp, folderDlg.folder + "/")
        stlExportOptions.isOneFilePerBody = True #This Property
        stlExportOptions.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementHigh
        stlExportOptions.sendToPrintUtility = False
        exportMgr.execute(stlExportOptions)
        
        ui.messageBox('Done')
    except:
        if ui:
            ui.messageBox('STL Export Failed / Cancelled')

 

 

Message 3 of 6
petrikas.m
in reply to: kandennti

Hey @kandennti, thanks for the reply!
Yes, isOneFilePerBody fixes my immediate problem of position and rotation, but creates another - multiple files for the same component. For rendering, having all e.g. bolts in one component is a life-saver.
I guess I have to work with what I have.
Thanks again!

@MPetrikas
Message 4 of 6
kandennti
in reply to: petrikas.m

It was possible to export with RootComponent using RootCompBodies and Occurrence LightBulbOn On or Off.
try it.

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        # Set styles of file dialog.
        fileDlg = ui.createFileDialog()
        fileDlg.isMultiSelectEnabled = False
        fileDlg.title = 'Fusion File Dialog'
        fileDlg.filter = '*.*'
            
        # Set styles of file dialog.
        folderDlg = ui.createFolderDialog()
        folderDlg.title = 'Fusion Folder Dialog' 
        
        # Show folder dialog
        dlgResult = folderDlg.showDialog()
        if dlgResult == adsk.core.DialogResults.DialogOK:
            format(folderDlg.folder) 
        
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        root = design.rootComponent
        allOcc = root.allOccurrences
        
        #Save State LightBulbOn
        lightOnLst = []
        
        #rootComp BodiesFolder
        if root.isBodiesFolderLightBulbOn:
            lightOnLst = [root]
        
        #Occurrence LightBulbOn List
        occLightOnLst = [occ for occ in allOcc if occ.isLightBulbOn == True]
            
        lightOnLst.extend(occLightOnLst)
        
        #LightBulbOn All Off
        for itm in lightOnLst:
            LightBulbOn(itm, False)       
        
        #export STL
        exportMgr = design.exportManager
        meshHigh = adsk.fusion.MeshRefinementSettings.MeshRefinementHigh
        for itm in lightOnLst:
            
            LightBulbOn(itm, True)
            
            if isinstance(itm, adsk.fusion.Component):
                fileName = folderDlg.folder + "/" + itm.name
            else:
                fileName = folderDlg.folder + "/" + itm.component.name
                
            stlExportOptions = exportMgr.createSTLExportOptions(root, fileName)
            stlExportOptions.meshRefinement = meshHigh
            stlExportOptions.sendToPrintUtility = False
            exportMgr.execute(stlExportOptions)

            LightBulbOn(itm, False)
        
        #Reproduction LightBulbOn
        for itm in lightOnLst:
            LightBulbOn(itm, True)
        
        ui.messageBox('Done')
    except:
        if ui:
            ui.messageBox('STL Export Failed / Cancelled')

def LightBulbOn(obj, on_off):
    if isinstance(obj, adsk.fusion.Component):
        obj.isBodiesFolderLightBulbOn = on_off
    else:
        obj.isLightBulbOn = on_off
Message 5 of 6
petrikas.m
in reply to: kandennti

Apologies for resurrecting an old thread... And thanks @kandennti for your help!
Since newer Fusion360 replaced light-bulbs with "eye" icons, I'd need to update this script. Is there any documentation on it? Thanks in advance.

@MPetrikas
Message 6 of 6
kandennti
in reply to: petrikas.m

Hi @petrikas.m .

 

I'm sorry. I've already forgotten some of the details.

The icons have been changed to "eye", but the API property names should not have changed,

so I don't think there is any change in behavior.

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7cd5b52f-26bb-4890-9c37-2dc48213d685 

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-b56b394e-b9c9-4403-a78b-a186b897909c 

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