Extra python modules

The python interpreter inside FreeCAD can easily be extended by adding new modules to your system's python installation. Those modules will be automatically detected and used by FreeCAD.

All pyhton modules can be used from within FreeCAD, but several of them, listed below, have a special importance because they allow python programs complete access to core functionality of FreeCAD. Examples of use of those modules can also be found on the Code snippets page.

PyQt4
homepage: http://www.riverbankcomputing.co.uk/pyqt

PyQt (version 4) is a python bindings library which allow programs to access, create or modify Qt interfaces. Since the FreeCAD interface is built with Qt, installing PyQt4 on your system allow python programs inside FreeCAD to access all the interface, modify its parts, create new widgets or gather info from interface parts.

PyQt is released under a multiple licensing system, same system as used by Qt. To resume, there is a commercial version and a free GPL version. If you want to use it to make commercial (closed source) programs, you need to purchase the commercial license, otherwise you can simply install and use freely the GPL version.

Installation
Before installing PyQt4, you obviously need a python environment installed and working.

Linux

The simplest way to install PyQt4 is through your distribution's package manager. On Debian/Ubuntu systems, the package name is generally python-qt4, while on RPM-based systems it is named pyqt4. The necessary dependencies (Qt and SIP) will be taken care of automatically.

Windows

The program can be downloaded from here. You'll need to install the Qt and SIP libraries before installing pyqt4 (to be documented).

Usage
Once it is installed, you can check that everything is working by typing in FreeCAD python console:

To access the FreeCAD interface, type:

Now you can start to explore the interface with the dir command. You can add new elements, like a custom widget, with commands like:

Documentation
More pyQt4 tutorials (including how to build interfaces with Qt Designer to use with python):

http://www.rkblog.rk.edu.pl/w/p/introduction-pyqt4/ - a simple introduction

http://www.zetcode.com/tutorials/pyqt4/ - very complete in-depth tutorial

Pivy
homepage: http://pivy.coin3d.org/

Pivy is a coin bindings library for python, officially supported by coin. Coin itself is a toolkit for building 3D applications in OpenGL. It is the toolkit that FreeCAD uses to draw its 3d Scene on the screen. Installing Pivy on your system will allow python programs to access the FreeCAD scenegraph, draw new objects on the scene and use the wide range of availible Coin tools for drawing operations. Coin is based on the open Inventor scene description language.

It is important to know that FreeCAD only uses coin for representation of objects on the screen, which is separated from the definition of objects. This means that pivy won't be able to modify existing objects, neither to create valid FreeCAD objects. But it can be used to draw all kind of temporary things on screen, such as axis, grids, manipulators, construction geometry, etc...

Pivy, as well as Coin, is released under a GPL license.

Installation
Linux

Unfortunately pivy is too new to have made its way to the usual repositories, so it is unlikely that you'll find it in your package manager.

You'll then need to compile it yourself. I assume here that you are familiar with the steps needed to compile programs.

To get pivy working you should get the latest sources from the project's repository: svn co https://svn.coin3d.org/repos/Pivy/trunk Pivy Then you need a tool called SWIG to generate the C++ code for the Python bindings. It is recommended to use version 1.3.25 of SWIG, not the latest version, because at the moment pivy will only function correctly with 1.3.25. Download a 1.3.25 source tarball from http://www.swig.org. Then unpack it and from a command line do ./configure make make install (or checkinstall if you use it) as root It takes just a few seconds to build. After that go to the pivy sources and call python setup.py build which creates the source files. You may run into a compiler error where a 'const char*' cannot be converted in a 'char*'. To fix that you just need to write a 'const' before in the appropriate lines. There are six lines to fix. After that, install by issuing: python setup.py install (or checkinstall python setup.py install) as root That's it, pivy is installed.

Windows

To be documented

Usage
To have Pivy access the FreeCAD scenegraph do the following:

You can now explore the FCSceneGraph with the dir command.

Documentation
Unfortunately documentation about pivy is still almost inexistant on the net. But you might find Coin documentation useful, since pivy simply translate Coin functions, nodes and methods in python, everything keeps the same name and properties, keeping in mind the difference of syntax between C and python:

http://www-evasion.imag.fr/~Francois.Faure/doc/inventorMentor/sgi_html/index.html - The Inventor Mentor - The "bible" of Inventor scene description language.