[Solved] Saving Python Features to the document
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[Solved] Saving Python Features to the document
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
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.
Re: Saving Python Features to the document
Good morning
and this is working, copy and paste, and it can be saved and opened.
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()
Re: Saving Python Features to the document
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'
Re: Saving Python Features to the document
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.
Re: Saving Python Features to the document
Yes
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__.
Make a simpler one where to isolate the matter of conflict ('Divide and conquer').
Re: Saving Python Features to the document
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.
Re: Saving Python Features to the document
With 'custom Properties' I mean the Properties I added with
Code: Select all
obj.addProperty(...)
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.
Re: Saving Python Features to the document
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?damian wrote: ↑Mon Nov 05, 2018 1:43 pmThere 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.
Thanks again for taking your time helping me out. I'm pretty familiar with python, but relatively new to FreeCAD scripting.