Connecting C++ widget to Python
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Re: Connecting C++ widget to Python
@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.
Re: Connecting C++ widget to Python
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:
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.
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)
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: Connecting C++ widget to Python
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.The "deleted object error" is happening also when you try to iterate through a document to delete all objects.
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>]
Code: Select all
doc.removeObject(doc.Objects[0].Name)
doc.Objects
# []
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.I have to put a try: except: around doc.removeObject to avoid such error
Code: Select all
for obj in objects:
try:
if obj.TypeId in ("App::"):
pass
else:
doc.removeObject(obj.Name)
except ReferenceError as e:
pass