Pivy/ja: Difference between revisions

From FreeCAD Documentation
(Created page with "スクリプトでシーングラフを操作する場合の鍵となるのは必要な際に追加したノードの特定のプロパティにアクセスできるという...")
(Updating to match new version of source page)
 
(38 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages/>
<languages/>

{{Docnav
|[[Scenegraph|Scenegraph]]
|[[PySide|PySide]]
}}

{{TOCright}}

==Introduction==

<div class="mw-translate-fuzzy">
[http://pivy.coin3d.org/ Pivy]はFreeCADで使われている3Dレンダリングライブラリである[http://www.coin3d.org Coin3d]用のPythonのバインディングライブラリです。実行中のPythonインタプリタにインポートするとFreeCADの3DビューアーなどのCoin3d[[Scenegraph/jp|シーングラフ]]と直に対話処理を行ったり、新しい3Dビューアーを作成することさえできます。Pivyは標準のFreeCADインストールに同梱されています。
[http://pivy.coin3d.org/ Pivy]はFreeCADで使われている3Dレンダリングライブラリである[http://www.coin3d.org Coin3d]用のPythonのバインディングライブラリです。実行中のPythonインタプリタにインポートするとFreeCADの3DビューアーなどのCoin3d[[Scenegraph/jp|シーングラフ]]と直に対話処理を行ったり、新しい3Dビューアーを作成することさえできます。Pivyは標準のFreeCADインストールに同梱されています。
</div>


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_Workbench|Draft]] and [[Arch_Workbench|Arch]]. Because of this, Pivy is normally installed when installing a distribution of FreeCAD.

<div class="mw-translate-fuzzy">
Coinライブラリはいくつかに分割されています。Coin自体はシーングラフの操作を担い、Windowsといった複数のGUIシステム用のパッケージがありますが私たちの場合はQt用になります。これらの分割されたモジュールもシステム上に存在する場合にはPivyから利用することができます。Coinモジュールは常駐していて自由に利用することができます。既にFreeCADによって行われているので3D表示をどのインターフェイスに紐付けするのか気にする必要はありません。私たちがやらなければならないのは以下のコマンドを実行することだけです:
Coinライブラリはいくつかに分割されています。Coin自体はシーングラフの操作を担い、Windowsといった複数のGUIシステム用のパッケージがありますが私たちの場合はQt用になります。これらの分割されたモジュールもシステム上に存在する場合にはPivyから利用することができます。Coinモジュールは常駐していて自由に利用することができます。既にFreeCADによって行われているので3D表示をどのインターフェイスに紐付けするのか気にする必要はありません。私たちがやらなければならないのは以下のコマンドを実行することだけです:
</div>


from pivy import coin
{{Code|code=
{{Code|code=
from pivy import coin
from pivy import coin
}}
}}

<div class="mw-translate-fuzzy">
==シーングラフへのアクセスと変更==
==シーングラフへのアクセスと変更==
</div>


<div class="mw-translate-fuzzy">
典型的なCoinシーンがどの様に構成されているかは[[Scenegraph/jp|シーングラフ]]ページで見ました。FreeCADの3Dビューに表示されているものは全てCoinシーングラフであり、同じやり方で構成されています。ルートのノードが一つあり、画面上のオブジェクトは全てその子ノードです。
典型的なCoinシーンがどの様に構成されているかは[[Scenegraph/jp|シーングラフ]]ページで見ました。FreeCADの3Dビューに表示されているものは全てCoinシーングラフであり、同じやり方で構成されています。ルートのノードが一つあり、画面上のオブジェクトは全てその子ノードです。
</div>


FreeCADでは3Dビューシーングラフのルートノードに簡単にアクセスすることができます:
FreeCADでは3Dビューシーングラフのルートノードに簡単にアクセスすることができます:


sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
print sg
{{Code|code=
{{Code|code=
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
print sg
print(sg)
}}
}}

こうするとルートノードが返されます。
こうするとルートノードが返されます。


<pivy.coin.SoSelection; proxy of <Swig Object of type 'SoSelection *' at 0x360cb60> >
{{Code|code=
{{Code|code=
<pivy.coin.SoSelection; proxy of <Swig Object of type 'SoSelection *' at 0x360cb60> >
<pivy.coin.SoSelection; proxy of <Swig Object of type 'SoSelection *' at 0x360cb60> >
}}
}}

シーンの直下の子ノードを調べることもできます。
シーンの直下の子ノードを調べることもできます。


for node in sg.getChildren():
print node
{{Code|code=
{{Code|code=
for node in sg.getChildren():
for node in sg.getChildren():
print node
print(node)
}}
}}

<div class="mw-translate-fuzzy">
SoSeparatorsやSoGroupsといったこれらノードは自身の子ノードを持ちます。利用可能なCoinオブジェクトの全リストは[http://doc.coin3d.org/Coin/classes.html Coin公式ドキュメント]にあります。
SoSeparatorsやSoGroupsといったこれらノードは自身の子ノードを持ちます。利用可能なCoinオブジェクトの全リストは[http://doc.coin3d.org/Coin/classes.html Coin公式ドキュメント]にあります。
</div>


今度はシーングラフに何か追加してみましょう。すてきな赤い立方体を追加してみることにしましょう:
今度はシーングラフに何か追加してみましょう。すてきな赤い立方体を追加してみることにしましょう:


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

(すてきな)赤い立方体ができました。以下を試してみてください:

col.rgb=(1,1,0)

Let's try to add something to our scenegraph now. We'll add a nice red cube:
{{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 60: Line 67:
sg.addChild(myCustomNode)
sg.addChild(myCustomNode)
}}
}}

and here is our (nice) red cube. Now, let's try this:
<div class="mw-translate-fuzzy">
(すてきな)赤い立方体ができました。以下を試してみてください:
</div>

{{Code|code=
{{Code|code=
col.rgb=(1,1,0)
col.rgb = (1, 1, 0)
}}
}}

<div class="mw-translate-fuzzy">
わかったでしょうか?実行中であっても全てのものに対してアクセスと変更を行うことができるのです。再計算や再描画は必要ありません。Coinが全て面倒を見てくれます。物体のシーングラフへの追加、プロパティの変更、物体の非表示、一時的なオブジェクトの表示、何でもできます。もちろん3Dビューに関係したことだけですが。この表示はFreeCADがファイルを開いた時とオブジェクトが必要とした時に再計算されます。従って既存のFreeCADオブジェクトの外見を変更した場合、その変更はオブジェクトが再計算されるかファイルを再度開いた瞬間に失われます。
わかったでしょうか?実行中であっても全てのものに対してアクセスと変更を行うことができるのです。再計算や再描画は必要ありません。Coinが全て面倒を見てくれます。物体のシーングラフへの追加、プロパティの変更、物体の非表示、一時的なオブジェクトの表示、何でもできます。もちろん3Dビューに関係したことだけですが。この表示はFreeCADがファイルを開いた時とオブジェクトが必要とした時に再計算されます。従って既存のFreeCADオブジェクトの外見を変更した場合、その変更はオブジェクトが再計算されるかファイルを再度開いた瞬間に失われます。
</div>


<div class="mw-translate-fuzzy">
スクリプトでシーングラフを操作する場合の鍵となるのは必要な際に追加したノードの特定のプロパティにアクセスできるということなのです。例えば立方体を動かしたいとしましょう。自作のノードにSoTranslationノードを追加することになるでしょう。ちょうどこんな具合です:
スクリプトでシーングラフを操作する場合の鍵となるのは必要な際に追加したノードの特定のプロパティにアクセスできるということなのです。例えば立方体を動かしたいとしましょう。自作のノードにSoTranslationノードを追加することになるでしょう。ちょうどこんな具合です:
</div>


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)
{{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)
}}
}}

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:
<div class="mw-translate-fuzzy">
OpenInventorのシーングラフではその順番が重要であるということを思い出してください。ノードは以降のノードに影響を与えます。color red、cube、color yellow、sphere とすれば赤い立方体と黄色い球が表示されます。もし今、既存の自作ノードに移動を追加すると移動は立方体の後になり、立方体に影響を与えません。上記のように作成時に挿入しておけば次のようにできます:
</div>

