Determine object visibility.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Determine object visibility.

Post by Roy_043 »

Unnested Body results in "TypeError: 'NoneType' object is not iterable".
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Determine object visibility.

Post by TheMarkster »

Roy_043 wrote: Fri Oct 22, 2021 6:07 pm Unnested Body results in "TypeError: 'NoneType' object is not iterable".
Ah, so it returns None instead of an empty list [].

Code: Select all

def getParents(obj):
    if hasattr(obj,"Parents") and obj.Parents:
        return [obj.Parents[0][0]]+[FreeCAD.ActiveDocument.getObject(p) for p in obj.Parents[0][1].split('.') if obj != FreeCAD.ActiveDocument.getObject(p) and p != '']

def isVisible(obj):
    if not obj.ViewObject.Visibility:
        return False
    if not obj.VisibleFeature:
        return False
    parents = getParents(obj)
    if not parents:
        return True
    for par in parents:
        if not par.ViewObject.Visibility:
            return False
    return True

bodies = [obj for obj in FreeCAD.ActiveDocument.Objects if obj.TypeId == "PartDesign::Body"]
FreeCADGui.Selection.clearSelection()
for body in bodies:
    if isVisible(body):
        FreeCADGui.Selection.addSelection(FreeCAD.ActiveDocument.Name,body.Name)
    
Post Reply