Python: Difference between revisions

From FreeCAD Documentation
(Assume we are already in 2020.)
(Changed a category)
 
(14 intermediate revisions by 7 users not shown)
Line 2: Line 2:
<translate>
<translate>
<!--T:25-->
<!--T:25-->
{{VeryImportantMessage|(January 2020) FreeCAD was originally designed to work with Python 2. Since Python 2 reached end of life in 2020, future development of FreeCAD will be done exclusively with Python 3, and backwards compatibility will not be supported.}}
{{VeryImportantMessage|FreeCAD was originally designed to work with Python 2.x. This series ended with 2.7.18 release dated April, 20th 2020 and is succeeded by Python 3. The further development of FreeCAD will be done exclusively with Python 3, and backwards compatibility will not be supported.}}


== Description == <!--T:26-->
== Description == <!--T:26-->

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


<!--T:27-->
<!--T:27-->
Line 14: Line 18:
<!--T:29-->
<!--T:29-->
See [[Introduction to Python|Introduction to Python]] to learn about the Python programming language, and then [[Python scripting tutorial|Python scripting tutorial]] and [[FreeCAD Scripting Basics|FreeCAD Scripting Basics]] to start scripting in FreeCAD.
See [[Introduction to Python|Introduction to Python]] to learn about the Python programming language, and then [[Python scripting tutorial|Python scripting tutorial]] and [[FreeCAD Scripting Basics|FreeCAD Scripting Basics]] to start scripting in FreeCAD.

== Readability == <!--T:44-->


<!--T:30-->
<!--T:30-->
When writing Python code, it's advisable to follow [https://www.python.org/dev/peps/pep-0008/ PEP8: Style Guide for Python Code].
Readability of Python code is one of the most important aspects of this language. Using a clear and consistent style within the Python community facilitates contributions by different developers, as most experienced Python programmers expect the code to be formatted in a certain way and to follow certain rules. When writing Python code, it is advisable to follow [https://www.python.org/dev/peps/pep-0008/ PEP8: Style Guide for Python Code] and [https://www.python.org/dev/peps/pep-0257/ PEP257: Docstring Conventions].

<!--T:45-->
These documents present explanations in a more user-friendly way:
* [https://realpython.com/python-pep8/ How to Write Beautiful Python Code With PEP 8]
* [https://realpython.com/documenting-python-code/ Documenting Python Code: A Complete Guide].


== Conventions == <!--T:31-->
== Conventions == <!--T:31-->
Line 26: Line 37:


{{Code|code=
{{Code|code=
Wire = makeWire(pointslist, closed=False, placement=None, face=None, support=None)
Wire = make_wire(pointslist, closed=False, placement=None, face=None, support=None)
}}
}}
<translate>
<translate>
Line 35: Line 46:


{{Code|code=
{{Code|code=
Wire = makeWire(pointslist, False, None, None, None)
Wire = make_wire(pointslist, False, None, None, None)
Wire = makeWire(pointslist, False, None, None)
Wire = make_wire(pointslist, False, None, None)
Wire = makeWire(pointslist, False, None)
Wire = make_wire(pointslist, False, None)
Wire = makeWire(pointslist, False)
Wire = make_wire(pointslist, False)
Wire = makeWire(pointslist)
Wire = make_wire(pointslist)
}}
}}


Line 50: Line 61:
</translate>
</translate>
{{Code|code=
{{Code|code=
Wire = makeWire(pointslist, closed=False, placement=None, face=None)
Wire = make_wire(pointslist, closed=False, placement=None, face=None)
Wire = makeWire(pointslist, closed=False, face=None, placement=None)
Wire = make_wire(pointslist, closed=False, face=None, placement=None)
Wire = makeWire(pointslist, placement=None, closed=False, face=None)
Wire = make_wire(pointslist, placement=None, closed=False, face=None)
Wire = makeWire(pointslist, support=None, closed=False, placement=None, face=None)
Wire = make_wire(pointslist, support=None, closed=False, placement=None, face=None)
}}
}}
<translate>
<translate>
Line 65: Line 76:
p2 = Vector(1, 1, 0)
p2 = Vector(1, 1, 0)
p3 = Vector(2, 0, 0)
p3 = Vector(2, 0, 0)
Wire = makeWire([p1, p2, p3], closed=True)
Wire = make_wire([p1, p2, p3], closed=True)
}}
}}
<translate>
<translate>
Line 77: Line 88:
2, 4, 5]
2, 4, 5]


Wire = makeWire(pointslist,
Wire = make_wire(pointslist,
False, None,
False, None,
None, None)
None, None)
Line 87: Line 98:
</translate>
</translate>
{{Code|code=
{{Code|code=
Wire = makeWire(pointslist, closed=True, face=True)
Wire = make_wire(pointslist, closed=True, face=True)
Window = makeWindow(Wire, name="Big window")
Window = make_window(Wire, name="Big window")
}}
}}
<translate>
<translate>
Line 99: Line 110:
<!--T:41-->
<!--T:41-->
This creates prefixed functions, that is, {{incode|module.function()}}. This system prevents name clashes with functions that are named the same but that come from different modules.
This creates prefixed functions, that is, {{incode|module.function()}}. This system prevents name clashes with functions that are named the same but that come from different modules.
For example, the two functions {{incode|Arch.makeWindow()}} and {{incode|myModule.makeWindow()}} may coexist without problem.
For example, the two functions {{incode|Arch.make_window()}} and {{incode|myModule.make_window()}} may coexist without problem.


