Difference between revisions of "Pivy"

From FreeCAD Documentation
Jump to navigation Jump to search
(However, since it is an accurate wrapper of the Coin3d library, you can read the C++ reference for information. In this case, you need to translate the C++ class naming style to Python style.)
(Marked this version for translation)
 
(35 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
<languages/>
 
<languages/>
 
<translate>
 
<translate>
 +
 
<!--T:23-->
 
<!--T:23-->
{{docnav|Scenegraph|PySide}}
+
{{Docnav
 +
|[[Scenegraph|Scenegraph]]
 +
|[[PySide|PySide]]
 +
}}
 +
 
 +
</translate>
 +
{{TOCright}}
 +
<translate>
 +
 
 +
==Introduction== <!--T:28-->
  
 
<!--T:1-->
 
<!--T:1-->
[https://bitbucket.org/Coin3D/pivy/src/default/ Pivy] is a python binding library for [https://bitbucket.org/Coin3D/coin/wiki/Home Coin3d], the 3D-rendering library used in FreeCAD. When imported in a running python interpreter, it allows to dialog directly with any running Coin3d [[Scenegraph|scenegraphs]], such as the FreeCAD 3D views, or even to create new ones. Pivy is bundled in standard FreeCAD installation.
+
[[Pivy|Pivy]] is a [[Python|Python]] binding library for [https://github.com/coin3d Coin], the 3D-rendering library used in FreeCAD to display things in a [[3D_view|3D view]]. Coin is an open source implementation of the "Open Inventor" specification to handle graphics. Therefore, in FreeCAD, the terms "Pivy", "Coin" or "Open Inventor" refer to the same thing essentially.
 +
 
 +
<!--T:32-->
 +
When imported in a running Python interpreter, Pivy allows us to communicate directly with any running Coin [[Scenegraph|scenegraph]], such as the [[3D_view|3D view]], 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 create shapes on screen, like [[Draft_Module|Draft]] and [[Arch_Module|Arch]]. Because of this, Pivy is normally installed when installing a distribution of FreeCAD.
  
 
<!--T:2-->
 
<!--T:2-->
The coin library is divided into several pieces, coin itself, for manipulating scenegraphs and bindings for several GUI systems, such as windows or, like in our case, 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:
+
The Coin library is divided into several pieces, Coin itself for manipulating scenegraphs, and bindings for several GUI systems, such as Windows and Qt. If present on the system, those modules are available to Pivy as well. 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, that is already done by FreeCAD. All we need to do is this:
 +
 
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
Line 14: Line 28:
 
}}
 
}}
 
<translate>
 
<translate>
==Accessing and modifying the scenegraph== <!--T:3-->
+
 
 +
==Scenegraph== <!--T:3-->
  
 
<!--T:4-->
 
<!--T:4-->
We saw in the [[Scenegraph]] page how a typical Coin scene is organized. Everything that appears in a FreeCAD 3D view is a coin scenegraph, organized the same way. We have one root node, and all objects on the screen are its children.
+
We saw on the [[Scenegraph]] page how a typical Coin scene is organized. Everything that appears in a [[3D_view|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.
  
 
<!--T:5-->
 
<!--T:5-->
 
FreeCAD has an easy way to access the root node of a 3D view scenegraph:
 
FreeCAD has an easy way to access the root node of a 3D view scenegraph:
 +
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
 
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
 
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
print sg
+
print(sg)
 
}}
 
}}
 
<translate>
 
<translate>
 +
 
<!--T:6-->
 
<!--T:6-->
 
This will return the root node:
 
This will return the root node:
 +
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
Line 34: Line 52:
 
}}
 
}}
 
<translate>
 
<translate>
 +
 
<!--T:7-->
 
<!--T:7-->
 
We can inspect the immediate children of our scene:
 
We can inspect the immediate children of our scene:
 +
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
 
for node in sg.getChildren():
 
for node in sg.getChildren():
     print node
+
     print(node)
 
}}
 
}}
 
<translate>
 
<translate>
 +
 
<!--T:8-->
 
<!--T:8-->
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 [https://coin3d.bitbucket.io/Coin/annotated.html official coin documentation].
+
Some of those nodes, such as {{incode|SoSeparator}} or {{incode|SoGroup}} nodes, can have children themselves. The complete list of the available Coin objects can be found in the official Coin documentation.
  
 
<!--T:9-->
 
<!--T:9-->
 
Let's try to add something to our scenegraph now. We'll add a nice red cube:
 
Let's try to add something to our scenegraph now. We'll add a nice red cube:
 +
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
 
col = coin.SoBaseColor()
 
col = coin.SoBaseColor()
col.rgb=(1,0,0)
+
col.rgb = (1, 0, 0)
 
cub = coin.SoCube()
 
cub = coin.SoCube()
 
myCustomNode = coin.SoSeparator()
 
myCustomNode = coin.SoSeparator()
Line 58: Line 80:
 
}}
 
}}
 
