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: 

Can a child component be hidden and show only the parent with Python?

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
tallman5
274 Views, 2 Replies

Can a child component be hidden and show only the parent with Python?

I have a Python script for exporting images of components. If a parent component has a child component and bodies, I'm trying to get an image of just the parent's bodies. The child components should be hidden.

 

There's an attached sample file and the code is below. The parent component has a cube body. The child component has a cylinder body. The parent image should be of only the cube. However, I can't get the viewport to show the cube and hide the cylinder. Thanks in advance!

 

 

 

 

 

import adsk.core, adsk.fusion, adsk.cam, traceback

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

        activeProduct = app.activeProduct
        design = adsk.fusion.Design.cast(activeProduct)

        rootFolder = None
        folderDialog = ui.createFolderDialog()
        folderDialog.title = "Selcect a folder for images"
        dialogResult = folderDialog.showDialog()
        if dialogResult == adsk.core.DialogResults.DialogOK:
            rootFolder = folderDialog.folder + "\\"
        else:
            return

        for occurrence in design.rootComponent.allOccurrences:

            if occurrence.bRepBodies.count > 0 and occurrence.childOccurrences.count > 0:
                for childOccurrence in occurrence.childOccurrences:
                    # childOccurrence.isVisible = False # doesn't work, isVisible is readonly
                    hideIt = True # stuck here

            imageFileName = occurrence.component.partNumber + ".png"
            occurrence.activate()
            occurrence.isIsolated = True
            viewPort.fit()
            viewPort.saveAsImageFile(rootFolder + imageFileName, 100, 100)

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

 

 

 

 

2 REPLIES 2
Message 2 of 3
kandennti
in reply to: tallman5

Hi @tallman5 .

 

The Occurrence.isLightBulbOn property toggles between showing and hiding.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-203a9b47-df8f-4373-9535-b3df3d8e558f 

 

Fixed like this.

import adsk.core, adsk.fusion, adsk.cam, traceback

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

        activeProduct = app.activeProduct
        design = adsk.fusion.Design.cast(activeProduct)

        rootFolder = None
        folderDialog = ui.createFolderDialog()
        folderDialog.title = "Selcect a folder for images"
        dialogResult = folderDialog.showDialog()
        if dialogResult == adsk.core.DialogResults.DialogOK:
            rootFolder = folderDialog.folder + "\\"
        else:
            return

        for occurrence in design.rootComponent.allOccurrences:
            # LightBulbOn Information
            occs_Light_Info = None

            # if occurrence.bRepBodies.count > 0 and occurrence.childOccurrences.count > 0:
            if occurrence.bRepBodies.count < 1:
                continue

            if occurrence.childOccurrences.count > 0:
                # Backup LightBulbOn Information
                occs_Light_Info = [(o, o.isLightBulbOn) for o in occurrence.childOccurrences]

                # childOccurrences hide
                for occ, _ in occs_Light_Info:
                    occ.isLightBulbOn = False

            imageFileName = occurrence.component.partNumber + ".png"
            # occurrence.activate()
            occurrence.isIsolated = True
            viewPort.refresh()
            viewPort.fit()
            viewPort.saveAsImageFile(rootFolder + imageFileName, 100, 100)

            # Recovery LightBulbOn
            if occs_Light_Info:
                for occ, lightInfo in occs_Light_Info:
                    occ.isLightBulbOn = lightInfo

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

 

It works fine with the data you attached, but with the data I attached, the isLightBulbOn property does not end up in the correct state at the end.

Message 3 of 3
tallman5
in reply to: tallman5

Thanks @kandennti ! My UI doesn't have the lightbulbs in the browser tree, so, never thought to look there. FYI, the code below returns the occurrences to a correct state. Thanks again!

# ...
hiddenOccurrences = []
if occurrence.childOccurrences.count > 0:
    for childOccurrence in occurrence.childOccurrences:
        if childOccurrence.isLightBulbOn:
            childOccurrence.isLightBulbOn = False
            hiddenOccurrences.append(childOccurrence)
# ...
for ho in hiddenOccurrences:
    ho.isLightBulbOn = True
# ...

 

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