self.Object does return an error on file writing

About the development of the FEM module/workbench.

Moderator: bernd

User avatar
bernd
Posts: 10702
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

self.Object does return an error on file writing

Postby bernd » Mon Mar 09, 2020 6:09 pm

git commit f9c081f06d add self.Object to a View Provider class which is inherited from a base class. As soon as self.Object is added to a inherited class on document saving the obj raises an error ...

this commit is interessting too ... git commit 8b84a1cc94

- start latest FreeCAD
- open FEM 3D cantilever example
- save as file on disk (desktop or else where)
- error ...

Code: Select all

$ ./bin/FreeCAD
FreeCAD 0.19, Libs: 0.19R19965 (Git)
© Juergen Riegel, Werner Mayer, Yorik van Havre and others 2001-2020
FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.
FreeCAD wouldn't be possible without FreeCAD community.
  #####                 ####  ###   ####  
  #                    #      # #   #   # 
  #     ##  #### ####  #     #   #  #   # 
  ####  # # #  # #  #  #     #####  #   # 
  #     #   #### ####  #    #     # #   # 
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##

connect failed: Datei oder Verzeichnis nicht gefunden
Traceback (most recent call last):
  File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
<class 'TypeError'>: Object of type FeaturePython is not JSON serializable
vocx
Posts: 5031
Joined: Thu Oct 18, 2018 9:18 pm

Re: self.Object does return an erro on file writing

Postby vocx » Tue Mar 10, 2020 6:55 am

Yes, I don't know what you did, but the unit tests show those messages.

The tests don't seem to fail, so I have ignored the errors.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
wmayer
Site Admin
Posts: 16301
Joined: Thu Feb 19, 2009 10:32 am

Re: self.Object does return an erro on file writing

Postby wmayer » Tue Mar 10, 2020 3:11 pm

The problem is caused by the proxy object of FemGui::ViewProviderResultPython.

Code: Select all

Gui.ActiveDocument.CalculiX_static_results.Proxy.__dict__
will give something like this
{'Object': <Fem::FemResultObjectPython object>, 'ViewObject': <View provider object at 0x3148560>}
which I think must be caused by the attach() method of git commit f9c081f06d49ee

When serializing a proxy object then first it's checked whether it implements the __getstate__ method and if yes its output will be used. If it doesn't have then the __dict__ attribute is used and if this doesn't exist then the object as is will be saved. See PropertyPythonObject::toString()

Now due to the implementation of attach() the __dict__ attribute contains an object and its view provider that are objects that cannot be serialized. To solve the issue you should implement the methods __getstate__ and __setstate__. As a default implementation it's sufficient to do:

Code: Select all

def __getstate__(self):
   return None
   
def __setstate__(self, data):
   return None
wmayer
Site Admin
Posts: 16301
Joined: Thu Feb 19, 2009 10:32 am

Re: self.Object does return an erro on file writing

Postby wmayer » Tue Mar 10, 2020 3:33 pm

User avatar
bernd
Posts: 10702
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: self.Object does return an erro on file writing

Postby bernd » Tue Mar 10, 2020 5:11 pm

Got it! Many thanks for the explanation. git commit 062d782236 was the reason for all the excitement on my side. I did not know what these methods are used for ...
User avatar
bernd
Posts: 10702
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: self.Object does return an erro on file writing

Postby bernd » Tue Mar 10, 2020 5:19 pm