<translate>
 
<translate>
 +
 
<!--T:10-->
 
<!--T:10-->
and here is our (nice) red cube. Now, let's try this:
+
Now, let's try this:
 +
 
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
col.rgb=(1,1,0)
+
col.rgb = (1, 1, 0)
 
}}
 
}}
 
<translate>
 
<translate>
 +
 
<!--T:11-->
 
<!--T:11-->
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 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.
  
 
<!--T:12-->
 
<!--T:12-->
A key to work with scenegraphs in your scripts is to be able to access certain properties of the nodes you added when needed. For example, if we wanted to move our cube, we would have added a SoTranslation node to our custom node, and it would have looked like this:
+
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 {{incode|SoTranslation}} node {{Emphasis|before}} the cube:
 +
 
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
 
col = coin.SoBaseColor()
 
col = coin.SoBaseColor()
col.rgb=(1,0,0)
+
col.rgb = (1, 0, 0)
 
trans = coin.SoTranslation()
 
trans = coin.SoTranslation()
trans.translation.setValue([0,0,0])
+
trans.translation.setValue([0, 0, 0])
 
cub = coin.SoCube()
 
cub = coin.SoCube()
 
myCustomNode = coin.SoSeparator()
 
myCustomNode = coin.SoSeparator()
 
myCustomNode.addChild(col)
 
myCustomNode.addChild(col)
mtCustomNode.addChild(trans)
+
myCustomNode.addChild(trans)
 
myCustomNode.addChild(cub)
 
myCustomNode.addChild(cub)
 
sg.addChild(myCustomNode)
 
sg.addChild(myCustomNode)
 
}}
 
}}
 
<translate>
 
<translate>
 +
 
<!--T:13-->
 
<!--T:13-->
Remember that in an openInventor scenegraph, the order is important. A node affects what comes next, so you can say something like: color red, cube, color yellow, sphere, and you will get a red cube and a yellow sphere. If we added the translation now to our existing custom node, it would come after the cube, and not affect it. If we had inserted it when creating it, like here above, we could now do:
+
To move our cube we can now do:
 +
 
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
trans.translation.setValue([2,0,0])
+
trans.translation.setValue([2, 0, 0])
 
}}
 
}}
 
<translate>
 
<translate>
 +
 
<!--T:14-->
 
<!--T:14-->
And our cube would jump 2 units to the right.
 
 
Finally, removing something is done with:
 
Finally, removing something is done with:
 +
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
Line 99: Line 128:
 
}}
 
}}
 
<translate>
 
<translate>
==Using callback mechanisms== <!--T:15-->
+
 
 +
<!--T:29-->
 +
