Difference between revisions of "Pivy"

From FreeCAD Documentation
Jump to navigation Jump to search
Line 181: Line 181:
* [https://github.com/coin3d Coin3d] homepage
* [https://github.com/coin3d Coin3D] homepage.
* [https://github.com/coin3d/pivy Pivy] homepage
* [https://github.com/coin3d/pivy Pivy] homepage.
* [https://grey.colorado.edu/coin3d/index.html Coin Documentation], at University of Colorado
* [https://grey.colorado.edu/coin3d/index.html Coin Documentation], at University of Colorado.
* [https://coin3d.bitbucket.io/Coin/index.html Coin Documentation], at BitBucket
* [https://coin3d.bitbucket.io/Coin/index.html Coin Documentation], at BitBucket.

Revision as of 10:30, 2 June 2020

Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Türkçe • ‎español • ‎français • ‎italiano • ‎polski • ‎română • ‎svenska • ‎čeština • ‎русский • ‎日本語
Arrow-left.svg Previous: Scenegraph
Next: PySide Arrow-right.svg


Pivy is a Python binding library for Coin3D, the 3D-rendering library used in FreeCAD to display things in a 3D view. When imported in a running Python interpreter, Pivy allows us to communicate directly with any running Coin scenegraph, such as FreeCAD 3D views, or even to create new ones. Pivy is not required to compile FreeCAD, but it is required at runtime when running Python-based workbenches that creates shapes on screen, like Draft and Arch. Because of this, Pivy is normally installed when installing a distribution of FreeCAD.

The Coin library is divided into several pieces, Coin itself for manipulating scenegraphs, and bindings for several GUI systems, such as Windows or Qt. Those modules are available to Pivy too, depending if they are present on the system. The Coin module is always present, and it is what we will use anyway, since we won't need to care about anchoring our 3D display in any interface, it is already done by FreeCAD itself. All we need to do is this:

from pivy import coin


We saw on the Scenegraph page how a typical Coin scene is organized. Everything that appears in a 3D view is a Coin scenegraph, organized in the same way. We have one root node, and all objects on the screen are its children.

FreeCAD has an easy way to access the root node of a 3D view scenegraph:

sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()

This will return the root node:

<pivy.coin.SoSelection; proxy of <Swig Object of type 'SoSelection *' at 0x360cb60> >

We can inspect the immediate children of our scene:

for node in sg.getChildren():

Some of those nodes, such as SoSeparators or SoGroups, can have children themselves. The complete list of the available Coin objects can be found in the official Coin documentation.

Let's try to add something to our scenegraph now. We'll add a nice red cube:

col = coin.SoBaseColor()
col.rgb = (1,0,0)
cub = coin.SoCube()
myCustomNode = coin.SoSeparator()

Now, let's try this:

col.rgb = (1, 1, 0)

As you can see everything is still accessible and modifiable on-the-fly. No need to recompute or redraw anything, Coin takes care of everything. You can add stuff to your scenegraph, change properties, hide stuff, show temporary objects, anything. Of course, this only concerns the display in the 3D view. That display gets recomputed by FreeCAD on file open, and when an object needs recomputing. So, if you change the aspect of an existing FreeCAD object, those changes will be lost if the object gets recomputed or when you reopen the file.

As already mentioned, in an openInventor scenegraph the order is important. A node affects what comes next. For example, if we want to have the ability to move our cube we will need to add a SoTranslation node before the cube:

col = coin.SoBaseColor()
trans = coin.SoTranslation()
cub = coin.SoCube()
myCustomNode = coin.SoSeparator()

To move our cube we can now do:


Finally, removing something is done with:



A callback mechanism is a system that permits a library, such as our Coin library, to call you back, that is, to call a certain function from your currently running Python object. That way Coin can notify you that some specific event occurred in the scene. Coin can watch very different things, such as mouse position, clicks of a mouse button, keyboard keys being pressed, and many more.

FreeCAD features an easy way to use such callbacks:

import pivy.coin as coin

class ButtonTest:
    def __init__(self):
        self.view = FreeCADGui.ActiveDocument.ActiveView
        self.callback = self.view.addEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(), self.getMouseClick) 

    def getMouseClick(self,event_cb):
        event = event_cb.getEvent()
        if event.getState() == coin.SoMouseButtonEvent.DOWN:
            print("Alert!!! A mouse button has been improperly clicked!!!")
            self.view.removeEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(), self.callback)


The callback has to be initiated from an object, because that object must still be running when the callback occurs. See also a complete list of possible events and their parameters, or the official Coin documentation.


Unfortunately, Pivy itself doesn't have its own documentation. However, since it is an accurate wrapper of the Coin library, you can read the C++ reference for information. In this case, you need to translate the C++ class naming style to Python style.

In C++:


In Pivy:

Arrow-left.svg Previous: Scenegraph
Next: PySide Arrow-right.svg