{{Code|code=
{{Code|code=
trans.translation.setValue([2,0,0])
trans.translation.setValue([2, 0, 0])
}}
}}

And our cube would jump 2 units to the right.
<div class="mw-translate-fuzzy">
Finally, removing something is done with:
こうすると立方体が2単位分、右に移動します。
最後に何かを取り除くには次のようにします:
</div>

{{Code|code=
{{Code|code=
sg.removeChild(myCustomNode)
sg.removeChild(myCustomNode)
}}
}}
{{Top}}
==Using callback mechanisms==
<div class="mw-translate-fuzzy">
==コールバック機能の使用==
</div>


<div class="mw-translate-fuzzy">
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.
[http://en.wikipedia.org/wiki/Callback_%28computer_science%29 コールバック機能]とはCoinライブラリの様な現在使用しているライブラリがコールバック、つまり現在実行中のPythonオブジェクトから特定の関数を呼び出すことを可能にするシステムのことです。これは非常に便利です。これを使うとシーンで何か特定のイベントが起きた場合にCoinからあなたへ通知することができるのです。Coinでは色々なものを監視することができます。マウスの位置、マウスボタンのクリック、キーボードのキーが押されたかどうか、まだまだ他にもあります。
</div>

FreeCADではそういったコールバックを簡単に使うための機能があります:


FreeCAD features an easy way to use such callbacks:
{{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()
if event.getState() == coin.SoMouseButtonEvent.DOWN:
print "Alert!!! A mouse button has been improperly clicked!!!"
print("Alert!!! A mouse button has been improperly clicked!!!")
self.view.removeEventCallbackSWIG(SoMouseButtonEvent.getClassTypeId(),self.callback)
self.view.removeEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(), self.callback)

ButtonTest()
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 [[Code_snippets#Observing_mouse_events_in_the_3D_viewer_via_Python|complete list]] of possible events and their parameters, or the [http://doc.coin3d.org/Coin/classes.html official coin documentation].


<div class="mw-translate-fuzzy">
== Documentation ==
コールバックが起きた時にもオブジェクトは実行され続けなければならないのでコールバックはオブジェクトで初期化されなけれればなりません。
利用可能なイベントとパラメータの[[Code_snippets#Observing_mouse_events_in_the_3D_viewer_via_Python|全リスト]]または[http://doc.coin3d.org/Coin/classes.html Coin公式ドキュメント]を参照してください。
</div>
{{Top}}
<div class="mw-translate-fuzzy">
== ドキュメント ==
</div>


<div class="mw-translate-fuzzy">
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())
残念ながらPivy自体にはちゃんとしたドキュメントがまだありません。しかしPivyはCoinと正確に対応するのでC++スタイルをPythonスタイルに読み替えれば(例えばSoFile::getClassTypeId()はPivyではSoFile.getClassId()となるでしょう)Coinドキュメントをリファレンスとして安全に使用できます。
</div>


In C++:
{{docnav|Scenegraph|PySide}}


{{Code|code=
[[Category:Poweruser Documentation]]
SoFile::getClassTypeId()
}}

In Pivy:

{{Code|code=
SoFile.getClassId()
}}

* [https://github.com/coin3d Coin3D] homepage.
* [https://github.com/coin3d/pivy Pivy] homepage.
* [https://github.com/coin3d/coin/wiki Coin3D wiki], at GitHub.
* [https://github.com/coin3d/coin/wiki/Documentation Coin3D wiki documentation], at GitHub.
* [https://coin3d.github.io/Coin/html/ Coin3D Documentation], latest automatically generated Doxygen documentation.
* [https://webdocs.cs.ualberta.ca/~graphics/books/mentor.pdf (Open)Inventor Mentor] - recommended.

=== Older ===

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

* [https://coin3d.bitbucket.io/Coin/index.html Coin3D Documentation], at BitBucket.
* [https://grey.colorado.edu/coin3d/index.html Coin3D Documentation], at University of Colorado.
* [https://mevislabdownloads.mevis.de/docs/current/MeVis/ThirdParty/Documentation/Publish/OpenInventorReference/index.html Open Inventor Reference Documentation], by MeVisLab.
{{Top}}

<div class="mw-translate-fuzzy">
{{docnav|Scenegraph|PySide}}
</div>


{{Powerdocnavi{{#translation:}}}}
{{clear}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]

Latest revision as of 21:15, 2 June 2022

Introduction

PivyはFreeCADで使われている3DレンダリングライブラリであるCoin3d用のPythonのバインディングライブラリです。実行中のPythonインタプリタにインポートするとFreeCADの3DビューアーなどのCoin3dシーングラフと直に対話処理を行ったり、新しい3Dビューアーを作成することさえできます。Pivyは標準のFreeCADインストールに同梱されています。

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.

Coinライブラリはいくつかに分割されています。Coin自体はシーングラフの操作を担い、Windowsといった複数のGUIシステム用のパッケージがありますが私たちの場合はQt用になります。これらの分割されたモジュールもシステム上に存在する場合にはPivyから利用することができます。Coinモジュールは常駐していて自由に利用することができます。既にFreeCADによって行われているので3D表示をどのインターフェイスに紐付けするのか気にする必要はありません。私たちがやらなければならないのは以下のコマンドを実行することだけです:

from pivy import coin

シーングラフへのアクセスと変更

典型的なCoinシーンがどの様に構成されているかはシーングラフページで見ました。FreeCADの3Dビューに表示されているものは全てCoinシーングラフであり、同じやり方で構成されています。ルートのノードが一つあり、画面上のオブジェクトは全てその子ノードです。

FreeCADでは3Dビューシーングラフのルートノードに簡単にアクセスすることができます:

sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
print(sg)

こうするとルートノードが返されます。

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

シーンの直下の子ノードを調べることもできます。

for node in sg.getChildren():
    print(node)

SoSeparatorsやSoGroupsといったこれらノードは自身の子ノードを持ちます。利用可能なCoinオブジェクトの全リストはCoin公式ドキュメントにあります。

今度はシーングラフに何か追加してみましょう。すてきな赤い立方体を追加してみることにしましょう:

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

(すてきな)赤い立方体ができました。以下を試してみてください:

col.rgb = (1, 1, 0)

わかったでしょうか?実行中であっても全てのものに対してアクセスと変更を行うことができるのです。再計算や再描画は必要ありません。Coinが全て面倒を見てくれます。物体のシーングラフへの追加、プロパティの変更、物体の非表示、一時的なオブジェクトの表示、何でもできます。もちろん3Dビューに関係したことだけですが。この表示はFreeCADがファイルを開いた時とオブジェクトが必要とした時に再計算されます。従って既存のFreeCADオブジェクトの外見を変更した場合、その変更はオブジェクトが再計算されるかファイルを再度開いた瞬間に失われます。

スクリプトでシーングラフを操作する場合の鍵となるのは必要な際に追加したノードの特定のプロパティにアクセスできるということなのです。例えば立方体を動かしたいとしましょう。自作のノードにSoTranslationノードを追加することになるでしょう。ちょうどこんな具合です:

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)

OpenInventorのシーングラフではその順番が重要であるということを思い出してください。ノードは以降のノードに影響を与えます。color red、cube、color yellow、sphere とすれば赤い立方体と黄色い球が表示されます。もし今、既存の自作ノードに移動を追加すると移動は立方体の後になり、立方体に影響を与えません。上記のように作成時に挿入しておけば次のようにできます:

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

こうすると立方体が2単位分、右に移動します。 最後に何かを取り除くには次のようにします:

sg.removeChild(myCustomNode)

Top

コールバック機能の使用

コールバック機能とはCoinライブラリの様な現在使用しているライブラリがコールバック、つまり現在実行中のPythonオブジェクトから特定の関数を呼び出すことを可能にするシステムのことです。これは非常に便利です。これを使うとシーンで何か特定のイベントが起きた場合にCoinからあなたへ通知することができるのです。Coinでは色々なものを監視することができます。マウスの位置、マウスボタンのクリック、キーボードのキーが押されたかどうか、まだまだ他にもあります。

FreeCADではそういったコールバックを簡単に使うための機能があります:

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

コールバックが起きた時にもオブジェクトは実行され続けなければならないのでコールバックはオブジェクトで初期化されなけれればなりません。 利用可能なイベントとパラメータの全リストまたはCoin公式ドキュメントを参照してください。

Top

ドキュメント

残念ながらPivy自体にはちゃんとしたドキュメントがまだありません。しかしPivyはCoinと正確に対応するのでC++スタイルをPythonスタイルに読み替えれば(例えばSoFile::getClassTypeId()はPivyではSoFile.getClassId()となるでしょう)Coinドキュメントをリファレンスとして安全に使用できます。

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.

Top

Scenegraph
PySide