Pivy/pl: Difference between revisions

From FreeCAD Documentation
(Created page with "==Scenograf==")
(Created page with "Na stronie Scenogram zobaczyliśmy, jak zorganizowana jest typowa scena Coin. Wszystko, co pojawia się w oknie widoku 3D jest scenegrafem Coi...")
Line 22: Line 22:
==Scenograf==
==Scenograf==


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.
Na stronie [[Scenegraph/pl|Scenogram]] zobaczyliśmy, jak zorganizowana jest typowa scena Coin. Wszystko, co pojawia się w oknie [[3D_view/pl|widoku 3D]] jest scenegrafem Coin, zorganizowanym w ten sam sposób. Mamy jeden węzeł główny, a wszystkie obiekty na ekranie jego produktami pochodnymi.


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:

Revision as of 16:09, 28 December 2022

Wprowadzenie

Pivy jest biblioteką wiążącą Python do Coin, biblioteką renderowania 3D używaną w FreeCAD do wyświetlania obiektów w oknie widoku 3D. Coin jest otwartą implementacją specyfikacji "Open Inventor" do obsługi grafiki. Dlatego w środowisku FreeCAD terminy Pivy, Coin lub Open Inventor odnoszą się zasadniczo do tego samego.

Po zaimportowaniu do działającego interpretera Python, Pivy pozwala nam na bezpośrednią komunikację z dowolnym działającym scenegrafem Coin, takim jak widok 3D, a nawet na tworzenie nowych. Pivy nie jest wymagany do kompilacji FreeCAD, ale jest wymagany podczas uruchamiania opartych na Pythonie środowisk pracy, które tworzą kształty na ekranie, takich jak Rysunek Roboczy i Architektura. Z tego powodu, Pivy jest zwykle instalowany podczas instalacji programu FreeCAD.

Biblioteka Coin jest podzielona na kilka części, sam Coin do manipulowania scenegrafami oraz wiązania dla kilku systemów GUI, takich jak Windows i Qt. Jeśli są obecne w systemie, moduły te są również dostępne dla Pivy. Moduł Coin jest zawsze obecny i to właśnie z niego będziemy korzystać, ponieważ nie będziemy musieli dbać o zakotwiczenie naszego wyświetlacza 3D w jakimkolwiek interfejsie, to już robi FreeCAD. Jedyne co musimy zrobić to:

from pivy import coin

Scenograf

Na stronie Scenogram zobaczyliśmy, jak zorganizowana jest typowa scena Coin. Wszystko, co pojawia się w oknie widoku 3D jest scenegrafem Coin, zorganizowanym w ten sam sposób. Mamy jeden węzeł główny, a wszystkie obiekty na ekranie są jego produktami pochodnymi.

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)

Przewiń na górę strony

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.

Przewiń na górę strony

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

Older

These links provide reference documentation for Coin v3.x. The differences with v4.x are minimal, so they may still be useful.

Przewiń na górę strony