Why would I use FeaturePython
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Why would I use FeaturePython
I'm experimenting with making a workbench at the moment and I've worked out how to create objects in python using parameters from a custom task panel which I've designed in QT Creator and subsequently shown in FreeCAD.
I have read about FeaturePython and ViewProvider but I cannot seem to determine why or when I would choose to use these... Some of the documentation relating to python seems to be somewhat out of date, which is not meant in any way as a complaint by the way.
I would like to be able to create a default item and then change it in response to changes in the task panel parameters - my simple thoughts are to attach signals and slots to detect changes in the task panel parameters and then delete any items I have created and recreate them from scratch each time a parameter is changed - but this seems a bit clumsy - is there a better way? Is this where I should be using FeaturePython?
I see mention of setEdit function in respect to ViewProvider but it's not clear what this is doing.
Can anyone point me towards some explanations?
I have read about FeaturePython and ViewProvider but I cannot seem to determine why or when I would choose to use these... Some of the documentation relating to python seems to be somewhat out of date, which is not meant in any way as a complaint by the way.
I would like to be able to create a default item and then change it in response to changes in the task panel parameters - my simple thoughts are to attach signals and slots to detect changes in the task panel parameters and then delete any items I have created and recreate them from scratch each time a parameter is changed - but this seems a bit clumsy - is there a better way? Is this where I should be using FeaturePython?
I see mention of setEdit function in respect to ViewProvider but it's not clear what this is doing.
Can anyone point me towards some explanations?
Re: Why would I use FeaturePython
Yes, this is what FeaturePython is for. You have to use it because it will properly handle lot of things that you won't have using signal/slots.
There is already lot of documentation available in wiki and forum if you search for it. Eventually look at the code because Python workbenches often use it too.
There is already lot of documentation available in wiki and forum if you search for it. Eventually look at the code because Python workbenches often use it too.
Re: Why would I use FeaturePython
Thanks, I've read the info in the wiki but I must be missing something because I can't see the benefit of using it. Are you able to point to one of the things it will properly handle which I wouldn't otherwise achieveopenBrain wrote: ↑Sun Jan 23, 2022 7:20 am Yes, this is what FeaturePython is for. You have to use it because it will properly handle lot of things that you won't have using signal/slots.
There is already lot of documentation available in wiki and forum if you search for it. Eventually look at the code because Python workbenches often use it too.
When you say "eventually look at the code" , do you mean there's an example python workbench template I could learn from? That would be great Alternatively is there a simple python coded workbench you'd recommend I study?
Last edited by EskiBrew on Sun Jan 23, 2022 8:36 am, edited 1 time in total.
Re: Why would I use FeaturePython
Not so easy to tell differences without knowing what you currently use. Right now I'm thinking about DocumentObserver messages.
As a workbench to study, maybe SheetMetal which has not so much functions and interesting behaviors.
As a workbench to study, maybe SheetMetal which has not so much functions and interesting behaviors.
Re: Why would I use FeaturePython
Ok, great, thanks - I already have Fasteners and Sheet Metal installed so I will study them.
Re: Why would I use FeaturePython
One of the main arguments about a FeaturePython object, is simple:
It is the FreeCAD way of doing things, there is a framework to make objects interact with each other, and "standard" signals.
example if a Document has to be recomputed, an object created has to be recalculated so FreeCAD issue a command that will force every object to be recomputed.
Other thing is the separation between FreeCAD and FreeCADGui, or using another paradigm OBj and ViewObject, they are managed by two different part of FreeCAD and there are some interaction between them, so the ViewProvider part is dealing with the ViewObject (Coin3D, Pivy) part.
I don't know what you are coding, but if you plan that your objects have to be used by FreeCAD as building blocks for other objects, it is not a "great think" to not use the "FreeCAD way" to expose objects.
It could be done in other ways? Yes and maybe better, but this is how things are done in FreeCAD.
Bat as the WB is yours take whatever decision you want.
This is my personal opinion
It is the FreeCAD way of doing things, there is a framework to make objects interact with each other, and "standard" signals.
example if a Document has to be recomputed, an object created has to be recalculated so FreeCAD issue a command that will force every object to be recomputed.
Other thing is the separation between FreeCAD and FreeCADGui, or using another paradigm OBj and ViewObject, they are managed by two different part of FreeCAD and there are some interaction between them, so the ViewProvider part is dealing with the ViewObject (Coin3D, Pivy) part.
I don't know what you are coding, but if you plan that your objects have to be used by FreeCAD as building blocks for other objects, it is not a "great think" to not use the "FreeCAD way" to expose objects.
It could be done in other ways? Yes and maybe better, but this is how things are done in FreeCAD.
Bat as the WB is yours take whatever decision you want.
This is my personal opinion
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: Why would I use FeaturePython
I'll second @onekk in the fact that using a FeaturePython object allows user to manipulate your objects with scripts, including in a CLI/headless mode.
Re: Why would I use FeaturePython
Thanks - I'm not suggesting the FreeCAD way is wrong, I'm just trying to understand how things are expected to work and your comments are very helpful for thatonekk wrote: ↑Sun Jan 23, 2022 10:26 am Other thing is the separation between FreeCAD and FreeCADGui, or using another paradigm OBj and ViewObject, they are managed by two different part of FreeCAD and there are some interaction between them, so the ViewProvider part is dealing with the ViewObject (Coin3D, Pivy) part.
Re: Why would I use FeaturePython
Sadly documentation is sparse.
One thing to learn is the concept of "Proxy" that is related to the way FreeCAD expose informations, sometimes, you have to call explicitly the Proxy or assign it in complex object creation, like in objects that are not created to be used with a script, like some "Path" objects.
Usually, it is a good habit to have some patience and ask here, waiting for some developers getting in and explaining things.
I have no deep knowledge of WB creation and in general about FeaturePython objects, as i usually code almost any object directly and show them only as a final operation to see them in the "3d view".
One of the main obstacles I had when learning something about FreeCAD was the separation between OCCT and Coin3D (to make things short).
When interacting with other users I have seen that apparently there are many misunderstanding about FreeCAD, some interesting informations could be "mined" among others in these pages:
https://wiki.freecadweb.org/Scripted_objects (Probably you already know this)
https://wiki.freecadweb.org/OpenCASCADE
https://wiki.freecadweb.org/Scenegraph
https://wiki.freecadweb.org/Pivy
Maybe also reading some pages in this this place:
https://wiki.freecadweb.org/Power_users_hub
would be an interesting read.
Maybe these advices are useless, but just in case.
Hoping that some of these infos could help you.
Regards
Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/