Autodesk Fusion 360 BOM Scripts

While I was cadding a base for Over Under, I thought that it might be helpful to have a way to extract a BOM from Fusion 360 and put it in the notebook. I edited a program to do that, but when I run it in Fusion, it generates parts out of nowhere and it doesn’t include some parts. Below is a photo of my chassis, the BOM fusion gives me, and the code. Could anyone help me find a way to generate a proper BOM list?
image

import adsk.core, adsk.fusion, traceback

def walkThrough(bom):
    mStr = ''
    for item in bom:
        if item['name'][len(item['name'])-2]=='v':
            mStr += (item['name'][:-2]+ ": ") + str((item['instances']))+"\n"
        else:
            mStr += (item['name']+ ": ") + str((item['instances']))+"\n"
    return mStr

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        title = 'Extract BOM'
        if not design:
            ui.messageBox('No active design', title)
            return

        # Get all occurrences in the root component of the active design
        root = design.rootComponent
        occs = root.allOccurrences
        
        # Gather information about each unique component
        bom = []
        for occ in occs:
            comp = occ.component
            jj = 0
            for bomI in bom:
                if bomI['component'] == comp:
                    # Increment the instance count of the existing row.
                    bomI['instances'] += 1
                    break
                jj += 1

            if jj == len(bom):
                
                # Add this component to the BOM
                bom.append({
                    'component': comp,
                    'name': comp.name,
                    'instances': 1,
                })

        # Display the BOM
        title = "Name: Instances"
        msg = title + '\n' + walkThrough(bom)
        
        ui.messageBox(msg, 'Bill Of Materials')

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

image

if you make a drawing of the assembly, Fusion will generate a BOM for you and you can copy that

A drawing of your assembly Could be a good thing to include in your notebook either way.

The problem with your script is that it is taking the base parts and printing them, instead of the larger subassemblies. the program is printing out a full BOM, and I definitely see a use of it, if you are trying to find, say how many of a type of bolt (or other part) are on your robot.