Difference between revisions of "FreeCAD Scripting Basics"

From FreeCAD Documentation
Jump to navigation Jump to search
(Minor edits to 'Using additional modules'.)
(Added: Category:Developer Documentation.)
 
(9 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
{{Docnav
 
{{Docnav
 
|[[Python_scripting_tutorial|Python scripting tutorial]]
 
|[[Python_scripting_tutorial|Python scripting tutorial]]
|[[Mesh_Scripting|Mesh Scripting]]
+
|[[Topological_data_scripting|Topological data scripting]]
 
}}
 
}}
  
Line 14: Line 14:
 
==Python scripting in FreeCAD== <!--T:1-->
 
==Python scripting in FreeCAD== <!--T:1-->
  
 +
<!--T:44-->
 
FreeCAD is built from scratch to be totally controlled by Python scripts. Almost all parts of FreeCAD, such as the interface, the scene contents, and even the representation of this content in the 3D views, are accessible from the built-in Python interpreter or from your own scripts. As a result, FreeCAD is probably one of the most deeply customizable engineering applications available today.
 
FreeCAD is built from scratch to be totally controlled by Python scripts. Almost all parts of FreeCAD, such as the interface, the scene contents, and even the representation of this content in the 3D views, are accessible from the built-in Python interpreter or from your own scripts. As a result, FreeCAD is probably one of the most deeply customizable engineering applications available today.
  
Line 21: Line 22:
 
===The interpreter=== <!--T:4-->
 
===The interpreter=== <!--T:4-->
  
 +
<!--T:45-->
 
From the interpreter, you can access all your system-installed Python modules, as well as the built-in FreeCAD modules, and all additional FreeCAD modules you installed later. The screenshot below shows the Python interpreter:
 
From the interpreter, you can access all your system-installed Python modules, as well as the built-in FreeCAD modules, and all additional FreeCAD modules you installed later. The screenshot below shows the Python interpreter:
  
Line 40: Line 42:
 
<!--T:10-->
 
<!--T:10-->
 
By right-clicking in the interpreter window, you also have several other options, such as copy the entire history (useful when you want to experiment with things before making a full script of them), or insert a filename with complete path.
 
By right-clicking in the interpreter window, you also have several other options, such as copy the entire history (useful when you want to experiment with things before making a full script of them), or insert a filename with complete path.
 +
 +
<!--T:46-->
 +
[[#top|top]]
  
 
===Python Help=== <!--T:11-->
 
===Python Help=== <!--T:11-->
  
 +
<!--T:47-->
 
In the FreeCAD {{MenuCommand|Help}} menu, you'll find an entry labeled {{MenuCommand|Automatic python modules documentation}}, which will open a browser window containing a complete, realtime-generated documentation of all Python modules available to the FreeCAD interpreter, including Python and FreeCAD built-in modules, system-installed modules, and FreeCAD additional modules. The documentation available there depends on how much effort each module developer put into documenting his code, but Python modules have a reputation for being fairly well documented. Your FreeCAD window must stay open for this documentation system to work.
 
In the FreeCAD {{MenuCommand|Help}} menu, you'll find an entry labeled {{MenuCommand|Automatic python modules documentation}}, which will open a browser window containing a complete, realtime-generated documentation of all Python modules available to the FreeCAD interpreter, including Python and FreeCAD built-in modules, system-installed modules, and FreeCAD additional modules. The documentation available there depends on how much effort each module developer put into documenting his code, but Python modules have a reputation for being fairly well documented. Your FreeCAD window must stay open for this documentation system to work.
 
The entry {{MenuCommand|Python scripting documentation}} will give you a quick link to the [[Power_users_hub|Power users hub]] wiki section.
 
The entry {{MenuCommand|Python scripting documentation}} will give you a quick link to the [[Power_users_hub|Power users hub]] wiki section.
 +
 +
<!--T:48-->
 +
[[#top|top]]
  
 
==Built-in modules== <!--T:12-->
 
==Built-in modules== <!--T:12-->
  
 +
<!--T:49-->
 
Since FreeCAD is designed so that it can also be run without a Graphical User Interface (GUI), almost all its functionality is separated into two groups: Core functionality, named {{incode|App}}, and GUI functionality, named {{incode|Gui}}. These two modules can also be accessed from scripts outside of the interpreter, by the names {{incode|FreeCAD}} and {{incode|FreeCADGui}} respectively.
 
Since FreeCAD is designed so that it can also be run without a Graphical User Interface (GUI), almost all its functionality is separated into two groups: Core functionality, named {{incode|App}}, and GUI functionality, named {{incode|Gui}}. These two modules can also be accessed from scripts outside of the interpreter, by the names {{incode|FreeCAD}} and {{incode|FreeCADGui}} respectively.
  
Line 58: Line 68:
 
<!--T:15-->
 
<!--T:15-->
 
Listing the content of these modules is not very useful because they grow quite fast as FreeCAD develops. But the two browsing tools provided (the class browser and the Python help) should give you complete and up-to-date documentation at any moment.
 
Listing the content of these modules is not very useful because they grow quite fast as FreeCAD develops. But the two browsing tools provided (the class browser and the Python help) should give you complete and up-to-date documentation at any moment.
 +
 +
<!--T:50-->
 +
[[#top|top]]
  
 
===The App and Gui objects=== <!--T:16-->
 
===The App and Gui objects=== <!--T:16-->
  
 +
<!--T:51-->
 
As already mentioned, in FreeCAD everything is separated into core and representation. This includes the 3D objects. You can access defining properties of objects (called features in FreeCAD) via the {{incode|App}} module, and change the way they are represented on screen via the {{incode|Gui}} module. For example, a cube has properties that define it (like width, length, height) that are stored in an {{incode|App}} object, and representation properties (like faces color, drawing mode) that are stored in a corresponding {{incode|Gui}} object.
 
As already mentioned, in FreeCAD everything is separated into core and representation. This includes the 3D objects. You can access defining properties of objects (called features in FreeCAD) via the {{incode|App}} module, and change the way they are represented on screen via the {{incode|Gui}} module. For example, a cube has properties that define it (like width, length, height) that are stored in an {{incode|App}} object, and representation properties (like faces color, drawing mode) that are stored in a corresponding {{incode|Gui}} object.
  
Line 107: Line 121:
 
<!--T:23-->
 
<!--T:23-->
 
If you are in command-line mode and have no GUI, the last line will return {{incode|None}}.
 
If you are in command-line mode and have no GUI, the last line will return {{incode|None}}.
 +
 +
<!--T:52-->
 +
[[#top|top]]
  
 
===The Document objects=== <!--T:24-->
 
===The Document objects=== <!--T:24-->
  
 +
<!--T:53-->
 
In FreeCAD all your work resides inside documents. A document contains your geometry and can be saved to a file. Several documents can be opened at the same time. The document, like the geometry contained inside, has {{incode|App}} and {{incode|Gui}} objects. The {{incode|App}} object contains your actual geometry definitions, while the {{incode|Gui}} object contains the different views of your document. You can open several windows, each one viewing your work with a different zoom factor or from a different direction. These views are all part of your document's {{incode|Gui}} object.
 
In FreeCAD all your work resides inside documents. A document contains your geometry and can be saved to a file. Several documents can be opened at the same time. The document, like the geometry contained inside, has {{incode|App}} and {{incode|Gui}} objects. The {{incode|App}} object contains your actual geometry definitions, while the {{incode|Gui}} object contains the different views of your document. You can open several windows, each one viewing your work with a different zoom factor or from a different direction. These views are all part of your document's {{incode|Gui}} object.
  
Line 147: Line 165:
 
}}
 
}}
 
<translate>
 
<translate>
 +
 +
<!--T:54-->
 +
[[#top|top]]
  
 
==Using additional modules== <!--T:29-->
 
==Using additional modules== <!--T:29-->
  
The {{incode|FreeCAD}} and {{incode|FreeCADGui}} modules are only responsible for creating and managing objects in the FreeCAD document. They don't actually do anything more such as creating or modifying geometry. This is because that geometry can be of several types, and therefore requires additional modules, each responsible for managing a certain geometry type. For example, the [[Part Module|Part Module]] using the OpenCascade kernel is able to create and manipulate [http://en.wikipedia.org/wiki/Boundary_representation BRep] type geometry. Whereas the [[Mesh Module|Mesh Module]] is able to build and modify mesh objects. In this manner FreeCAD is able to handle a wide variety of object types, that can all coexist in the same document, and new types can easily be added in the future.
+
<!--T:55-->
 +
The {{incode|FreeCAD}} and {{incode|FreeCADGui}} modules are only responsible for creating and managing objects in the FreeCAD document. They don't actually do anything more such as creating or modifying geometry. This is because that geometry can be of several types, and therefore requires additional modules, each responsible for managing a certain geometry type. For example, the [[Part Module|Part Module]], using the OpenCascade kernel, is able to create and manipulate [http://en.wikipedia.org/wiki/Boundary_representation BRep] type geometry. Whereas the [[Mesh Module|Mesh Module]] is able to build and modify mesh objects. In this manner FreeCAD is able to handle a wide variety of object types, that can all coexist in the same document, and new types can easily be added in the future.
 +
 
 +
<!--T:56-->
 +
[[#top|top]]
  
 
===Creating objects=== <!--T:30-->
 
===Creating objects=== <!--T:30-->
  
Each module has its own way to treat its geometry, but one thing they usually all can do is create objects in the document. But the FreeCAD document is also aware of the available object types provided by the modules:
+
<!--T:57-->
 +
Each module has its own way of dealing with geometry, but one thing they usually all can do is create objects in the document. But the FreeCAD document is also aware of the available object types provided by the modules:
  
 
</translate>
 
</translate>
Line 163: Line 189:
  
 
<!--T:31-->
 
<!--T:31-->
will list you all the possible objects you can create. For example, let's create a mesh (treated by the mesh module) and a part (treated by the part module):
+
will list all possible objects you can create. For example, let's create a mesh (handled by the {{incode|Mesh}} module) and a part (handled by the {{incode|Part}} module):
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
myMesh = FreeCAD.ActiveDocument.addObject("Mesh::Feature","myMeshName")
+
myMesh = FreeCAD.ActiveDocument.addObject("Mesh::Feature", "myMeshName")
myPart = FreeCAD.ActiveDocument.addObject("Part::Feature","myPartName")
+
myPart = FreeCAD.ActiveDocument.addObject("Part::Feature", "myPartName")
 
}}
 
}}
 
<translate>
 
<translate>
  
 
<!--T:32-->
 
<!--T:32-->
The first argument is the object type, the second the name of the object. Our two objects look almost the same: They don't contain any geometry yet, and most of their properties are the same when you inspect them with dir(myMesh) and dir(myPart). Except for one, myMesh has a "Mesh" property and "Part" has a "Shape" property. That is where the Mesh and Part data are stored. For example, let's create a Part cube and store it in our myPart object:
+
The first argument is the object type, the second the name of the object. Our two objects look almost the same: They don't contain any geometry yet, and most of their properties are the same when you inspect them with {{incode|dir(myMesh)}} and {{incode|dir(myPart)}}. Except for one thing, {{incode|myMesh}} has a {{incode|Mesh}} property and {{incode|myPart}} has a {{incode|Shape}} property. That is where the Mesh and Part data are stored. For example, let's create a {{incode|Part}} cube and store it in our {{incode|myPart}} object:
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
 
import Part
 
import Part
cube = Part.makeBox(2,2,2)
+
cube = Part.makeBox(2, 2, 2)
 
myPart.Shape = cube
 
myPart.Shape = cube
 
}}
 
}}
Line 184: Line 210:
  
 
<!--T:33-->
 
<!--T:33-->
You could try storing the cube inside the Mesh property of the myMesh object, it will return an error complaining of the wrong type. That is because those properties are made to store only a certain type. In the myMesh's Mesh property, you can only save stuff created with the Mesh module. Note that most modules also have a shortcut to add their geometry to the document:
+
You could try storing the cube inside the {{incode|Mesh}} property of the {{incode|myMesh}} object, but it will return an error. That is because each properties is made to store only a certain type. In a {{incode|Mesh}} property, you can only save stuff created with the {{incode|Mesh}} module. Note that most modules also have a shortcut to add their geometry to the document:
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
 
import Part
 
import Part
cube = Part.makeBox(2,2,2)
+
cube = Part.makeBox(2, 2, 2)
 
Part.show(cube)
 
Part.show(cube)
 
}}
 
}}
 