<!--T:42-->
<!--T:42-->
Line 106: Line 117:
</translate>
</translate>
{{Code|code=
{{Code|code=
import FreeCAD, Draft
import FreeCAD as App
import Draft


p1 = FreeCAD.Vector(0, 0, 0)
p1 = App.Vector(0, 0, 0)
p2 = FreeCAD.Vector(1, 1, 0)
p2 = App.Vector(1, 1, 0)
p3 = FreeCAD.Vector(2, 0, 0)
p3 = App.Vector(2, 0, 0)
Wire = Draft.makeWire([p1, p2, p3], closed=True)
Wire = Draft.make_wire([p1, p2, p3], closed=True)
}}
}}


{{Code|code=
{{Code|code=
import FreeCAD, Draft, Arch
import FreeCAD as App
import Draft
import Arch


p1 = FreeCAD.Vector(0, 0, 0)
p1 = App.Vector(0, 0, 0)
p2 = FreeCAD.Vector(1, 0, 0)
p2 = App.Vector(1, 0, 0)
p3 = FreeCAD.Vector(1, 1, 0)
p3 = App.Vector(1, 1, 0)
p4 = FreeCAD.Vector(0, 2, 0)
p4 = App.Vector(0, 2, 0)
pointslist = [p1, p2, p3, p4]
pointslist = [p1, p2, p3, p4]


Wire = Draft.makeWire(pointslist, closed=True, face=True)
Wire = Draft.make_wire(pointslist, closed=True, face=True)
Structure = Arch.makeStructure(Wire, name="Big pillar")
Structure = Arch.make_structure(Wire, name="Big pillar")
}}
}}

<translate>
<translate>

<!--T:43-->
[[Category:API Documentation]]
[[Category:Developer Documentation]]
[[Category:Poweruser Documentation]]
[[Category:User Documentation]]
</translate>
</translate>
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:API{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
[[Category:Glossary{{#translation:}}]]

Latest revision as of 09:22, 17 October 2021

FreeCAD was originally designed to work with Python 2.x. This series ended with 2.7.18 release dated April, 20th 2020 and is succeeded by Python 3. The further development of FreeCAD will be done exclusively with Python 3, and backwards compatibility will not be supported.

Description

Python is a general purpose, high level programming language that is very commonly used in large applications to automate some tasks by creating scripts or macros.

In FreeCAD, Python code can be used to create various elements programmatically, without needing to click on the graphical user interface. Additionally, many tools and workbenches of FreeCAD are programmed in Python.

See Introduction to Python to learn about the Python programming language, and then Python scripting tutorial and FreeCAD Scripting Basics to start scripting in FreeCAD.

Readability

Readability of Python code is one of the most important aspects of this language. Using a clear and consistent style within the Python community facilitates contributions by different developers, as most experienced Python programmers expect the code to be formatted in a certain way and to follow certain rules. When writing Python code, it is advisable to follow PEP8: Style Guide for Python Code and PEP257: Docstring Conventions.

These documents present explanations in a more user-friendly way:

Conventions

In this documentation, some conventions for Python examples should be followed.

This is a typical function signature

Wire = make_wire(pointslist, closed=False, placement=None, face=None, support=None)
  • Arguments with key-value pairs are optional, with the default value indicated in the signature. This means that the following calls are equivalent:
Wire = make_wire(pointslist, False, None, None, None)
Wire = make_wire(pointslist, False, None, None)
Wire = make_wire(pointslist, False, None)
Wire = make_wire(pointslist, False)
Wire = make_wire(pointslist)
In this example the first argument doesn't have a default value so it should always be included.
  • When the arguments are given with the explicit key, the optional arguments can be given in any order. This means that the following calls are equivalent:
Wire = make_wire(pointslist, closed=False, placement=None, face=None)
Wire = make_wire(pointslist, closed=False, face=None, placement=None)
Wire = make_wire(pointslist, placement=None, closed=False, face=None)
Wire = make_wire(pointslist, support=None, closed=False, placement=None, face=None)
  • Python's guidelines stress readability of code; in particular, parentheses should immediately follow the function name, and a space should follow a comma.
p1 = Vector(0, 0, 0)
p2 = Vector(1, 1, 0)
p3 = Vector(2, 0, 0)
Wire = make_wire([p1, p2, p3], closed=True)
  • If code needs to be broken over several lines, this should be done at a comma inside brackets or parentheses; the second line should be aligned with the previous one.
a_list = [1, 2, 3,
          2, 4, 5]

Wire = make_wire(pointslist,
                False, None,
                None, None)
  • Functions may return an object that can be used as the base of another drawing function.
Wire = make_wire(pointslist, closed=True, face=True)
Window = make_window(Wire, name="Big window")

Imports

Python functions are stored in files called modules. Before using any function in that module, the module must be included in the document with the import instruction.

This creates prefixed functions, that is, module.function(). This system prevents name clashes with functions that are named the same but that come from different modules. For example, the two functions Arch.make_window() and myModule.make_window() may coexist without problem.

Full examples should include the necessary imports and the prefixed functions.

import FreeCAD as App
import Draft

p1 = App.Vector(0, 0, 0)
p2 = App.Vector(1, 1, 0)
p3 = App.Vector(2, 0, 0)
Wire = Draft.make_wire([p1, p2, p3], closed=True)
import FreeCAD as App
import Draft
import Arch

p1 = App.Vector(0, 0, 0)
p2 = App.Vector(1, 0, 0)
p3 = App.Vector(1, 1, 0)
p4 = App.Vector(0, 2, 0)
pointslist = [p1, p2, p3, p4]

Wire = Draft.make_wire(pointslist, closed=True, face=True)
Structure = Arch.make_structure(Wire, name="Big pillar")