How to check if an object has been deleted?
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
How to check if an object has been deleted?
What is the standard way to check if an object has been deleted?
While running the Draft unit tests there is an issue with make_clone. The DiffuseColor is applied with a delay, which works fine during normal operations, but during the unit tests the document gets deleted beforehand.
While running the Draft unit tests there is an issue with make_clone. The DiffuseColor is applied with a delay, which works fine during normal operations, but during the unit tests the document gets deleted beforehand.
Re: How to check if an object has been deleted?
Probably the best in Python is to use the potentially deleted object in a 'try' block.
This said, is the problem the test, or the delay?
This said, is the problem the test, or the delay?
Re: How to check if an object has been deleted?
The delay was introduced as a workaround to fix another problem.
-
- Veteran
- Posts: 2764
- Joined: Mon Feb 27, 2012 5:31 pm
Re: How to check if an object has been deleted?
Maybe FreeCAD.ActiveDocument.getObject('Label') and test for None
Re: How to check if an object has been deleted?
Can you link the test here? I test code like that all the time in the Addon Manager and use a self-managed event loop to wait through the delay, maybe that's a technique you can use here.
Re: How to check if an object has been deleted?
The test in test_modification.py:
https://github.com/FreeCAD/FreeCAD/blob ... on.py#L556
The delay in make_clone.py:
https://github.com/FreeCAD/FreeCAD/blob ... ne.py#L129
The test does not actually fail, there is an error message in the Report view however.
Note that on Windows I am having an issue with running all Draft unit tests:
https://forum.freecadweb.org/viewtopic.php?f=3&t=73269
On Ubuntu this problem does not occur:
The current Windows weekly does not include the Test WB BTW.
https://github.com/FreeCAD/FreeCAD/blob ... on.py#L556
The delay in make_clone.py:
https://github.com/FreeCAD/FreeCAD/blob ... ne.py#L129
The test does not actually fail, there is an error message in the Report view however.
Note that on Windows I am having an issue with running all Draft unit tests:
https://forum.freecadweb.org/viewtopic.php?f=3&t=73269
On Ubuntu this problem does not occur:
Code: Select all
import Test, TestDraft
Test.runTestsFromModule(TestDraft)
The current Windows weekly does not include the Test WB BTW.
Re: How to check if an object has been deleted?
Is there a variable that you can check to see when the operation is completed? In the Addon Manager's tests, I do things that in this application would look like:
I also run a QTimer that is a bit longer than the delay and use it to kill the loop if the stop condition never becomes true (e.g. the test fails).
Code: Select all
obj = Draft.make_clone(box)
while not stop_condition_is_true:
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 10)
do_stuff_with_result()
Re: How to check if an object has been deleted?
Thanks you! That can indeed work. My first try (a bit cryptic, but so is the DiffuseColor workaround):
I'll simplify it to this:
Code: Select all
if App.GuiUp:
diffuse_col = [(0.0, 0.0, 1.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(1.0, 0.0, 0.0, 0.0)]
box.ViewObject.DiffuseColor = diffuse_col
obj = Draft.make_clone(box)
if App.GuiUp:
obj.ViewObject.DiffuseColor = [(1.0, 1.0, 1.0, 0.0)]
from PySide import QtCore
i = 0
while i < 10 and obj.ViewObject.DiffuseColor != diffuse_col:
i += 1
_msg(str(i))
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 10)
Code: Select all
obj = Draft.make_clone(box)
if App.GuiUp:
from PySide import QtCore
i = 0
while i < 10:
i += 1
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 10)