[[#top|top]]
 +
 
 +
==Callbacks== <!--T:15-->
  
 
<!--T:16-->
 
<!--T:16-->
A [http://en.wikipedia.org/wiki/Callback_%28computer_science%29 callback mechanism] is a system that permits a library that you are using, such as our coin library, to call you back, that is, to call a certain function from your currently running python object. This is extremely useful, because that way coin can notify you if some specific event occurs in the scene. Coin can watch very different things, such as mouse position, clicks of a mouse button, keyboard keys being pressed, and many other things.
+
A [http://en.wikipedia.org/wiki/Callback_%28computer_science%29 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, mouse button clicks, keyboard keys being pressed, and many more.
  
 
<!--T:17-->
 
<!--T:17-->
 
FreeCAD features an easy way to use such callbacks:
 
FreeCAD features an easy way to use such callbacks:
 +
 
</translate>
 
</translate>
 
{{Code|code=
 
{{Code|code=
 +
from pivy import coin
 +
 
class ButtonTest:
 
class ButtonTest:
  def __init__(self):
+
    def __init__(self):
    self.view = FreeCADGui.ActiveDocument.ActiveView
+
        self.view = FreeCADGui.ActiveDocument.ActiveView
    self.callback = self.view.addEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.getMouseClick)  
+
        self.callback = self.view.addEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(), self.getMouseClick)  
  def getMouseClick(self,event_cb):
+
 
    event = event_cb.getEvent()
+
    def getMouseClick(self, event_cb):
    if event.getState() == SoMouseButtonEvent.DOWN:
+
        event = event_cb.getEvent()
      print "Alert!!! A mouse button has been improperly clicked!!!"
+
        if event.getState() == coin.SoMouseButtonEvent.DOWN:
      self.view.removeEventCallbackSWIG(SoMouseButtonEvent.getClassTypeId(),self.callback)  
+
            print("Alert!!! A mouse button has been improperly clicked!!!")
+
            self.view.removeEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(), self.callback)
 +
 
 
ButtonTest()
 
ButtonTest()
 
}}
 
}}
 
<translate>
 
<translate>
 +
 
<!--T:18-->
 
<!--T:18-->
The callback has to be initiated from an object, because that object must still be running when the callback will occur.
+
The callback has to be initiated from an object, because that object must still be running when the callback occurs.
See also a [[Code_snippets#Observing_mouse_events_in_the_3D_viewer_via_Python|complete list]] of possible events and their parameters, or the [https://coin3d.bitbucket.io/Coin/index.html official coin documentation].
+
See also a [[Code_snippets#Observing_mouse_events_in_the_3D_viewer_via_Python|complete list]] of possible events and their parameters, or the official Coin documentation.
  
== Documentation == <!--T:19-->  
+
<!--T:30-->
 +
[[#top|top]]
 +
 
 +
==Documentation== <!--T:19-->  
  
 
<!--T:20-->
 
<!--T:20-->
Unfortunately, Pivy itself doesn't have its own documentation. However, since it is an accurate wrapper of the Coin3d library, you can read the C++ reference for information. In this case, you need to translate the C++ class naming style to Python style.
+
Unfortunately, Pivy 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.
  
 +
<!--T:25-->
 
In C++:
 
In C++:
 +
 +
</translate>
 
{{Code|code=
 
{{Code|code=
 
SoFile::getClassTypeId()
 
SoFile::getClassTypeId()
 
}}
 
}}
 +
<translate>
 +
 +
<!--T:26-->
 +
In Pivy:
  
In Pivy
+
</translate>
 
{{Code|code=
 
{{Code|code=
 
SoFile.getClassId()
 
SoFile.getClassId()
 
}}
 
}}
 +
<translate>
  
<!--T:21-->
+
<!--T:27-->
{{docnav|Scenegraph|PySide}}
+
* [https://github.com/coin3d Coin3D] homepage.
 +
* [https://github.com/coin3d/pivy Pivy] homepage.
 +
* [https://grey.colorado.edu/coin3d/index.html Coin3D Documentation], at University of Colorado.
 +
* [https://coin3d.bitbucket.io/Coin/index.html Coin3D Documentation], at BitBucket.
 +
* [https://mevislabdownloads.mevis.de/docs/current/MeVis/ThirdParty/Documentation/Publish/OpenInventorReference/index.html Open Inventor Reference Documentation], by MeVisLab.
  
<!--T:24-->
+
<!--T:31-->
{{Userdocnavi}}
+
[[#top|top]]
  
<!--T:22-->
+
<!--T:21-->
[[Category:Poweruser Documentation]]
+
{{Docnav
 +
|[[Scenegraph|Scenegraph]]
 +
|[[PySide|PySide]]
 +
}}
  
 
</translate>
 
</translate>
 +
{{Powerdocnavi{{#translation:}}}}
 +
[[Category:Developer Documentation{{#translation:}}]]
 +
[[Category:Python Code{{#translation:}}]]
 
{{clear}}
 
{{clear}}

Latest revision as of 21:15, 21 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

Introduction

Pivy is a Python binding library for Coin, the 3D-rendering library used in FreeCAD to display things in a 3D view. Coin is an open source implementation of the "Open Inventor" specification to handle graphics. Therefore, in FreeCAD, the terms "Pivy", "Coin" or "Open Inventor" refer to the same thing essentially.

When imported in a running Python interpreter, Pivy allows us to communicate directly with any running Coin scenegraph, such as the 3D view, 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 create 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 and Qt. If present on the system, those modules are available to Pivy as well. 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, that is already done by FreeCAD. All we need to do is this:

from pivy import coin

Scenegraph

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()
print(sg)

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():
    print(node)

Some of those nodes, such as SoSeparator or SoGroup nodes, 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()
myCustomNode.addChild(col)
myCustomNode.addChild(cub)
sg.addChild(myCustomNode)

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()
col.rgb = (1, 0, 0)
trans = coin.SoTranslation()
trans.translation.setValue([0, 0, 0])
cub = coin.SoCube()
myCustomNode = coin.SoSeparator()
myCustomNode.addChild(col)
myCustomNode.addChild(trans)
myCustomNode.addChild(cub)
sg.addChild(myCustomNode)

To move our cube we can now do:

trans.translation.setValue([2, 0, 0])

Finally, removing something is done with:

sg.removeChild(myCustomNode)

top

Callbacks

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, mouse button clicks, keyboard keys being pressed, and many more.

FreeCAD features an easy way to use such callbacks:

from pivy import 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)

ButtonTest()

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.

top

Documentation

Unfortunately, Pivy 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++:

SoFile::getClassTypeId()

In Pivy:

SoFile.getClassId()

top

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