<translate>
 
<translate>
 +
 +
<!--T:58-->
 +
[[#top|top]]
  
 
===Modifying objects=== <!--T:34-->
 
===Modifying objects=== <!--T:34-->
  
Modifying an object is done the same way:
+
<!--T:59-->
 +
Modifying an object is done in the same way:
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
 
import Part
 
import Part
cube = Part.makeBox(2,2,2)
+
cube = Part.makeBox(2, 2, 2)
 
myPart.Shape = cube
 
myPart.Shape = cube
 
}}
 
}}
Line 211: Line 241:
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
biggercube = Part.makeBox(5,5,5)
+
biggercube = Part.makeBox(5, 5, 5)
 
myPart.Shape = biggercube
 
myPart.Shape = biggercube
 
}}
 
}}
 
<translate>
 
<translate>
 +
 +
<!--T:60-->
 +
[[#top|top]]
  
 
===Querying objects=== <!--T:36-->
 
===Querying objects=== <!--T:36-->
  
 +
<!--T:61-->
 
You can always look at the type of an object like this:
 
You can always look at the type of an object like this:
  
Line 223: Line 257:
 
{{Code|code=
 
{{Code|code=
 
myObj = FreeCAD.ActiveDocument.getObject("myObjectName")
 
myObj = FreeCAD.ActiveDocument.getObject("myObjectName")
print myObj.TypeId
+
print(myObj.TypeId)
 
}}
 
}}
 
<translate>
 
<translate>
  
 
<!--T:37-->
 
<!--T:37-->
or know if an object is derived from one of the basic ones (Part Feature, Mesh Feature, etc):
+
or check if an object is derived from one of the basic ones (Part Feature, Mesh Feature, etc):
  
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
print myObj.isDerivedFrom("Part::Feature")
+
print(myObj.isDerivedFrom("Part::Feature"))
 
}}
 
}}
 
