How do you get a reference to Body by name?

How do you get a reference to Body by name?

valgrind
Explorer Explorer
661 Views
2 Replies
Message 1 of 3

How do you get a reference to Body by name?

valgrind
Explorer
Explorer

I'm making a script that exports >50 3D printable objects from a few bodies by changing parameters with code. That part is fairly simple as long as you want to save everything that is currently visible, just feed ExportManager the rootComponent after making the changes. Except it would be nice to save specific named objects instead of the whole scene. How to do that?

 

So in other words, what should I use to export the Body 'box-top' instead of rootComponent in the below pseudo-code?

rootComp = design.rootComponent
...
for something in range(x,y):
    fiddle_with_parameters()
    ...
    exportMgr = adsk.fusion.ExportManager.cast(design.exportManager)
    stlOptions = exportMgr.createC3MFExportOptions(rootComp)
    ...
    exportMgr.execute(stlOptions)

 

0 Likes
Accepted solutions (1)
662 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor
Accepted solution

Hi @valgrind -San.

 

To export as STL, use createSTLExportOptions.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-87489be2-dca3-4ca8-9c18-b78c7dd8686d 

 

It is also possible to export only a specific Body.

A sample script is available here, but we have simply extracted the process of exporting the Body separately.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-ECA8A484-7EDD-427D-B1E3-BD59A646F4FA 

# Fusion360API Python script
import adsk.core, adsk.fusion, traceback
import os.path
    
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        
        # get active design        
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        
        # get root component in this design
        rootComp = design.rootComponent
        
        # create a single exportManager instance
        exportMgr = design.exportManager

        # get the script location
        scriptDir = os.path.dirname(os.path.realpath(__file__))  

        # export the body one by one in the design to a specified file
        allBodies = rootComp.bRepBodies
        for body in allBodies:
            if body.name != 'box-top': continue

            fileName = scriptDir + "/" + body.parentComponent.name + '-' + body.name
            
            # create stl exportOptions
            stlExportOptions = exportMgr.createSTLExportOptions(body, fileName)
            stlExportOptions.sendToPrintUtility = False
            
            exportMgr.execute(stlExportOptions)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Message 3 of 3

valgrind
Explorer
Explorer

Perfect, the rootComp.bRepBodies loop was exactly what I was looking for!