Connecting C++ widget to Python

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
chennes
Veteran
Posts: 3910
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Connecting C++ widget to Python

Post by chennes »

@wmayer wrote a new Python wrapper for the SheetView that's accessible from the ViewProvider, so though I never figured out why the problem was occurring, we're bypassing it now.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
onekk
Veteran
Posts: 6206
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Connecting C++ widget to Python

Post by onekk »

Not directly related to this thing, but only with the error issued by FreeCAD.

The "deleted object error" is happening also when you try to iterate through a document to delete all objects.

Sometimes this code will fail:

Code: Select all

def clear_doc(doc_name):
    """Clear the document deleting all the objects.

    Parameters:
    name       type        description
    doc_name   string      document name
    """
    doc = FreeCAD.getDocument(doc_name)
    doc.recompute()
    objects = doc.Objects
    for obj in objects:
        if obj.TypeId in ("App::"):
            pass
        else:
            doc.removeObject(obj.Name)
I have to put a try: except: around doc.removeObject to avoid such error, maybe it is related to the way object is constructed when deleting an object it will delete his childs but doc.Objects are retaining childs and when it try to delete them they are already gone.

For sure it is my fault in coding.


Sorry for the OT.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Connecting C++ widget to Python

Post by wmayer »

The "deleted object error" is happening also when you try to iterate through a document to delete all objects.
This is not related to the reference counting issue of PySide but the order in which you remove objects from the document. If e.g. have a body and remove it then everything inside will be removed, too.

Code: Select all

doc = App.ActiveDocument
doc.addObject("PartDesign::Body")
doc.Objects
# [<body object>, <App::Origin object>, <GeoFeature object>, <GeoFeature object>, <GeoFeature object>, <GeoFeature object>, <GeoFeature object>, <GeoFeature object>]
As you can see the very first element is the body and when you remove it then the document will be empty.

Code: Select all

doc.removeObject(doc.Objects[0].Name)
doc.Objects
# []
I have to put a try: except: around doc.removeObject to avoid such error
Whenever you try to access any attribute if the underlying C++ object is destroyed it raises a ReferenceError, i. e. that obj.TypeId will fail, too. So, you must use the try/except block around the if/else block.

Code: Select all

    for obj in objects:
        try:
            if obj.TypeId in ("App::"):
                pass
            else:
                doc.removeObject(obj.Name)
        except ReferenceError as e:
            pass
Post Reply