[solved] hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
dprojects
Posts: 722
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

[solved] hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by dprojects »

Hi, anyone can confirm this is FreeCAD bug?

For LinkGroup this code below causes FreeCAD crash at ViewObject:

Code: Select all

import FreeCAD

lg = FreeCAD.ActiveDocument.getObjectsByLabel("LinkGroup")[0]
vo = lg.ViewObject
d = dir(vo)

for o in d:
	FreeCAD.Console.PrintMessage("\n")
	FreeCAD.Console.PrintMessage(str(o))
	hasattr(vo, o)

For stable version:

Code: Select all

OS: Ubuntu 22.04 LTS (XFCE/xubuntu)
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.20)
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.9.13, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: English/United States (en_US)
Installed mods: 
  * Woodworking 0.20.29177
This is also at latest FreeCAD version:

Code: Select all

OS: Ubuntu 22.04 LTS (XFCE/xubuntu)
Word size of FreeCAD: 64-bit
Version: 0.21.29485 (Git) AppImage
Build type: Release
Branch: master
Hash: a236ca843fdd6674afb6d7ed1454fbd3b547f5ea
Python 3.10.5, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: English/United States (en_US)
Installed mods: 
  * Woodworking 0.20.29177
Attachments
FreeCAD-bug.FCStd
(4.28 KiB) Downloaded 10 times
test.FCMacro
(211 Bytes) Downloaded 5 times
Last edited by dprojects on Wed Jul 20, 2022 2:20 pm, edited 1 time in total.

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
Roy_043
Veteran
Posts: 8552
Joined: Thu Dec 27, 2018 12:28 pm

Re: hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by Roy_043 »

Confirmed:

Code: Select all

>>> d = dir(vo)
>>> for o in d:
...  hasattr(vo, o)
... 
True
True
True
True
True
True
True

Traceback (most recent call last):
  File "<input>", line 2, in <module>
Base.FreeCADError: Unknown exception while reading attribute 'DraggingPlacement' of object 'ViewProviderLink'

Code: Select all

OS: Windows 8.1 (6.3)
Word size of FreeCAD: 64-bit
Version: 0.21.29485 (Git)
Build type: Release
Branch: master
Hash: a236ca843fdd6674afb6d7ed1454fbd3b547f5ea
Python 3.8.13, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: Dutch/Netherlands (nl_NL)
Installed mods: 
User avatar
dprojects
Posts: 722
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by dprojects »

Yes, this crash FreeCAD also from python console manually, not only from macro. Also the getattr make crash too:

Code: Select all

Document
<GUI Document object at 0x562879352250>
any workaround?

EDIT:

Removing "DraggingPlacement" works, but I am not sure if this is correct solution. What is there at DraggingPlacement?

Code: Select all

import FreeCAD

lg = FreeCAD.ActiveDocument.getObjectsByLabel("LinkGroup")[0]
vo = lg.ViewObject
d = dir(vo)

for o in d:
	if str(o).find("DraggingPlacement") == -1:
		FreeCAD.Console.PrintMessage("\n")
		FreeCAD.Console.PrintMessage(str(o))
		hasattr(vo, o)

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by wmayer »

There is a failing assert() in FreeCAD/src/Gui/ViewProviderLink.cpp inside the function Base::Placement Gui::ViewProviderLink::currentDraggingPlacement():

Code: Select all

Base::Placement ViewProviderLink::currentDraggingPlacement() const{
    assert(pcDragger); // <<<=== pcDragger is null
    SbVec3f v;
...
}
This can be easily fixed by removing the assert() and doing some null pointer checks instead.
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by wmayer »

git commit 3f8dc1a5a8

@uwestoehr
Please backport that one.
User avatar
dprojects
Posts: 722
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by dprojects »

@wmayer Thanks Werner for this fix, the C ++ scares me a bit ;-)

Is there any way to get the LinkGroup.ViewObject.DraggingPlacement ?
Even if you run from python console

Code: Select all

lg = FreeCAD.ActiveDocument.getObjectsByLabel("LinkGroup")[0]
vo = lg.ViewObject
vo.ViewObject.DraggingPlacement 
the FreeCAD crash. So how to deal with such things in the future? the try and except will not work too in this case. Any workaround for future?

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by uwestoehr »

wmayer wrote: Tue Jul 19, 2022 8:41 am Please backport that one.
Done.
(FYI: I also started to look once a week through the commits what can/should be backported to not miss things.)
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by wmayer »

dprojects wrote: Tue Jul 19, 2022 11:48 am Is there any way to get the LinkGroup.ViewObject.DraggingPlacement ?
Even if you run from python console
You have to start the "Transform" edit mode as:

Code: Select all

vo.Document.setEdit(vo, 1) # the number 1 means Transform
vo.DraggingPlacement 
User avatar
dprojects
Posts: 722
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by dprojects »

wmayer wrote: Wed Jul 20, 2022 1:04 pm You have to start the "Transform" edit mode as:

Code: Select all

vo.Document.setEdit(vo, 1) # the number 1 means Transform
vo.DraggingPlacement 
This works but I was rather thinking about global solution to avoid FreeCAD crashes from python.

I use the hasattr at each ViewObject property in scanObjects inspection tool. I can see the DraggingPlacement is there because of dir(ViewObject) but if I want to go deeper to see what is inside ViewObject I have to call hasattr at each property and then the FreeCAD crash. Because I am trying to call hasattr at DraggingPlacement.

Maybe there is possibility (I don't know FreeCAD and C++ so well) to add some kind of sandbox function? similar to translate maybe? Something like FreeCAD.sandbox("any python command to run it safely without FreeCAD crash"). If someone would like to run something safely, there would be possibility to run it thru sandbox function?

Thanks for your help and sorry for long post, I know you probably don't have time to read long stories.

Image

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: [solved] hasattr at LinkGroup.ViewObject causes FreeCAD crash ?

Post by wmayer »

This works but I was rather thinking about global solution to avoid FreeCAD crashes from python.
That's not possible. If there is a C++ function that doesn't do correct error-checking and thus causes a segmentation fault or a failing assert() then the application crashes. And it doesn't make a difference whether this function is called from another C++ function or from Python.
Post Reply