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

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

tallman5
Participant Participant
770 Views
2 Replies
Message 1 of 3

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

tallman5
Participant
Participant

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()))

 

 

 

 

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

kandennti
Mentor
Mentor
Accepted solution

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.

0 Likes
Message 3 of 3

tallman5
Participant
Participant

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
# ...

 

0 Likes