Extra python modules

From FreeCAD Documentation
Revision as of 18:50, 3 January 2008 by Yorik (talk | contribs)

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:

import PyQt4

To access the FreeCAD interface, type:

from PyQt4 import QtCore,QtGui app = QtGui.qApp FreeCADWindow = app.activeWindow()

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

FreeCADWindow.addDockWidget(QtCore.Qt.RghtDockWidgetArea,my_custom_widget)

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.

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

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. I recommend you to use SWIG in the version 1.3.25 because with the latest version I had problems to use pivy. A source tarball can be downloaded from http://www.swig.org. Then unpack the sources and do

./configure
make
make install (or checkinstall if you prefer) 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

Windows

To be documented

Usage

To combine FreeCAD and pivy do the following:

from pivy.coin import * App.newDocument() # Open a document and a view view = Gui.ActiveDocument.ActiveView root = view.getSceneGraph() # returns a pivy Python object that holds an internal SoSeparator -- the root node of the scene root.addChild(SoCube()) # add a box to scene

Template:Devdocnavi