Create a FeaturePython object part I: Difference between revisions

From FreeCAD Documentation
m (update: Mac OSX …/Preferences/… -> macOS …/Application Support/…)
 
(159 intermediate revisions by 10 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
<translate>
<translate>
<!--T:28-->
{{docnav|PySide|Embedding FreeCAD}}


<!--T:1-->
== Introduction ==
{{Docnav
|
|[[Create_a_FeaturePython_object_part_II|Create a FeaturePython object part II]]
|IconL=
|IconR=
}}


</translate>
{{TOCright}}
<translate>


==Introduction== <!--T:2-->
FeaturePython objects (also often referred to as 'Scripted Objects') provide users the ability to extend FreeCAD's with objects that integrate seamlessly into the FreeCAD framework.


<!--T:3-->
FeaturePython objects (also referred to as [[Scripted_objects|Scripted objects]]) provide the ability to extend FreeCAD with objects that integrate seamlessly into the FreeCAD framework.


<!--T:4-->
This encourages:
This encourages:
*Rapid prototyping of new objects and tools with custom Python classes.
*Saving and restoring data (also known as serialization) through {{incode|App::Property}} objects, without embedding any script in the FreeCAD document file.
*Creative freedom to adapt FreeCAD for any task.


<!--T:5-->
*'''Rapid prototyping''' of new objects and tools with custom Python classes.
On this page we are going to construct a working example of a FeaturePython custom class, identifying all the major components and gaining an understanding of how everything works as we go along.


==How does it work?== <!--T:6-->
*'''Serialization''' through 'App::Property' objects, ''without embedding any script'' in the FreeCAD document file.


<!--T:7-->
*'''Creative freedom''' to adapt FreeCAD for any task!
FreeCAD comes with a number of default object types for managing different kinds of geometry. Some of them have "FeaturePython" alternatives that allow for customization with a user defined Python class.


<!--T:8-->
This custom Python class takes a reference to one of these objects and modifies it. For example, the Python class may add properties to the object or link it to other objects. In addition the Python class may implement certain methods to enable the object to respond to document events, making it possible to trap object property changes and document recomputes.


<!--T:9-->
=== How Does It Work? ===
When working with custom classes and FeaturePython objects it is important to know that the custom class and its state are not saved in the document as this would require embedding a script in a FreeCAD document file, which would pose a significant security risk. Only the FeaturePython object itself is saved (serialized). But since the script module path is stored in the document, a user need only install the custom Python class code as an importable module, following the same folder structure, to regain the lost functionality.


<!--T:94-->
FreeCAD comes with a number of default object types for managing different kinds of geometry. Some of them have 'FeaturePython' alternatives that allow for user customization with a custom python class.
[[#top|top]]


==Setting things up== <!--T:11-->
The custom python class simply takes a reference to one of these objects and modifies it in any number of ways. For example, the python class may add properties directly to the object, modifying other properties when it's recomputed, or linking it to other objects. In addition the python class implements certain methods to enable it to respond to document events, making it possible to trap object property changes and document recomputes.


<!--T:12-->
It's important to remember, however, that for as much as one can accomplish with custom classes and FeaturePython objects, when it comes time to save the document, '''only the FeaturePython object itself is serialized'''. The custom class and it's state are not retained between document reloading. Doing so would require embedding script in the FreeCAD document file, which poses a significant security risk, much like the risks posed by [https://www.howtogeek.com/171993/macros-explained-why-microsoft-office-files-can-be-dangerous/ embedding VBA macros in Microsoft Office documents].
FeaturePython Object classes need to act as importable modules in FreeCAD. That means you need to place them in a path that exists in your Python environment (or add it specifically). For the purposes of this tutorial, we're going to use the FreeCAD user Macro folder. But if you have another idea in mind, feel free to use that instead.


<!--T:13-->
Thus, a FeaturePython object ultimately exists entirely apart from it's script. The inconvenience posed by not packing the script with the object in the document file is far less than the risk posed by running a file embedded with an unknown script. However, the script module path is stored in the document file. Therefore, a user need only install the custom python class code as an importable module following the same directory structure to regain the lost functionality.
If you don't know where the FreeCAD Macro folder is type {{incode|FreeCAD.getUserMacroDir(True)}} in FreeCAD's [[Python_console|Python console]]:
* On Linux it is usually {{FileName|/home/<username>/.local/share/FreeCAD/Macro/}} ({{VersionPlus|0.20}}) or {{FileName|/home/<username>/.FreeCAD/Macro/}} ({{VersionMinus|0.19}}).
* On Windows it is {{FileName|%APPDATA%\FreeCAD\Macro\}}, which is usually {{FileName|C:\Users\<username>\Appdata\Roaming\FreeCAD\Macro\}}.
* On macOS it is usually {{FileName|/Users/<username>/Library/Application Support/FreeCAD/Macro/}}.


<!--T:14-->
== The Complete Beginner's Guide to FeaturePython Objects ==
Now we need to create some folders and files:
*In the {{FileName|Macro}} folder create a new folder called {{FileName|fpo}}.
*In the {{FileName|fpo}} folder create an empty file: {{FileName|__init__.py}}.
*In the {{FileName|fpo}} folder, create a new folder called {{FileName|box}}.
*In the {{FileName|box}} folder create two files: {{FileName|__init__.py}} and {{FileName|box.py}} (leave both empty for now).


<!--T:16-->
Let's start from the very beginning (it's a very good place to start. ;) )
Your folder structure should look like this:


</translate>
Macro/
|--> fpo/
|--> __init__.py
|--> box/
|--> __init__.py
|--> box.py
<translate>


<!--T:15-->
We're going to construct a complete, working example of a FeaturePython custom class, identifying all of the major components and gaining an intimate understanding of how everything works as we go. But before we do, there's a few details to get out of the way.
The {{FileName|fpo}} folder provides a nice place to play with new FeaturePython objects and the {{FileName|box}} folder is the module we will be working in. {{FileName|__init__.py}} tells Python that there is an importable module in the folder, and {{FileName|box.py}} will be the class file for our new FeaturePython Object.


<!--T:18-->
=== Setting up your development environment ===
With our module paths and files created, let's make sure FreeCAD is set up properly:
*Start FreeCAD (if you haven't done so already).
*Enable the [[Report_view|Report view]] ({{MenuCommand|View → Panels → Report view}}).
*Enable the [[Python_console|Python console]] ({{MenuCommand|View → Panels → Python console}}) see [[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]].


<!--T:95-->
Finally, navigate to the {{FileName|Macro/fpo/box}} folder and open {{FileName|box.py}} in your favorite code editor. We will only edit that file.


<!--T:96-->
To begin, FeaturePython Object classes need to act as importable modules in FreeCAD. That means you need to place them in a path that exists in your Python environment (or add it specifically). For the purposes of this tutorial, we're going to use the FreeCAD user Macro folder, though if you have another idea in mind, feel free to use that instead!
[[#top|top]]


==A FeaturePython object== <!--T:22-->


<!--T:23-->
Anyway, if you don't know where the FreeCAD Macro folder is:
Let's get started by writing our class and its constructor:


</translate>
*Windows: Type '%APPDATA%/FreeCAD/Macro' in the filepath bar at the top of Explorer
{{Code|code=
class box():


def __init__(self, obj):
*Linux: Navigate to /home/USERNAME/.FreeCAD/Macro
"""
Default constructor
"""


self.Type = 'box'
*Mac: Navigate to /Users/USERNAME/Library/Preferences/FreeCAD/Macro


obj.Proxy = self
}}
<translate>


<!--T:24-->
Now we need to create some files.
'''The {{incode|__init__()}} method breakdown:'''


<!--T:97-->
*In the Macro folder create a new folder called '''fpo'''.
{|class="wikitable" cellpadding="5px" width="100%"
*In the fpo folder, create a new folder called '''box'''.
|style="width:25%" | {{incode|def __init__(self, obj):}}
*In the box folder create two files: '''__init__.py''' and '''box.py''' (leave both empty for now)
|style="width:75%" | Parameters refer to the Python class itself and the FeaturePython object that it is attached to.
|-
| {{incode|self.Type <nowiki>=</nowiki> 'box'}}
| String definition of the custom Python type.
|-
| {{incode|obj.Proxy <nowiki>=</nowiki> self}}
| Stores a reference to the Python instance in the FeaturePython object.
|}


<!--T:25-->
Add the following code at the top of the file:


</translate>
The '''fpo''' folder provides a nice spot to play with new FeaturePython objects and the '''box''' folder is the module we will be working in.
{{Code|code=
import FreeCAD as App


def create(obj_name):
'''__init__.py''' tells Python that in the folder is an importable module, and '''box.py''' will be the class file for our new FeaturePython Object
"""
Object creation method
"""


obj = App.ActiveDocument.addObject('App::FeaturePython', obj_name)


box(obj)
Your directory structure should look like this:


return obj
.FreeCAD
}}
|--> Macro
<translate>
|--> fpo
|--> box
|--> __init__.py
|--> box.py


<!--T:26-->
'''The {{incode|create()}} method breakdown:'''


<!--T:98-->
With our module paths and files created, let's make sure FreeCAD is set up properly. If FreeCAD isn't loaded, now is a good time to boot it up.
{|class="wikitable" cellpadding="5px" width="100%"
|style="width:25%" | {{incode|import FreeCAD as App}}
|style="width:75%" | Standard import for most Python scripts, the App alias is not required.
|-
| {{incode|obj <nowiki>=</nowiki> ... addObject(...)}}
| Creates a new FreeCAD FeaturePython object with the name passed to the method. If there is no name clash, this will be the label and the name of the created object. Otherwise, a unique name and label will be created based on 'obj_name'.
|-
| {{incode|box(obj)}}
| Creates our custom class instance.
|-
| {{incode|return obj}}
| Returns the FeaturePython object.
|}


<!--T:27-->
Make sure the Python Console and Report View are enabled by selecting '''View -> Panels -> Report view''' and '''Python console'''
The {{incode|create()}} method is not required, but it provides a nice way to encapsulate the object creation code.


<!--T:99-->
If you haven't been introduced to the Python Console in FreeCAD, you can [[FreeCAD Scripting Basics|learn more about it here]]
[[#top|top]]


===Testing the code=== <!--T:29-->


<!--T:30-->
Now that FreeCAD is set up, switch over to your favorite code editor, navigate to the Macro/fpo/box folder, and open '''box.py'''. It's time to write some code!
Now we can test our new object. Save your code and return to FreeCAD. Make sure you have opened a new document, you can do this by pressing {{KEY|Ctrl}}+{{KEY|N}} or selecting {{MenuCommand|File → New}}.


-----
<!--T:31-->
In the Python console type the following:
=== A Very Basic FeaturePython Object ===


</translate>
{{Code|code=
from fpo.box import box
}}
<translate>


<!--T:33-->
We're going to start really simply. It won't be impressive, but it will provide a clear idea of how FeaturePython objects work with custom python classes.
Now we need to create our object:


</translate>
{{Code|code=
mybox = box.create('my_box')
}}
<translate>


<!--T:35-->
So, let's get started by writing our class and it's constructor:
[[Image:Fpo_treeview.png | right]]
You should see a new object appear in the [[Tree_view|Tree view]] labelled "my_box".


<!--T:100-->
class box():
Note that the icon is gray. FreeCAD is telling us that the object is not able to display anything in the [[3D_view|3D view]]. Click on the object and look at its properties in the [[Property_editor|Property editor]]. There is not much there, just the name of the object.
def __init__(self, obj):
"""
Default Constructor
"""
self.Type = 'box'
obj.Proxy = self
self.Object = obj


<!--T:101-->
{|class="wikitable" cellpadding="5" style="float:right; margin-left: 15px; margin-right: 5px"
Also note that there is a small blue check mark next to the FeaturePython object in the Tree view. That is because when an object is created or changed it is "touched" and needs to be recomputed. Pressing the {{Button|[[Image:Std_Refresh.svg|16px]] [[Std_Refresh|Std Refresh]]}} button will accomplish this. We will add some code to automate this later.
|+ style="caption-side:bottom"|The <code>__init__()</code> method breakdown
{{Clear}}
|style="width:20%" | <code>def __init__(self, obj):</code> || Parameters refer to the Python class itself and the FeaturePython object that it is attached to.
|-
|<code> self.Type = 'box'</code> || String definition of the custom python type
|-
|<code> obj.Proxy = self</code> || Stores a reference to the Python class in the FeaturePython object
|-
|<code> self.Object = obj</code> || Stores a reference to the FeaturePython object in the Python class.
|}
<br>
This small piece of code illustrates just how FeaturePython objects interact with Python classes.<br><br>We'll examine that in more detail momentarily, but first we need to add a little more code to manage object creation.
<br clear="all"><br>
In the '''box.py''' file at the top, add the following code:


<!--T:37-->
import FreeCAD as App
Let's look at our object's attributes:
</translate>
def create(obj_name):
{{Code|code=
"""
dir(mybox)
Object creation method
}}
"""
<translate>
obj = App.ActiveDocument.addObject('App::FeaturePython', obj_name)
fpo = box(obj)
return fpo


<!--T:102-->
This will return:


</translate>
{|class="wikitable" cellpadding="5" style="float:right; margin-left: 15px; margin-right: 5px"
{{Code|code=
|+ style="caption-side:bottom"|The <code>create()</code> method breakdown
['Content', 'Document', 'ExpressionEngine', 'FullName', 'ID', 'InList',
|style="width:20%" | <code>import FreeCAD as App</code> || Standard import for most python scripts. The '''App''' alias is not required, but it is consistent with the FreeCAD Python console.
...
|-
'setPropertyStatus', 'supportedProperties', 'touch']
|<code>obj = ... addObject(...)</code> || Creates a new FreeCAD FeaturePython object with the name passed to the method.
}}
|-
<translate>
|<code>fpo = box(obj)</code> || Create our custom class instance and link it to the FeaturePython object.
|}
<br><br>The <code>create()</code> method is not required, but it provides a nice way to encapsulate the object creation code.
<br clear="all"><br>
-----


<!--T:38-->
=== Testing the Code ===
There are a lot of attributes because we're accessing the native FreeCAD FeaturePyton object created in the first line of our {{incode|create()}} method. The {{incode|Proxy}} property we added in our {{incode|__init__()}} method is there too.


<!--T:40-->
Now we can try our new object. Save your code and return to FreeCAD
Let's inspect it with the {{incode|dir()}} method:


</translate>
{{Code|code=
dir(mybox.Proxy)
}}
<translate>


<!--T:103-->
In the Python Console, type the following:
This will return:


</translate>
>>> from fpo.box import box
{{Code|code=
['Type', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
...
'__str__', '__subclasshook__', '__weakref__']
}}
<translate>


<!--T:42-->
We can see our {{incode|Type}} property. Let's check it:


</translate>
Now, we need to create our object:
{{Code|code=
mybox.Proxy.Type
}}
<translate>


<!--T:104-->
>>> box.create('my_box')
This will return:


</translate>
{{Code|code=
'box'
}}
<translate>


<!--T:46-->
You should see a new object appear in the tree view at the top left labelled '''my_box'''. Note that the icon is gray. FreeCAD is simply telling us that the object is not able to display anything in the 3D view... yet.
This is indeed the assigned value, so we know we're accessing the custom class through the FeaturePython object.


<!--T:49-->
That's ok. There's still something to learn here.
Now let's see if we can make our class a little more interesting, and maybe more useful as well.


<!--T:105-->
[[#top|top]]


===Adding properties=== <!--T:51-->
Click on the object and note what appears in the property panel under it. Not very much - just the name of the object. We'll need to add some properties in a bit.


<!--T:52-->
Now, back at the console, let's see just what that code we wrote in the <code>__init__()</code> function does for us.
Properties are the lifeblood of a FeaturePython class. Fortunately, FreeCAD supports [[FeaturePython_Custom_Properties|a number of property types]] for FeaturePython classes. These properties are attached directly to the FeaturePython object and are fully serialized when the file is saved. To avoid having to serialize data yourself, it is advisable to only use these property types.


<!--T:84-->
Adding properties is done using the {{incode|add_property()}} method. The syntax for the method is:


<!--T:106-->
First, let's make referencing our new object a little more convenient:
<!--Do not use Code template to avoid syntax highlighting-->
</translate>
add_property(type, name, section, description)
<translate>


<!--T:55-->
>>> mybox = App.ActiveDocument.my_box
You can view the list of supported properties by typing:


</translate>
{{Code|code=
mybox.supportedProperties()
}}
<translate>


<!--T:57-->
Let's try adding a property to our box class. Switch to your code editor, move to the {{incode|__init__()}} method, and at the end of the method add:


</translate>
Now, let's list the attributes of the object:
{{Code|code=
obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = ""
}}
<translate>


<!--T:58-->
>>> dir(mybox)
Note how we're using the reference to the (serializable) FeaturePython object {{incode|obj}}, and not the (non-serializable) Python class instance {{incode|self}}.
['Content', 'Document', 'ExpressionEngine', 'InList', 'InListRecursive', 'Label', 'MemSize', 'Module', 'Name', 'OutList', 'OutListRecursive', 'PropertiesList', 'Proxy', 'State', 'TypeId', 'ViewObject', '__class__',
...
'setEditorMode', 'setExpression', 'supportedProperties', 'touch']


<!--T:60-->
Once you're done, save the changes and switch back to FreeCAD. Before we can observe the changes made to our code, we need to reload the module. This can be accomplished by restarting FreeCAD, but restarting FreeCAD every time we edit the code would be inconvenient. To make things easier type the following in the Python console:

</translate>
{{Code|code=
from importlib import reload
reload(box)
}}
<translate>

<!--T:61-->
With the module reloaded, let's see what we get when we create an object:

</translate>
{{Code|code=
box.create('box_property_test')
}}
<translate>

<!--T:62-->
You should see the new box object appear in the Tree view:
*Select it and look at the Property editor. There, you should see the ''Description'' property.
*Hover over the property name on the left and the tooltip should appear with the description you provided.
*Select the field and type whatever you like. You'll notice that Python update commands are executed and displayed in the console as you type letters and the property changes.

<!--T:107-->
[[#top|top]]

<!--T:86-->
Let's add some more properties. Return to your source code and add the following properties to the {{incode|__init__()}} method:

</translate>
{{Code|code=
obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = '10 mm'
obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = '1 cm'
}}
<translate>

<!--T:108-->
And let's also add some code to recompute the document automatically. Add the following line above the {{incode|return()}} statement in the {{incode|create()}} method :

</translate>
{{Code|code=
App.ActiveDocument.recompute()
}}
<translate>

<!--T:65-->
'''Be careful where you recompute a FeaturePython object. Recomputing should be handled by a method external to its class.'''

</translate>
[[Image:fpo_box_properties.png | right]]
<translate>

<!--T:109-->
Now, test your changes as follows:
*Save your changes and reload your module.
*Delete all objects in the Tree view.
*Create a new box object from the Python console by calling {{incode|box.create('myBox')}}.

<!--T:87-->
Once the box is created and you've checked to make sure it has been recomputed, select the object and look at its properties. You should note two things:
*A new property group: ''Dimensions''.
*Three new properties: ''Height'', ''Length'' and ''Width''.

<!--T:89-->
Note also how the properties have units. More specifically, they have taken on the linear units set in the user preferences ({{MenuCommand|Edit → Preference... → General → Units}}).
{{Clear}}

<!--T:90-->
No doubt you noticed that three different values were entered for the dimensions: a floating-point value ({{incode|10.0}}) and two different strings ({{incode|'10 mm'}} and {{incode|'1 cm'}}). The {{incode|App::PropertyLength}} type assumes floating-point values are in millimeters, string values are parsed according to the units specified, and in the GUI all values are converted to the units specified in the user preferences ({{incode|mm}} in the image). This built-in behavior makes the {{incode|App::PropertyLength}} type ideal for dimensions.

<!--T:110-->
[[#top|top]]

===Trapping events=== <!--T:69-->

<!--T:70-->
The last element required for a basic FeaturePython object is event trapping. A FeaturePython object can react to events with callback functions. In our case we want the object to react whenever it is recomputed. In other words we want to trap recomputes. To accomplish this we need to add a function with a specific name, {{incode|execute()}}, to the object class. There are several other events that can be trapped, both in the FeaturePython object itself and in the [[Viewprovider|ViewProvider]], which we'll cover in [[Create_a_FeaturePython_object_part_II|Create a FeaturePython object part II]].

<!--T:113-->
For a complete reference of methods available to implement on FeautrePython classes, see [[FeaturePython_methods|FeaturePython methods]].

<!--T:92-->
Add the following after the {{incode|__init__()}} function:

</translate>
{{Code|code=
def execute(self, obj):
"""
Called on document recompute
"""

print('Recomputing {0:s} ({1:s})'.format(obj.Name, self.Type))
}}
<translate>

<!--T:72-->
Test the code by again following these steps:
*Save and reload the module.
*Delete all objects.
*Create a new box object.

<!--T:73-->
You should see the printed output in the Python Console, thanks to the {{incode|recompute()}} call we added to the {{incode|create()}} method. Of course, the {{incode|execute()}} method doesn't do anything here, except tell us that it was called, but it is the key to the magic of FeaturePython objects.

<!--T:93-->
That's it, you now know how to build a basic, functional FeaturePython object!

<!--T:111-->
[[#top|top]]

===Complete code=== <!--T:77-->

</translate>
{{Code|code=
import FreeCAD as App


def create(obj_name):
There's a lot of attributes there! That's because we're accessing the native FreeCAD FeaturePyton object that we created in the first line of our <code>create()</code> method.
"""
Object creation method
"""


obj = App.ActiveDocument.addObject('App::FeaturePython', obj_name)


box(obj)
Remember the <code>Proxy</code> property we added in our class <code>__init__()</code> method? Did you see it in the attribute list?


App.ActiveDocument.recompute()


return obj
Let's try looking at it's attributes:


class box():
>>> dir(mybox.Proxy)
['Object', 'Type', '__class__', '__delattr__', '__dict__', '__dir__',
...
'__str__', '__subclasshook__', '__weakref__']


def __init__(self, obj):
Among the list of protected methods and properties, we see our <code>Object</code> and <code>Type</code> properties!
"""
Default constructor
"""


self.Type = 'box'


obj.Proxy = self
Call the <code>Type</code> property and look at the result:
>>> mybox.Proxy.Type
'box'


obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = ""
obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = '10 mm'
obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = '1 cm'


def execute(self, obj):
Sure enough, it returns the value we assigned, so we know we're accessing the custom class itself through the FeaturePython object. likewise, if you call:
"""
>>> mybox.Proxy.Object
Called on document recompute
"""


print('Recomputing {0:s} ({1:s})'.format(obj.Name, self.Type))
}}
<translate>


<!--T:112-->
You'll get back the FeaturePython Object, the same thing that <code>mybox</code> refers to.
[[#top|top]]


<!--T:78-->
{{Docnav
|
|[[Create_a_FeaturePython_object_part_II|Create a FeaturePython object part II]]
|IconL=
|IconR=
}}


That was fun! But now let's see if we can make our class a little more interesting... and maybe more useful.


</translate>
</translate>
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{clear}}

Latest revision as of 17:25, 16 February 2023

Introduction

FeaturePython objects (also referred to as Scripted objects) provide the ability to extend FreeCAD with objects that integrate seamlessly into the FreeCAD framework.

This encourages:

  • Rapid prototyping of new objects and tools with custom Python classes.
  • Saving and restoring data (also known as serialization) through App::Property objects, without embedding any script in the FreeCAD document file.
  • Creative freedom to adapt FreeCAD for any task.

On this page we are going to construct a working example of a FeaturePython custom class, identifying all the major components and gaining an understanding of how everything works as we go along.

How does it work?

FreeCAD comes with a number of default object types for managing different kinds of geometry. Some of them have "FeaturePython" alternatives that allow for customization with a user defined Python class.

This custom Python class takes a reference to one of these objects and modifies it. For example, the Python class may add properties to the object or link it to other objects. In addition the Python class may implement certain methods to enable the object to respond to document events, making it possible to trap object property changes and document recomputes.

When working with custom classes and FeaturePython objects it is important to know that the custom class and its state are not saved in the document as this would require embedding a script in a FreeCAD document file, which would pose a significant security risk. Only the FeaturePython object itself is saved (serialized). But since the script module path is stored in the document, a user need only install the custom Python class code as an importable module, following the same folder structure, to regain the lost functionality.

top

Setting things up

FeaturePython Object classes need to act as importable modules in FreeCAD. That means you need to place them in a path that exists in your Python environment (or add it specifically). For the purposes of this tutorial, we're going to use the FreeCAD user Macro folder. But if you have another idea in mind, feel free to use that instead.

If you don't know where the FreeCAD Macro folder is type FreeCAD.getUserMacroDir(True) in FreeCAD's Python console:

  • On Linux it is usually /home/<username>/.local/share/FreeCAD/Macro/ (version 0.20 and above) or /home/<username>/.FreeCAD/Macro/ (version 0.19 and below).
  • On Windows it is %APPDATA%\FreeCAD\Macro\, which is usually C:\Users\<username>\Appdata\Roaming\FreeCAD\Macro\.
  • On macOS it is usually /Users/<username>/Library/Application Support/FreeCAD/Macro/.

Now we need to create some folders and files:

  • In the Macro folder create a new folder called fpo.
  • In the fpo folder create an empty file: __init__.py.
  • In the fpo folder, create a new folder called box.
  • In the box folder create two files: __init__.py and box.py (leave both empty for now).

Your folder structure should look like this:

Macro/
    |--> fpo/
        |--> __init__.py
        |--> box/
            |--> __init__.py
            |--> box.py

The fpo folder provides a nice place to play with new FeaturePython objects and the box folder is the module we will be working in. __init__.py tells Python that there is an importable module in the folder, and box.py will be the class file for our new FeaturePython Object.

With our module paths and files created, let's make sure FreeCAD is set up properly:

Finally, navigate to the Macro/fpo/box folder and open box.py in your favorite code editor. We will only edit that file.

top

A FeaturePython object

Let's get started by writing our class and its constructor:

class box():

    def __init__(self, obj):
        """
        Default constructor
        """

        self.Type = 'box'

        obj.Proxy = self

The __init__() method breakdown:

def __init__(self, obj): Parameters refer to the Python class itself and the FeaturePython object that it is attached to.
self.Type = 'box' String definition of the custom Python type.
obj.Proxy = self Stores a reference to the Python instance in the FeaturePython object.

Add the following code at the top of the file:

import FreeCAD as App

def create(obj_name):
    """
    Object creation method
    """

    obj = App.ActiveDocument.addObject('App::FeaturePython', obj_name)

    box(obj)

    return obj

The create() method breakdown:

import FreeCAD as App Standard import for most Python scripts, the App alias is not required.
obj = ... addObject(...) Creates a new FreeCAD FeaturePython object with the name passed to the method. If there is no name clash, this will be the label and the name of the created object. Otherwise, a unique name and label will be created based on 'obj_name'.
box(obj) Creates our custom class instance.
return obj Returns the FeaturePython object.

The create() method is not required, but it provides a nice way to encapsulate the object creation code.

top

Testing the code

Now we can test our new object. Save your code and return to FreeCAD. Make sure you have opened a new document, you can do this by pressing Ctrl+N or selecting File → New.

In the Python console type the following:

from fpo.box import box

Now we need to create our object:

mybox = box.create('my_box')

You should see a new object appear in the Tree view labelled "my_box".

Note that the icon is gray. FreeCAD is telling us that the object is not able to display anything in the 3D view. Click on the object and look at its properties in the Property editor. There is not much there, just the name of the object.

Also note that there is a small blue check mark next to the FeaturePython object in the Tree view. That is because when an object is created or changed it is "touched" and needs to be recomputed. Pressing the Std Refresh button will accomplish this. We will add some code to automate this later.

Let's look at our object's attributes:

dir(mybox)

This will return:

['Content', 'Document', 'ExpressionEngine', 'FullName', 'ID', 'InList',
...
'setPropertyStatus', 'supportedProperties', 'touch']

There are a lot of attributes because we're accessing the native FreeCAD FeaturePyton object created in the first line of our create() method. The Proxy property we added in our __init__() method is there too.

Let's inspect it with the dir() method:

dir(mybox.Proxy)

This will return:

['Type', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
...
'__str__', '__subclasshook__', '__weakref__']

We can see our Type property. Let's check it:

mybox.Proxy.Type

This will return:

'box'

This is indeed the assigned value, so we know we're accessing the custom class through the FeaturePython object.

Now let's see if we can make our class a little more interesting, and maybe more useful as well.

top

Adding properties

Properties are the lifeblood of a FeaturePython class. Fortunately, FreeCAD supports a number of property types for FeaturePython classes. These properties are attached directly to the FeaturePython object and are fully serialized when the file is saved. To avoid having to serialize data yourself, it is advisable to only use these property types.

Adding properties is done using the add_property() method. The syntax for the method is:

add_property(type, name, section, description)

You can view the list of supported properties by typing:

mybox.supportedProperties()

Let's try adding a property to our box class. Switch to your code editor, move to the __init__() method, and at the end of the method add:

obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = ""

Note how we're using the reference to the (serializable) FeaturePython object obj, and not the (non-serializable) Python class instance self.

Once you're done, save the changes and switch back to FreeCAD. Before we can observe the changes made to our code, we need to reload the module. This can be accomplished by restarting FreeCAD, but restarting FreeCAD every time we edit the code would be inconvenient. To make things easier type the following in the Python console:

from importlib import reload
reload(box)

With the module reloaded, let's see what we get when we create an object:

box.create('box_property_test')

You should see the new box object appear in the Tree view:

  • Select it and look at the Property editor. There, you should see the Description property.
  • Hover over the property name on the left and the tooltip should appear with the description you provided.
  • Select the field and type whatever you like. You'll notice that Python update commands are executed and displayed in the console as you type letters and the property changes.

top

Let's add some more properties. Return to your source code and add the following properties to the __init__() method:

obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = '10 mm'
obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = '1 cm'

And let's also add some code to recompute the document automatically. Add the following line above the return() statement in the create() method :

App.ActiveDocument.recompute()

Be careful where you recompute a FeaturePython object. Recomputing should be handled by a method external to its class.

Now, test your changes as follows:

  • Save your changes and reload your module.
  • Delete all objects in the Tree view.
  • Create a new box object from the Python console by calling box.create('myBox').

Once the box is created and you've checked to make sure it has been recomputed, select the object and look at its properties. You should note two things:

  • A new property group: Dimensions.
  • Three new properties: Height, Length and Width.

Note also how the properties have units. More specifically, they have taken on the linear units set in the user preferences (Edit → Preference... → General → Units).

No doubt you noticed that three different values were entered for the dimensions: a floating-point value (10.0) and two different strings ('10 mm' and '1 cm'). The App::PropertyLength type assumes floating-point values are in millimeters, string values are parsed according to the units specified, and in the GUI all values are converted to the units specified in the user preferences (mm in the image). This built-in behavior makes the App::PropertyLength type ideal for dimensions.

top

Trapping events

The last element required for a basic FeaturePython object is event trapping. A FeaturePython object can react to events with callback functions. In our case we want the object to react whenever it is recomputed. In other words we want to trap recomputes. To accomplish this we need to add a function with a specific name, execute(), to the object class. There are several other events that can be trapped, both in the FeaturePython object itself and in the ViewProvider, which we'll cover in Create a FeaturePython object part II.

For a complete reference of methods available to implement on FeautrePython classes, see FeaturePython methods.

Add the following after the __init__() function:

def execute(self, obj):
    """
    Called on document recompute
    """

    print('Recomputing {0:s} ({1:s})'.format(obj.Name, self.Type))

Test the code by again following these steps:

  • Save and reload the module.
  • Delete all objects.
  • Create a new box object.

You should see the printed output in the Python Console, thanks to the recompute() call we added to the create() method. Of course, the execute() method doesn't do anything here, except tell us that it was called, but it is the key to the magic of FeaturePython objects.

That's it, you now know how to build a basic, functional FeaturePython object!

top

Complete code

import FreeCAD as App

def create(obj_name):
    """
    Object creation method
    """

    obj = App.ActiveDocument.addObject('App::FeaturePython', obj_name)

    box(obj)

    App.ActiveDocument.recompute()

    return obj

class box():

    def __init__(self, obj):
        """
        Default constructor
        """

        self.Type = 'box'

        obj.Proxy = self

        obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = ""
        obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
        obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = '10 mm'
        obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = '1 cm'

    def execute(self, obj):
        """
        Called on document recompute
        """

        print('Recomputing {0:s} ({1:s})'.format(obj.Name, self.Type))

top