tag:crieit.net,2005:https://crieit.net/users/kutuharu/feed ヨセミテの投稿 - Crieit Crieitでユーザーヨセミテによる最近の投稿 2019-09-01T00:57:44+09:00 https://crieit.net/users/kutuharu/feed tag:crieit.net,2005:PublicArticle/15368 2019-09-01T00:57:44+09:00 2019-09-01T00:57:44+09:00 https://crieit.net/posts/MObject-MDagPath MObjectからMDagPathを取得する <h1 id="MDepenedencyNodeからMDagPathを取得する"><a href="#MDepenedencyNode%E3%81%8B%E3%82%89MDagPath%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B">MDepenedencyNodeからMDagPathを取得する</a></h1> <p>API経由で階層などを取得すると、MDepenedencyNode(インスタンスの形式はMObject)で帰ってくることが多い<br /> でもそれを別のMFnに入れようとすると大体求められるのはMDagPath。</p> <p>MDependencyNodeからMDagPathを取得するにはMFnDagPathにMObjectを入れる必要がある。MFnDependecyNodeにはDagPathを取得機能はないのでちょっと分かりづらい</p> <h2 id="MFnDagNode"><a href="#MFnDagNode">MFnDagNode</a></h2> <p><a target="_blank" rel="nofollow noopener" href="https://help.autodesk.com/view/MAYAUL/2019/JPN/?guid=Maya_SDK_MERGED_py_ref_class_open_maya_1_1_m_fn_dag_node_html">Maya2019 MFnDagNode クラスリファレンス</a></p> <p>DagPathの生成と階層の走査が出来るクラス</p> <h2 id="テストスクリプト"><a href="#%E3%83%86%E3%82%B9%E3%83%88%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88">テストスクリプト</a></h2> <p>以下の四角があるだけのシーンで後述のスクリプトをテスト</p> <p><a href="https://crieit.now.sh/upload_images/844318ac123aeb28755363d0bce4f1da5d6a96b6b1b75.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/844318ac123aeb28755363d0bce4f1da5d6a96b6b1b75.png?mw=700" alt="2019-09-01 00_47_48-Autodesk Maya 2019_ 無題_.png" /></a></p> <hr /> <p>ボックスを選択して実行</p> <pre><code class="python">from maya.api import OpenMaya as om2 sel_list = om2.MGlobal.getActiveSelectionList() # selection list からの MDagPath と MDependensyNode の取得 dag_from_sel = sel_list.getDagPath(0) dep_from_sel = sel_list.getDependNode(0) print type(dag_from_sel) # return : &lt;type 'OpenMaya.MDagPath'&gt; print type(dep_from_sel) # return : &lt;type 'OpenMaya.MObject'&gt; </code></pre> <p>定義時同様の方法で、MSelectionListを定義して取得しようとしてもMDependencyNodeからはMDagPathの取得はできない。</p> <pre><code class="python"># DagPathならselection listに再度入れると取得できる temp_sel_list = om2.MSelectionList() temp_sel_list.add(dag_from_sel) print temp_sel_list.getDependNode(0) # return : &lt;OpenMaya.MObject object at 0x000001B6519BF6F0&gt; # DepenedencyNodeはselection listに再度入れてもDagPathを取得できない temp_sel_list = om2.MSelectionList() temp_sel_list.add(dep_from_sel) print temp_sel_list.getDagPath(0) # エラー: TypeError: file &lt;maya console&gt; line 3: item is not a DAG path # </code></pre> <p>MFnDagNodeクラスを経由することでMDependensyNodeからMDagPathの取得が出来る。</p> <pre><code class="python"># MObject → MDadPathはMFnDagNodeに入れると取得できる mfn_dag = om2.MFnDagNode(dep_from_sel) dag_from_dep = mfn_dag.getPath() print type(dag_from_dep) # return : &lt;type 'OpenMaya.MDagPath'&gt; print dag_from_dep # return : pCube1 </code></pre> ヨセミテ tag:crieit.net,2005:PublicArticle/15350 2019-08-25T22:38:19+09:00 2019-08-25T22:38:19+09:00 https://crieit.net/posts/MDagPath MDagPathの掘り下げ <p>ゲーム系でTAしてます。</p> <p>スクラップボックスから引っ越し中<br /> 引っ越し前の記事から新しい情報はありません。<br /> スクラップボックスはコードコピペするとインデント崩れるので止めましたが、入力周りでもcrieitは使いやすいですね。</p> <hr /> <h1 id="MDagPathの掘り下げ"><a href="#MDagPath%E3%81%AE%E6%8E%98%E3%82%8A%E4%B8%8B%E3%81%92">MDagPathの掘り下げ</a></h1> <p>今の会社で、家でMayaを利用できる環境を提供いただけたのでAPI2.0の情報をまとめていこうと思います。<br /> テスト環境はMaya2019。</p> <h2 id="MDagPath"><a href="#MDagPath">MDagPath</a></h2> <p><a target="_blank" rel="nofollow noopener" href="https://help.autodesk.com/view/MAYAUL/2019/JPN/?guid=Maya_SDK_MERGED_py_ref_class_open_maya_1_1_m_dag_path_html">## Maya Python API2.0 2019 Reference</a></p> <p>MDagPathはMayaAPIのMFn、MItなどのノードに対するクラス群を利用する際に利用するクラス。<br /> APIを利用する際には絶対使うと考えてもよいと思う。</p> <h3 id="MDagPath インスタンスの取得と利用"><a href="#MDagPath+%E3%82%A4%E3%83%B3%E3%82%B9%E3%82%BF%E3%83%B3%E3%82%B9%E3%81%AE%E5%8F%96%E5%BE%97%E3%81%A8%E5%88%A9%E7%94%A8">MDagPath インスタンスの取得と利用</a></h3> <p>ノード単位でインスタンスを定義して利用するもので、自分だと以下の名前で取得する書き方でよく取得する。<br /> <strong>コードを試す場合はCubeを一つ作成してください。</strong></p> <p>``` python<a href="https://crieit.now.sh/upload_images/10188592e7a97caca403c39ab13808675d628e5499a60.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/10188592e7a97caca403c39ab13808675d628e5499a60.png?mw=700" alt="46fa376e0744bbd562f9b24d9265417e-png.png" /></a><br /> from maya.api import OpenMaya as om2</p> <h1 id="ノード名からDagPathを取得"><a href="#%E3%83%8E%E3%83%BC%E3%83%89%E5%90%8D%E3%81%8B%E3%82%89DagPath%E3%82%92%E5%8F%96%E5%BE%97">ノード名からDagPathを取得</a></h1> <p>node_name = "pCubeShape1"<br /> sel_list = om2.MGlobal.getSelectionListByName(node_name) # MGlobalにSelectionListを名前から取得する関数がある<br /> dag = sel_list.getDagPath(0) # Selectionリストのindexを指定してDagPathを取得</p> <pre><code><br />もしくは他のAPIクラスからMObjectを取得した場合はMFnDagNodeクラスからDagPathは取得できる。 ``` python # MObjectからDagPathを取得 node_name = "pCubeShape1" sel_list = om2.MGlobal.getSelectionListByName(node_name) dag = sel_list.getDagPath(0) transform = om2.MDagPath(dag).transform() # TransformのMObjectを取得(この時点だとDagPathではない) mfndag = om2.MFnDagNode(transform) # MObjectからMFnインスタンスを定義 dag = mfndag.getPath() # DagPathを取得 om2.MFnTransform(dag) # MFnTransformにクラスに利用できればDagPath </code></pre> <p>取得したDagPathをMFnMeshなどのクラス定義に利用すればそのノードに対する関数を利用できるようになります。<br /> また、DagPathは階層構造を取得するためのクラスでマテリアルなどの階層構造に依存しないノードは持っていません。</p> <pre><code class="python">node_name = "pCubeShape1" sel_list = om2.MGlobal.getSelectionListByName(node_name) dag = sel_list.getDagPath(0) mfnmesh = om2.MFnMesh(dag) print mfnmesh.numPolygons # MFnMeshクラスの関数でポリゴン数を取得 node_name = "lambert1" sel_list = om2.MGlobal.getSelectionListByName(node_name) dag = sel_list.getDagPath(0) # LambertマテリアルにDagPathは無い # エラー: TypeError: file &lt;maya console&gt; line 10: item is not a DAG path # </code></pre> <h3 id="MDagPathの機能"><a href="#MDagPath%E3%81%AE%E6%A9%9F%E8%83%BD">MDagPathの機能</a></h3> <p>DagPathはノードの位置情報を持っているのでMFn,MItなどのインスタンスに入れなくてもそれ単体で階層構造を取得する機能を持っています。以下のシーンで実行。<strong>コードを試す場合ボックスと多面体を作ってグループ化してください</strong>。</p> <p><a href="https://crieit.now.sh/upload_images/3a602f21ce628b4922052754d224056f5d628ec497b56.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/3a602f21ce628b4922052754d224056f5d628ec497b56.png?mw=700" alt="dag_path_testpng.png" /></a></p> <pre><code class="python">from maya.api import OpenMaya as om2 node_name = "group1" sel_list = om2.MGlobal.getSelectionListByName(node_name) dag = sel_list.getDagPath(0) # cmds.lsでいうlongNameも取得できる print dag.fullPathName() # 子階層のMObjectを取得できる。indexは何個目の子ノードかの指定 print "num_child".format(dag.childCount()) child_obj = dag.child(0) print "child_dag_1: {}".format(om2.MFnDagNode(child_obj).getPath()) child_obj = dag.child(1) print "child_dag_2: {}".format(om2.MFnDagNode(child_obj).getPath()) # 親ノードの数も取得できる num_hierarchy = dag.length() print u"num_hierarchy : {}".format(num_hierarchy) # 一つ上の階層のDagPathを取得できる for index in xrange(num_hierarchy): upper_dag = dag.pop() print "path : {} - type: {}".format(upper_dag.node, type(upper_dag)) </code></pre> <p>他にも覚えておくと便利そうなことだと、現在の階層までのTransformのMatrixを取れたりもする。</p> <pre><code class="python"># group2のスケール(1.0,0.8,1.0) # ∟ group1のスケール(1.0,0.4,1.0) print dag.exclusiveMatrix() # 該当ノードのMatrix print dag.inclusiveMatrix() # 該当ノードまでのMatrix # (((1, 0, 0, 0), (0, 0.8, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))) # (((1, 0, 0, 0), (0, 0.32, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))) </code></pre> <p>DagPathはAPI使う場合はめっちゃ使うのでいろいろ試してみることをお勧めします。</p> ヨセミテ