Pivy/ro

Pivy este o bilbiotecă de coduri care servește drept pasarelă între python și coin Coin3d, bilbioteca de randare 3D utilizată de către FreeCAD. Atunci când este importat într-un interpretor python care rulează, Pivy permite dialogul direct și imediat cu orice procedură Coin3d care rulează scenegraphs, ca dexempălu vederile FreeCAD 3D, sau chiar creare unor anoi. Pivy este inclus în instalarea standard FreeCAD.

Biblioteca coin este împărțită în mai multe piese, coin însuși, este pentru manipularea scripturilor și legăturilor pentru mai multe sisteme GUI, cum ar fi ferestrele sau, ca în cazul nostru, qt. Modulele respective sunt disponibile și pentru piv, în funcție de situația în care sunt prezente în sistem. Modulul coin este întotdeauna prezent și este ceea ce vom folosi oricum, deoarece nu va trebui să ne pese de ancorarea afișajului nostru 3D în orice interfață, este deja făcut de FreeCAD. Tot ce trebuie să facem este:

Accederea și modificarea unui scene grafioe 3D
Am văzut în pagina Scenegraph cum Coin organizează o scenă tipică. Tot ce este afișat 3D în FreeCAD este construit și festionat de către Coin. Avem o rădăcină, iar toate obiectele de pe ecran sunt copiii lui.legat prin noduri

Free cad are un mod facil de a accede la radacina unei scene 3D:

This will return the root node:

We can inspect the immediate children of our scene:

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:

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

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:

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:

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

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:

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.

Documentație
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)