How to check if an object has been deleted?

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

How to check if an object has been deleted?

Post by Roy_043 »

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.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to check if an object has been deleted?

Post by openBrain »

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? :)
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to check if an object has been deleted?

Post by Roy_043 »

openBrain wrote: Thu Jan 26, 2023 7:28 pm is the problem the test, or the delay?
It is the combination. The tests run too fast for the delay. The delayed action gets executed during the next test, when the document used for the previous test no longer exists.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to check if an object has been deleted?

Post by openBrain »

Roy_043 wrote: Thu Jan 26, 2023 7:35 pm It is the combination. The tests run too fast for the delay. The delayed action gets executed during the next test, when the document used for the previous test no longer exists.
What I meant is that maybe using a delay isn't appropriate or optimal. ;)
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to check if an object has been deleted?

Post by Roy_043 »

The delay was introduced as a workaround to fix another problem.
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: How to check if an object has been deleted?

Post by keithsloan52 »

Maybe FreeCAD.ActiveDocument.getObject('Label') and test for None
User avatar
chennes
Veteran
Posts: 3877
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: How to check if an object has been deleted?

Post by chennes »

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.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to check if an object has been deleted?

Post by Roy_043 »

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:

Code: Select all

import Test, TestDraft
Test.runTestsFromModule(TestDraft)

The current Windows weekly does not include the Test WB BTW.
User avatar
chennes
Veteran
Posts: 3877
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: How to check if an object has been deleted?

Post by chennes »

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:

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()
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).
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to check if an object has been deleted?

Post by Roy_043 »

Thanks you! That can indeed work. My first try (a bit cryptic, but so is the DiffuseColor workaround):

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)
I'll simplify it to this:

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)
Post Reply