How to store methods into an object

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

How to store methods into an object

Post by microelly2 »

I want to bind methods to my own objects which are callable after reload the file.
Is it possible to add methods from a trusted module to a python featured object

Something like this:

Code: Select all


c3.addProperty("App::PropertyMethod","key","MyProps","")
c3.key=myModule.myMethod

....

c3.key(args)
Or do I have to bind a Property String with the method name to the key and run an exec on this?
atzensepp
Posts: 100
Joined: Tue Feb 24, 2015 9:37 pm

Re: How to store methods into an object

Post by atzensepp »

This sounds interesting. If you are creating your own scripted objects it should be possible to add your own custom methods to them.
As far as I understood "Scripted Objects" http://freecadweb.org/wiki/index.php?ti ... ed_objects the code is serialized and saved together with the object. So if one later reads the object the code will get materialized again and can be invoked.
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: How to store methods into an object

Post by wmayer »

As far as I understood "Scripted Objects" http://freecadweb.org/wiki/index.php?ti ... ed_objects the code is serialized and saved together with the object. So if one later reads the object the code will get materialized again and can be invoked.
No, the code is NOT serialized because of two reasons:
* it's a security issue when loading such a project file because an attacker may modify the .fcstd file and then arbitrary code can be executed
* it will become almost impossible to use an updated script along with an older project file
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: How to store methods into an object

Post by yorik »

I wonder if it's possible to describe a python feature in a macro? I suppose yes... I might be a good way to distribute your object code...
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: How to store methods into an object

Post by microelly2 »

For me there are 3 levels:

1. Core FreeCAD functionallity
2. third party extension (workbenches, macros, modules)
3. end user data hold in freecad-objects and files

not every thing can be programmed in level 1 and 2. sometimes we want to create parametrized objects and we want to maintain them without extra programming and without trust to unsecure sources.

So I have looked for ways to achive this. My prototype was a picture frame http://freecadbuch.de/doku.php?id=blog: ... lderrahmen.

I think there should be a secure meta language and a collection of business objects which are powerful enough. FreeCAD is open for both.

After some time to collect experience about what configuration files should provide
I will write a parser for these files/texts
so that enduser can describe their configurations without extra python knowledge.
The configuration data can be stored by the enduser in freecad files and the parser can belong to secure modules.

The parser should close the insecure door that i have opened with my first simple eval/exec interpreter.
atzensepp
Posts: 100
Joined: Tue Feb 24, 2015 9:37 pm

Re: How to store methods into an object

Post by atzensepp »

wmayer wrote: No, the code is NOT serialized ...
I am wondering how this works. Is there an hands-on instruction how to work with those scripted objects? From the wiki I cannot grasp it.
W/r your "intelligent picture frame": I also would like to see such capabilities in FreeCAD:
User defined variables for dimensions etc. , variable scoping (local objects, related or attached / neighboured / container objects) and a means for geometric calculations. (I think it would be good to use those variables and formuals in the most of the dialogs).
B.t.w.: Do you provide a download of your code?
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: How to store methods into an object

Post by shoogen »

atzensepp wrote:I am wondering how this works. Is there an hands-on instruction how to work with those scripted objects? From the wiki I cannot grasp it.
Properties are stored as for c++ objects in XML. Attributes of the proxy are serialized using JSON. git commit 7032f5f329f1
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: How to store methods into an object

Post by wmayer »

... and the module name which contains the Python code is also stored inside the project. Later when opening a project with custom Python objects FreeCAD tries to import the above Python module.
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: How to store methods into an object

Post by microelly2 »

atzensepp wrote: B.t.w.: Do you provide a download of your code?
I have started a new project here: http://freecadbuch.de/doku.php?id=Sipoc
there is a git where the required modules are available
last days I have played with cascaded coordinate systems and context menues

http://freecadbuch.de/doku.php?id=blog: ... en_roboter
http://freecadbuch.de/doku.php?id=blog:freecad_and_qt

module is transform.py in the same git.
three things to do:
* use the power of qt to get handy property dialogs
* a parser for the configuration file
* clean up the code (improve error handling and documentation)
atzensepp
Posts: 100
Joined: Tue Feb 24, 2015 9:37 pm

Re: How to store methods into an object

Post by atzensepp »

Hi microelly2,

thank you for the link! But I am standing on the hose w/r how to use it.
I copied the files into my Macro-Folder and ran sipoc.py. No error, classes defined.
And now?

Code: Select all

dir__='/usr/lib/freecad/Mod/mylib'
is not present.
Post Reply