Difference between revisions of "Pivy/sv"

From FreeCAD Documentation
Jump to navigation Jump to search
(Created page with "[http://pivy.coin3d.org/ Pivy] är ett python bindnings bibliotek för [http://www.coin3d.org Coin3d], det 3D-renderingsbibliotek som används av FreeCAD. när det importeras ...")
(Created page with "Coin biblioteket är uppdelat i flera delar, själva coin, för manipulation av scengrafer och bindningar för flera GUI system, som windows, eller som i vårt fall, qt. Dessa...")
Line 1: Line 1:
 
[http://pivy.coin3d.org/ Pivy] är ett python bindnings bibliotek för [http://www.coin3d.org Coin3d], det 3D-renderingsbibliotek som används av FreeCAD. när det importeras i en körande python tolk, så kan du föra en direkt dialog med alla körande [[Scenegraph/sv|scengrafer]], som FreeCAD's 3D vyer, eller att även skapa nya. Pivy följer med i en standard FreeCAD installation.
 
[http://pivy.coin3d.org/ Pivy] är ett python bindnings bibliotek för [http://www.coin3d.org Coin3d], det 3D-renderingsbibliotek som används av FreeCAD. när det importeras i en körande python tolk, så kan du föra en direkt dialog med alla körande [[Scenegraph/sv|scengrafer]], som FreeCAD's 3D vyer, eller att även skapa nya. Pivy följer med i en standard FreeCAD installation.
  
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:
+
Coin biblioteket är uppdelat i flera delar, själva coin, för manipulation av scengrafer och bindningar för flera GUI system, som windows, eller som i vårt fall, qt. Dessa moduler är även tillgängliga för pivy, beroende på om de finns i systemet. Coin modulen finns alltid, och det är vad vi kommer att använda i alla fall, eftersom vi inte behöver bry oss om att förankra vårt 3D fönster i något gränssnitt, det görs redan av FreeCAD själv. Allt vi behöver göra är detta:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
  from pivy import coin
 
  from pivy import coin

Revision as of 21:08, 4 October 2014

Pivy är ett python bindnings bibliotek för Coin3d, det 3D-renderingsbibliotek som används av FreeCAD. när det importeras i en körande python tolk, så kan du föra en direkt dialog med alla körande scengrafer, som FreeCAD's 3D vyer, eller att även skapa nya. Pivy följer med i en standard FreeCAD installation.

Coin biblioteket är uppdelat i flera delar, själva coin, för manipulation av scengrafer och bindningar för flera GUI system, som windows, eller som i vårt fall, qt. Dessa moduler är även tillgängliga för pivy, beroende på om de finns i systemet. Coin modulen finns alltid, och det är vad vi kommer att använda i alla fall, eftersom vi inte behöver bry oss om att förankra vårt 3D fönster i något gränssnitt, det görs redan av FreeCAD själv. Allt vi behöver göra är detta:

 from pivy import coin

Accessing and modifying the scenegraph

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.

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 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()
 myCustomNode.addChild(col)
 myCustomNode.addChild(cub)
 sg.addChild(myCustomNode)

and here is our (nice) red cube. Now, let's try this:

 col.rgb=(1,1,0)

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.

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:

 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)
 mtCustomNode.addChild(trans)
 myCustomNode.addChild(cub)
 sg.addChild(myCustomNode)

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:

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

And our cube would jump 2 units to the right. Finally, removing something is done with:

 sg.removeChild(myCustomNode)

Using callback mechanisms

A 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.

FreeCAD features an easy way to use such callbacks:

 class ButtonTest:
   def __init__(self):
     self.view = FreeCADGui.ActiveDocument.ActiveView
     self.callback = self.view.addEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.getMouseClick) 
   def getMouseClick(self,event_cb):
     event = event_cb.getEvent()
     if event.getState() == SoMouseButtonEvent.DOWN:
       print "Alert!!! A mouse button has been improperly clicked!!!"
       self.view.removeEventCallbackSWIG(SoMouseButtonEvent.getClassTypeId(),self.callback) 
 
 ButtonTest()

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

Documentation

Unfortunately pivy itself still doesn't have a proper documentation, but since it is an accurate translation of coin, you can safely use the coin documentation as reference, and use python style instead of c++ style (for example SoFile::getClassTypeId() would in pivy be SoFile.getClassId())

Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Türkçe • ‎español • ‎français • ‎italiano • ‎polski • ‎română • ‎svenska • ‎čeština • ‎русский • ‎日本語