<translate>
 
<translate>
  
 
<!--T:38-->
 
<!--T:38-->
Now you can really start playing with FreeCAD! To look at what you can do with the [[Part Module|Part Module]], read the [[Topological data scripting|Part scripting]] page, or the [[Mesh Scripting|Mesh Scripting]] page for working with the [[Mesh Module|Mesh Module]]. Note that, although the Part and Mesh modules are the most complete and widely used, other modules such as the [[Draft Module|Draft Module]] also have [[Draft API|scripting]] APIs that can be useful to you. For a complete list of each modules and their available tools, visit the [[:Category:API|Category:API]] section.
+
Now you can really start playing with FreeCAD! For a complete list of available modules and their tools, visit the [[:Category:API|Category:API]] section.
 +
 
 +
<!--T:62-->
 +
[[#top|top]]
  
 
<!--T:39-->
 
<!--T:39-->
 
{{Docnav
 
{{Docnav
 
|[[Python_scripting_tutorial|Python scripting tutorial]]
 
|[[Python_scripting_tutorial|Python scripting tutorial]]
|[[Mesh_Scripting|Mesh Scripting]]
+
|[[Topological_data_scripting|Topological data scripting]]
 
}}
 
}}
  
 
</translate>
 
</translate>
 
{{Powerdocnavi{{#translation:}}}}
 
{{Powerdocnavi{{#translation:}}}}
[[Category:Python_Code{{#translation:}}]]
+
[[Category:Developer Documentation{{#translation:}}]]
 +
[[Category:Python Code{{#translation:}}]]
 
{{clear}}
 
{{clear}}

Latest revision as of 12:20, 3 June 2020

Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Türkçe • ‎español • ‎français • ‎italiano • ‎polski • ‎português • ‎română • ‎svenska • ‎čeština • ‎русский • ‎中文(中国大陆)‎

Python scripting in FreeCAD

FreeCAD is built from scratch to be totally controlled by Python scripts. Almost all parts of FreeCAD, such as the interface, the scene contents, and even the representation of this content in the 3D views, are accessible from the built-in Python interpreter or from your own scripts. As a result, FreeCAD is probably one of the most deeply customizable engineering applications available today.

If you are not familiar with Python, we recommend you search for tutorials on the internet and have a quick look at its structure. Python is a very easy language to learn, especially because it can be run inside an interpreter, where simple commands, right up to complete programs, can be executed on the fly without the need to compile anything. FreeCAD has a built-in Python interpreter. If you don't see the window labeled Python console as shown below, you can activate it under the View → Panels → Python console.

The interpreter

From the interpreter, you can access all your system-installed Python modules, as well as the built-in FreeCAD modules, and all additional FreeCAD modules you installed later. The screenshot below shows the Python interpreter:

The FreeCAD Python interpreter

From the interpreter, you can execute Python code and browse through the available classes and functions. FreeCAD provides a very handy class browser for exploration of the FreeCAD world: When you type the name of a known class followed by a period (meaning you want to add something from that class), a class browser window opens, where you can navigate between available subclasses and methods. When you select something, an associated help text (if it exists) is displayed:

The FreeCAD class browser

So, start here by typing App. or Gui. and see what happens. Another more generic Python way of exploring the content of modules and classes is to use the print(dir()) command. For example, typing print(dir()) will list all modules currently loaded in FreeCAD. print(dir(App)) will show you everything inside the App module, etc.

Another useful feature of the interpreter is the possibility to go back through the command history and retrieve a line of code that you already typed earlier. To navigate through the command history, just use Up arrow or Down arrow.

By right-clicking in the interpreter window, you also have several other options, such as copy the entire history (useful when you want to experiment with things before making a full script of them), or insert a filename with complete path.

top

Python Help

In the FreeCAD Help menu, you'll find an entry labeled Automatic python modules documentation, which will open a browser window containing a complete, realtime-generated documentation of all Python modules available to the FreeCAD interpreter, including Python and FreeCAD built-in modules, system-installed modules, and FreeCAD additional modules. The documentation available there depends on how much effort each module developer put into documenting his code, but Python modules have a reputation for being fairly well documented. Your FreeCAD window must stay open for this documentation system to work. The entry Python scripting documentation will give you a quick link to the Power users hub wiki section.

top

Built-in modules

Since FreeCAD is designed so that it can also be run without a Graphical User Interface (GUI), almost all its functionality is separated into two groups: Core functionality, named App, and GUI functionality, named Gui. These two modules can also be accessed from scripts outside of the interpreter, by the names FreeCAD and FreeCADGui respectively.

  • In the App module you'll find everything related to the application itself, like methods for opening or closing files, and to the documents, like setting the active document or listing their contents.
  • In the Gui module, you'll find tools for accessing and managing Gui elements, like the workbenches and their toolbars, and, more interestingly, the graphical representation of all FreeCAD content.

Listing the content of these modules is not very useful because they grow quite fast as FreeCAD develops. But the two browsing tools provided (the class browser and the Python help) should give you complete and up-to-date documentation at any moment.

top

The App and Gui objects

As already mentioned, in FreeCAD everything is separated into core and representation. This includes the 3D objects. You can access defining properties of objects (called features in FreeCAD) via the App module, and change the way they are represented on screen via the Gui module. For example, a cube has properties that define it (like width, length, height) that are stored in an App object, and representation properties (like faces color, drawing mode) that are stored in a corresponding Gui object.

This way of doing things allows a very wide range of uses, like having algorithms work only on the definition part of features, without the need to care about any visual part, or even redirect the content of the document to non-graphical application, such as lists, spreadsheets, or element analysis.

For every App object in your document, there exists a corresponding Gui object. In fact the document itself has both an App and a Gui object. This, of course, only applies when you run FreeCAD with its full interface. In the command-line version no GUI exists, so only App objects are available. Note that the Gui part of objects is re-generated every time an App object is marked as 'to be recomputed' (for example when one of its parameters changes), so any changes made directly to the Gui object may be lost.

To access the App part of something, you type:

myObject = App.ActiveDocument.getObject("ObjectName")

where "ObjectName" is the name of your object. You can also type:

myObject = App.ActiveDocument.ObjectName

To access the Gui part of the same object, you type:

myViewObject = Gui.ActiveDocument.getObject("ObjectName")

where "ObjectName" is the name of your object. You can also type:

myViewObject = App.ActiveDocument.ObjectName.ViewObject

If you are in command-line mode and have no GUI, the last line will return None.

top

The Document objects

In FreeCAD all your work resides inside documents. A document contains your geometry and can be saved to a file. Several documents can be opened at the same time. The document, like the geometry contained inside, has App and Gui objects. The App object contains your actual geometry definitions, while the Gui object contains the different views of your document. You can open several windows, each one viewing your work with a different zoom factor or from a different direction. These views are all part of your document's Gui object.

To access the App part of the currently open (active) document, you type:

myDocument = App.ActiveDocument

To create a new document, type:

myDocument = App.newDocument("Document Name")

To access the Gui part of the currently open (active) document, you type:

myGuiDocument = Gui.ActiveDocument

To access the current view, you type:

myView = Gui.ActiveDocument.ActiveView

top

Using additional modules

The FreeCAD and FreeCADGui modules are only responsible for creating and managing objects in the FreeCAD document. They don't actually do anything more such as creating or modifying geometry. This is because that geometry can be of several types, and therefore requires additional modules, each responsible for managing a certain geometry type. For example, the Part Module, using the OpenCascade kernel, is able to create and manipulate BRep type geometry. Whereas the Mesh Module is able to build and modify mesh objects. In this manner FreeCAD is able to handle a wide variety of object types, that can all coexist in the same document, and new types can easily be added in the future.

top

Creating objects

Each module has its own way of dealing with geometry, but one thing they usually all can do is create objects in the document. But the FreeCAD document is also aware of the available object types provided by the modules:

FreeCAD.ActiveDocument.supportedTypes()

will list all possible objects you can create. For example, let's create a mesh (handled by the Mesh module) and a part (handled by the Part module):

myMesh = FreeCAD.ActiveDocument.addObject("Mesh::Feature", "myMeshName")
myPart = FreeCAD.ActiveDocument.addObject("Part::Feature", "myPartName")

The first argument is the object type, the second the name of the object. Our two objects look almost the same: They don't contain any geometry yet, and most of their properties are the same when you inspect them with dir(myMesh) and dir(myPart). Except for one thing, myMesh has a Mesh property and myPart has a Shape property. That is where the Mesh and Part data are stored. For example, let's create a Part cube and store it in our myPart object:

import Part
cube = Part.makeBox(2, 2, 2)
myPart.Shape = cube

You could try storing the cube inside the Mesh property of the myMesh object, but it will return an error. That is because each properties is made to store only a certain type. In a Mesh property, you can only save stuff created with the Mesh module. Note that most modules also have a shortcut to add their geometry to the document:

import Part
cube = Part.makeBox(2, 2, 2)
Part.show(cube)

top

Modifying objects

Modifying an object is done in the same way:

import Part
cube = Part.makeBox(2, 2, 2)
myPart.Shape = cube

Now let's change the shape by a bigger one:

biggercube = Part.makeBox(5, 5, 5)
myPart.Shape = biggercube

top

Querying objects

You can always look at the type of an object like this:

myObj = FreeCAD.ActiveDocument.getObject("myObjectName")
print(myObj.TypeId)

or check if an object is derived from one of the basic ones (Part Feature, Mesh Feature, etc):

print(myObj.isDerivedFrom("Part::Feature"))

Now you can really start playing with FreeCAD! For a complete list of available modules and their tools, visit the Category:API section.

top