[Solved] Saving Python Features to the document

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
bliepp
Posts: 13
Joined: Thu Sep 27, 2018 11:57 am

[Solved] Saving Python Features to the document

Post by bliepp »

Hey Guys,

I'm having a problem using scripted objects in FreeCAD. In my module I'm creating some python features based on folders, which works fine (icons, properties, etc work exactly how I want them to be), but I cannot save them to the document project. So I googled around and found out that scripted objects are stored via pythons json module. Since I've never used it I have absolutly no clue on how to setup the __getstate__ and __setstate__ methods. Firstly I tried the example's "return None", which obviously failed, because I have data to save. But how is it done? As far as I know the set/getstate methods are part of the pickle module. How are they used in the json module of FreeCAD?

I also tested if I could save the feature created in the example, but it also failed to load again.

Wiki page: https://www.freecadweb.org/wiki/Scripted_objects

bliepp
Last edited by bliepp on Mon Nov 12, 2018 12:12 pm, edited 1 time in total.
damian
Posts: 583
Joined: Sun May 31, 2015 6:16 pm

Re: Saving Python Features to the document

Post by damian »

Good morning
bliepp wrote: Mon Nov 05, 2018 9:50 am I also tested if I could save the feature created in the example, but it also failed to load again.
The easiest example in the wiki page is "Working with simple shapes"

Code: Select all

import FreeCAD as App
import FreeCADGui
import FreeCAD
import Part
class Line:
    def __init__(self, obj):
        '''"App two point properties" '''
        obj.addProperty("App::PropertyVector","p1","Line","Start point")
        obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(1,0,0)
        obj.Proxy = self

    def execute(self, fp):
        '''"Print a short message when doing a recomputation, this method is mandatory" '''
        fp.Shape = Part.makeLine(fp.p1,fp.p2)

a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Line")
Line(a)
a.ViewObject.Proxy=0 # just set it to something different from None (this assignment is needed to run an internal notification)
FreeCAD.ActiveDocument.recompute() 
and this is working, copy and paste, and it can be saved and opened.
damian
Posts: 583
Joined: Sun May 31, 2015 6:16 pm

Re: Saving Python Features to the document

Post by damian »

bliepp wrote: Mon Nov 05, 2018 9:50 am the __getstate__ and __setstate__ methods
You'll need these methods if you want to store extra information not included in a freecad's property
Publish a resume of your class and requirements and easily someone could explain to you.
bliepp
Posts: 13
Joined: Thu Sep 27, 2018 11:57 am

Re: Saving Python Features to the document

Post by bliepp »

damian wrote: Mon Nov 05, 2018 12:18 pm Good morning
...
The easiest example in the wiki page is "Working with simple shapes"
Thanks for your reply. But when I tried the first example (the box class, under "Basic example") it doesn't work.
I just tested the example suggested by you and it doesn't work either. Theres the following error when loading a saved file with a scripted object inside:

Code: Select all

<unknown exception traceback><type 'exceptions.AttributeError'>: 'module' object has no attribute 'Line'
<unknown exception traceback><type 'exceptions.AttributeError'>: 'module' object has no attribute 'ViewProviderLine'
The same error is raised by my class or any other example on the wiki page.
bliepp
Posts: 13
Joined: Thu Sep 27, 2018 11:57 am

Re: Saving Python Features to the document

Post by bliepp »

damian wrote: Mon Nov 05, 2018 12:24 pm
bliepp wrote: Mon Nov 05, 2018 9:50 am the __getstate__ and __setstate__ methods
You'll need these methods if you want to store extra information not included in a freecad's property
Publish a resume of your class and requirements and easily someone could explain to you.
So if i saved all my properties in FreeCAD properties they are saved automatically? Does this also apply to custom Properties?
To publish a resume of my class(es) would be really difficult as I have about 10+ files in a module I'm creating. It's basically the same as the examples except for the objects given to the scripted object. While the examples use Part Features such as Cubes I am using folders. Shouldn't make any difference, though.

As the error and behavior is the same on the examples of the given wiki page and a resume would be too complicated (even for a resume), use those examples as my resume, especially the first example.

Also, I want to thank you for your fast response and for taking you time.
damian
Posts: 583
Joined: Sun May 31, 2015 6:16 pm

Re: Saving Python Features to the document

Post by damian »

bliepp wrote: Mon Nov 05, 2018 1:14 pm So if i saved all my properties in FreeCAD properties they are saved automatically?
Yes
bliepp wrote: Mon Nov 05, 2018 1:14 pm Does this also apply to custom Properties?
What do you refer about 'custom Properties'?
If the custom properties are freecad's properties they must be saved automatically.
If they are python's properties or attributes you'll need the methods __getstate__ and setstate__.
bliepp wrote: Mon Nov 05, 2018 1:14 pm To publish a resume of my class(es) would be really difficult as I have about 10+ files in a module I'm creating.
Make a simpler one where to isolate the matter of conflict ('Divide and conquer').
damian
Posts: 583
Joined: Sun May 31, 2015 6:16 pm

Re: Saving Python Features to the document

Post by damian »

damian wrote: Mon Nov 05, 2018 1:29 pm If they are python's properties or attributes you'll need the methods __getstate__ and setstate__.
There is an alternative way using the freecad's property App::PropertyPythonObject.
In all cases, the stored information must be in a format supported by json, https://docs.python.org/2/library/json. ... d-decoders.
bliepp
Posts: 13
Joined: Thu Sep 27, 2018 11:57 am

Re: Saving Python Features to the document

Post by bliepp »

damian wrote: Mon Nov 05, 2018 1:29 pm What do you refer about 'custom Properties'?
If the custom properties are freecad's properties they must be saved automatically.
If they are python's properties or attributes you'll need the methods __getstate__ and setstate__.
With 'custom Properties' I mean the Properties I added with

Code: Select all

obj.addProperty(...)
and then are shown in the FreeCAD Property Panel.
damian wrote: Mon Nov 05, 2018 1:29 pm Make a simpler one where to isolate the matter of conflict ('Divide and conquer').
Yeah, I know how to make a resume, but in the end there's the same content as in the examples from the wiki. The only difference is, that I have much more methods. The basics are the same, because I literally copy and pasted them. The error is coming up on just those example codes as well. It's nothing my extra methods interfere with. The "isolated matter of conflict" are basically the same as in the examples.
bliepp
Posts: 13
Joined: Thu Sep 27, 2018 11:57 am

Re: Saving Python Features to the document

Post by bliepp »

damian wrote: Mon Nov 05, 2018 1:43 pm
damian wrote: Mon Nov 05, 2018 1:29 pm If they are python's properties or attributes you'll need the methods __getstate__ and setstate__.
There is an alternative way using the freecad's property App::PropertyPythonObject.
In all cases, the stored information must be in a format supported by json, https://docs.python.org/2/library/json. ... d-decoders.
What do you mean with "python's properties"? Do you mean regular python attributes of classes or those properties to show in the property panel as mentioned above?

Thanks again for taking your time helping me out. I'm pretty familiar with python, but relatively new to FreeCAD scripting.
damian
Posts: 583
Joined: Sun May 31, 2015 6:16 pm

Re: Saving Python Features to the document

Post by damian »

bliepp wrote: Mon Nov 05, 2018 1:07 pm Theres the following error when loading a saved file with a scripted object inside:
Now I'm going out ... Tonight I'll answer you.
EDIT: this error message is due to the code is not stored in the folder ~/.FreeCAD/Mod (in my system). See you
Post Reply