Create a script that exports each combination of components?

Create a script that exports each combination of components?

Anonymous
Not applicable
1,349 Views
4 Replies
Message 1 of 5

Create a script that exports each combination of components?

Anonymous
Not applicable

So I have a workflow, but I could adapt this workflow however we need to do, the end product is a list of files with a combination of options.

What I do right now is:

Component 1: contains the base objects, this remains no matter what, everything under here we ignore
Component 2: contains sub-components, lets say A, B, C  , these could be all bodies under component 2, or individual components, 

We need to export each combination of Component 1 and 2, so in the above test case we'd export the following as an stl (or obj)
Component 1 + component 2A
Component 1 + component 2B
Component 1 + component 2C

Right now my process is entirely manual, but as the number of "options" and "component 1's" increase, this is rapidly getting untenable. the exported files could just be numbered if grabbing the name of the component or body isn't doable.

I am somewhat versed in using VBA scripts, I'm just having a lot of difficulty getting something that even begins to work so I can tune in the code to something workable.  Specifically I can't figure out how to pull a list of the components (or bodies) to even get started with the loop code.

Any help would be appreciated, thanks!

0 Likes
Accepted solutions (1)
1,350 Views
4 Replies
Replies (4)
Message 2 of 5

kandennti
Mentor
Mentor
Accepted solution

Hi @Anonymous .

 

I made a sample that exports STL while switching isLightBulbOn.

#Fusion360API Python script
import adsk.core, adsk.fusion, traceback
import re, os

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # get Component
        allComps :adsk.fusion.Components = des.allComponents
        comp1 :adsk.fusion.Component = allComps.itemByName('Component1')
        comp2 :adsk.fusion.Component = allComps.itemByName('Component2')

        # get OccurrenceList
        occLst_comp1 :adsk.fusion.OccurrenceList = root.occurrencesByComponent(comp1)
        occLst_comp2 :adsk.fusion.OccurrenceList = root.occurrencesByComponent(comp2)

        # Record the display status
        # root.bRepBodies
        body :adsk.fusion.BRepBody
        rootBodiesLightStateList = []
        for body in root.bRepBodies:
            rootBodiesLightStateList.append([body, body.isLightBulbOn])
            body.isLightBulbOn = False

        # Occurrence
        occ :adsk.fusion.Occurrence
        occLightStateList = []
        for occ in root.allOccurrences:
            occLightStateList.append([occ, occ.isLightBulbOn])
            occ.isLightBulbOn = False


        # export stl
        exportFolder = r'C:\temp'
        exportMgr = des.exportManager

        occ1 :adsk.fusion.Occurrence
        occ2 :adsk.fusion.Occurrence
        for occ1 in occLst_comp1:
            occ1.isLightBulbOn = True
            occ1ExpName = re.sub(r'[\\|/|:|?|.|"|<|>|\|]', '-', occ1.name)
            for occ2 in occLst_comp2:
                occ2.isLightBulbOn = True

                occ2ExpName = re.sub(r'[\\|/|:|?|.|"|<|>|\|]', '-', occ2.name)
                expName = f'{occ1ExpName}_{occ2ExpName}'
                expPath = os.path.join(exportFolder, f'{expName}.stl')
                stlOpts = exportMgr.createSTLExportOptions(root, expPath)
                exportMgr.execute(stlOpts)

                occ2.isLightBulbOn = False
            occ1.isLightBulbOn = False


        # Restore the display state.
        for body, light in rootBodiesLightStateList:
            body.isLightBulbOn = light

        for occ, light in occLightStateList:
            occ.isLightBulbOn = light

        _ui.messageBox('Done')

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

1.png
I think a more detailed check is needed.

Message 3 of 5

Anonymous
Not applicable

Sorry for the late reply @kandennti 

This seems to work, ALMOST!  with a slight GUI related error I think

When I created test components to test the script, I can get the combination component 1-1 component 2-1 to export, it works great

The issue i'm running into, is I can create components 1 and 2, they get renamed to Component 1:1 and component 2:1 automatically upon creation

Upon trying to create another component 2, it is renamed to component 2 (1):1
Trying to rename it to component 2:2, it is being renamed to component 2:2:1

So i'm missing something about fusions automatic naming convention that is letting me iterate that number after the :
Could you let me know what the trick is? 🙂



0 Likes
Message 4 of 5

Anonymous
Not applicable

I did a bunch more experimentation, and I think i've located the issue @kandennti 

You're using "

 comp1 :adsk.fusion.Component = allComps.itemByName('Component1')" to get a list of components that match the name "component1"  the :1 at the end is the issue
Fusion appears to add the :1 :2 :3, ect.  for linked components, meaning that we need to get our lists another way.
As an example: Since component1:2 component 1:3, all have to be identical to component1:1 with this naming convention, this won't work.

I played around with the code a bit, and can't seem to get a wildcard to work, but I feel like changing:
comp1 :adsk.fusion.Component = allComps.itemByName('Component1')

to

comp1 :adsk.fusion.Component = allComps.itemByName('Component1*')

so that comp1 (or comp2) also grabs any component that starts with the name "component1" but with anything afterwards, would be fully usable, I can change my naming convention to match it and away we go.

I feel like thats the easy solution as your script is 99% of the way there, but haven't hit my head against it enough to figure out how/if i can get it work XD
0 Likes
Message 5 of 5

kandennti
Mentor
Mentor

@Anonymous .

 

Perhaps you do not understand Occurrence.

It is certainly confusing because the word "Occurrence" does not appear in the GUI.

The naming of the component in the GUI looks like this.

1.png

The naming of Components in the GUI is like this, and it seems that different Components cannot have the same name.
(I think that's correct.)

 

First of all, I recommend you to read this article to understand Occurrence.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A 

0 Likes