editMAでコンストレインを書き出す


https://qiita.com/9boz/private/a278945f2d95329814d0

さてアニメーションは書き出せたので、コンストレインも書き出したいですね。
コンストレインが書き出せるということは、アニメーションをベイクしなくても良いのでより選択肢が広がります。

書き出し
--

さて exportEditsコマンドを眺めてみます。

https://help.autodesk.com/cloudhelp/2022/JPN/Maya-Tech-Docs/CommandsPython/exportEdits.html

>includeConstraints(ic)
さらに、書き出されるターゲット ノードに関連するコンストレイント関連のノードを含みます。

**コンストレイント関連のノード**というのが　？？ですが、多分これかなぁ。

同一リファレンス内のキューブからスフィアへparentConstraintを掛けました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1484234/e9912803-0e4a-c761-1a79-27504964afe2.png)

スフィアを選択して、書き出してみます。

```python
import maya.cmds as cmds
exportFilePath = "D:/test/testConstraint.editMA"
cmds.exportEdits(
                            exportFilePath,
                            type = "editMA",
                            selected =True,
                            includeConstraints =True,
                            f =True
                        )

```

書き出されたファイルを眺めてみると、

- コンストレインノードの作成
- コンストレインノードとのコネクション

が記載されていますね。

```testConstraint.editMA
//Maya ASCII 2022 scene
//Name: testConstraint.editMA
//Last modified: Sun, Dec 01, 2024 05:05:25 PM
//Codeset: 932
requires maya "2022";
requires -nodeType "aiOptions" -nodeType "aiAOVDriver" -nodeType "aiAOVFilter" "mtoa" "5.0.0.4";
requires "stereoCamera" "10.0";
currentUnit -l centimeter -a degree -t film;
fileInfo "fileCreationSource" "ExportEdits";
createNode parentConstraint -n "pSphere1_parentConstraint1";
	rename -uid "CFA4ADB4-4415-635B-C095-2BAC3145E243";
	addAttr -dcb 0 -ci true -k true -sn "w0" -ln "pCube1W0" -dv 1 -min 0 -at "double";
	setAttr -k on ".nds";
	setAttr -k off ".v";
	setAttr -k off ".tx";
	setAttr -k off ".ty";
	setAttr -k off ".tz";
	setAttr -k off ".rx";
	setAttr -k off ".ry";
	setAttr -k off ".rz";
	setAttr -k off ".sx";
	setAttr -k off ".sy";
	setAttr -k off ".sz";
	setAttr ".erp" yes;
	setAttr ".tg[0].tot" -type "double3" -2.413729457040962 0 -0.12020348705592987 ;
	setAttr -k on ".w0";
connectAttr "pSphere1_parentConstraint1.ctx" "<main>:pSphere1.tx";
connectAttr "pSphere1_parentConstraint1.cty" "<main>:pSphere1.ty";
connectAttr "pSphere1_parentConstraint1.ctz" "<main>:pSphere1.tz";
connectAttr "pSphere1_parentConstraint1.crx" "<main>:pSphere1.rx";
connectAttr "pSphere1_parentConstraint1.cry" "<main>:pSphere1.ry";
connectAttr "pSphere1_parentConstraint1.crz" "<main>:pSphere1.rz";
connectAttr "<main>:pSphere1.ro" "pSphere1_parentConstraint1.cro";
connectAttr "<main>:pSphere1.pim" "pSphere1_parentConstraint1.cpim";
connectAttr "<main>:pSphere1.rp" "pSphere1_parentConstraint1.crp";
connectAttr "<main>:pSphere1.rpt" "pSphere1_parentConstraint1.crt";
connectAttr "<main>:pCube1.t" "pSphere1_parentConstraint1.tg[0].tt";
connectAttr "<main>:pCube1.rp" "pSphere1_parentConstraint1.tg[0].trp";
connectAttr "<main>:pCube1.rpt" "pSphere1_parentConstraint1.tg[0].trt";
connectAttr "<main>:pCube1.r" "pSphere1_parentConstraint1.tg[0].tr";
connectAttr "<main>:pCube1.ro" "pSphere1_parentConstraint1.tg[0].tro";
connectAttr "<main>:pCube1.s" "pSphere1_parentConstraint1.tg[0].ts";
connectAttr "<main>:pCube1.pm" "pSphere1_parentConstraint1.tg[0].tpm";
connectAttr "pSphere1_parentConstraint1.w0" "pSphere1_parentConstraint1.tg[0].tw"
		;
connectAttr "pSphere1_parentConstraint1.msg" "<root>:MayaNodeEditorSavedTabsInfo.tgi[0].ni[10].dn"
		;
// End of testConstraint.editMA

```

読み込み
--

読み込みのコマンド自体は、アニメーションの読み込みと特に変わらないはずなのでそのまま実行してみます。

```python
import maya.cmds as cmds
exportFilePath = "D:/test/testConstraint.editMA"

cmds.file(exportFilePath,
        type = "editMA",
        i =True,
        applyTo = "sampleAsset_v01RN",
        f =True
        )
```

無事コンストレインが再構築されました。
が、コンストレインノードが通常の挙動と違いworldに出てしまうのは少しきになりますね。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1484234/f3ca7de2-1623-c6cf-5080-85124ede7b0f.png)

別の組み合わせ
--

同一のnamespace内でのコンストレイン情報の読み書きは出来ましたが、
別の場合はどうでしょうか。

シーンに追加したlocator1からconeに対してparentConstraintを掛けました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1484234/39a40e52-db27-96c8-edac-d35a1e642fc0.png)

これを書き出してみます。

```python
import maya.cmds as cmds
exportFilePath = "D:/test/testConstraintCone.editMA"
cmds.exportEdits(
                            exportFilePath,
                            type = "editMA",
                            selected =True,
                            includeConstraints =True,
                            f =True
                        )
```

コンストレインノードとリファレンスされてるアセットの処理は一緒ですが、
locator1に関しては　`<root>:` がついてますね。
これは読み込ませるときに何か指定すべきか・・・

``` testConstraintCone.editMA
//Maya ASCII 2022 scene
//Name: testConstraintCone.editMA
//Last modified: Sun, Dec 01, 2024 05:15:44 PM
//Codeset: 932
requires maya "2022";
requires -nodeType "aiOptions" -nodeType "aiAOVDriver" -nodeType "aiAOVFilter" "mtoa" "5.0.0.4";
requires "stereoCamera" "10.0";
currentUnit -l centimeter -a degree -t film;
fileInfo "fileCreationSource" "ExportEdits";
createNode parentConstraint -n "pCone1_parentConstraint1";
	rename -uid "A119C2F9-4062-A254-F6A6-B69734074287";
	addAttr -dcb 0 -ci true -k true -sn "w0" -ln "locator1W0" -dv 1 -min 0 -at "double";
	setAttr -k on ".nds";
	setAttr -k off ".v";
	setAttr -k off ".tx";
	setAttr -k off ".ty";
	setAttr -k off ".tz";
	setAttr -k off ".rx";
	setAttr -k off ".ry";
	setAttr -k off ".rz";
	setAttr -k off ".sx";
	setAttr -k off ".sy";
	setAttr -k off ".sz";
	setAttr ".erp" yes;
	setAttr ".tg[0].tot" -type "double3" -1.4121586931709507 0 -5.7290939218611925 ;
	setAttr ".rst" -type "double3" 1.4291536085618226 0 -3.2073003230214763 ;
	setAttr -k on ".w0";
connectAttr "pCone1_parentConstraint1.ctx" "<main>:pCone1.tx";
connectAttr "pCone1_parentConstraint1.cty" "<main>:pCone1.ty";
connectAttr "pCone1_parentConstraint1.ctz" "<main>:pCone1.tz";
connectAttr "pCone1_parentConstraint1.crx" "<main>:pCone1.rx";
connectAttr "pCone1_parentConstraint1.cry" "<main>:pCone1.ry";
connectAttr "pCone1_parentConstraint1.crz" "<main>:pCone1.rz";
connectAttr "<main>:pCone1.ro" "pCone1_parentConstraint1.cro";
connectAttr "<main>:pCone1.pim" "pCone1_parentConstraint1.cpim";
connectAttr "<main>:pCone1.rp" "pCone1_parentConstraint1.crp";
connectAttr "<main>:pCone1.rpt" "pCone1_parentConstraint1.crt";
connectAttr "<root>:locator1.t" "pCone1_parentConstraint1.tg[0].tt";
connectAttr "<root>:locator1.rp" "pCone1_parentConstraint1.tg[0].trp";
connectAttr "<root>:locator1.rpt" "pCone1_parentConstraint1.tg[0].trt";
connectAttr "<root>:locator1.r" "pCone1_parentConstraint1.tg[0].tr";
connectAttr "<root>:locator1.ro" "pCone1_parentConstraint1.tg[0].tro";
connectAttr "<root>:locator1.s" "pCone1_parentConstraint1.tg[0].ts";
connectAttr "<root>:locator1.pm" "pCone1_parentConstraint1.tg[0].tpm";
connectAttr "pCone1_parentConstraint1.w0" "pCone1_parentConstraint1.tg[0].tw";
connectAttr "pCone1_parentConstraint1.msg" "<root>:MayaNodeEditorSavedTabsInfo.tgi[0].ni[1].dn"
		;
// End of testConstraintCone.editMA

```

とりあえず読み込んでみます。

```python
import maya.cmds as cmds
exportFilePath = "D:/test/testConstraintCone.editMA"
cmds.file(exportFilePath,
        type = "editMA",
        i =True,
        applyTo = "sampleAsset_v01RN",
        f =True
        )
```

問題なく生成されましたね。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1484234/272023b5-f2bd-c083-1101-9d475fc74104.png)


さらに別の組み合わせ
--

さっきはシーンに追加したlocator1を使用しましたが、
新たに別のファイルをリファレンスして別のnamespace間でコンストレインした場合はどうでしょうか？

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1484234/4ee75409-0fe9-f538-9f05-8f4135477429.png)

```python
import maya.cmds as cmds
exportFilePath = "D:/test/testConstraint2.editMA"
cmds.exportEdits(
                            exportFilePath,
                            type = "editMA",
                            selected =True,
                            includeConstraints =True,
                            f =True
                        )
```

placeHolderが　`<main>`　と `<sampleAsset_v01RN1>` の2つに増えましたね。


```testConstraint2.editMA
//Maya ASCII 2022 scene
//Name: testConstraint2.editMA
//Last modified: Sun, Dec 01, 2024 05:29:19 PM
//Codeset: 932
requires maya "2022";
requires -nodeType "aiOptions" -nodeType "aiAOVDriver" -nodeType "aiAOVFilter" "mtoa" "5.0.0.4";
requires "stereoCamera" "10.0";
currentUnit -l centimeter -a degree -t film;
fileInfo "fileCreationSource" "ExportEdits";
createNode parentConstraint -n "pSphere1_parentConstraint1";
	rename -uid "46A98D5B-4314-001F-53E6-6182AC679CFB";
	addAttr -dcb 0 -ci true -k true -sn "w0" -ln "pSphere1W0" -dv 1 -min 0 -at "double";
	setAttr -k on ".nds";
	setAttr -k off ".v";
	setAttr -k off ".tx";
	setAttr -k off ".ty";
	setAttr -k off ".tz";
	setAttr -k off ".rx";
	setAttr -k off ".ry";
	setAttr -k off ".rz";
	setAttr -k off ".sx";
	setAttr -k off ".sy";
	setAttr -k off ".sz";
	setAttr ".erp" yes;
	setAttr ".tg[0].tot" -type "double3" 4.5490169468122517 0 4.5160390953307257 ;
	setAttr -k on ".w0";
connectAttr "pSphere1_parentConstraint1.ctx" "<main>:pSphere1.tx";
connectAttr "pSphere1_parentConstraint1.cty" "<main>:pSphere1.ty";
connectAttr "pSphere1_parentConstraint1.ctz" "<main>:pSphere1.tz";
connectAttr "pSphere1_parentConstraint1.crx" "<main>:pSphere1.rx";
connectAttr "pSphere1_parentConstraint1.cry" "<main>:pSphere1.ry";
connectAttr "pSphere1_parentConstraint1.crz" "<main>:pSphere1.rz";
connectAttr "<main>:pSphere1.ro" "pSphere1_parentConstraint1.cro";
connectAttr "<main>:pSphere1.pim" "pSphere1_parentConstraint1.cpim";
connectAttr "<main>:pSphere1.rp" "pSphere1_parentConstraint1.crp";
connectAttr "<main>:pSphere1.rpt" "pSphere1_parentConstraint1.crt";
connectAttr "<sampleAsset_v01RN1>:pSphere1.t" "pSphere1_parentConstraint1.tg[0].tt"
		;
connectAttr "<sampleAsset_v01RN1>:pSphere1.rp" "pSphere1_parentConstraint1.tg[0].trp"
		;
connectAttr "<sampleAsset_v01RN1>:pSphere1.rpt" "pSphere1_parentConstraint1.tg[0].trt"
		;
connectAttr "<sampleAsset_v01RN1>:pSphere1.r" "pSphere1_parentConstraint1.tg[0].tr"
		;
connectAttr "<sampleAsset_v01RN1>:pSphere1.ro" "pSphere1_parentConstraint1.tg[0].tro"
		;
connectAttr "<sampleAsset_v01RN1>:pSphere1.s" "pSphere1_parentConstraint1.tg[0].ts"
		;
connectAttr "<sampleAsset_v01RN1>:pSphere1.pm" "pSphere1_parentConstraint1.tg[0].tpm"
		;
connectAttr "pSphere1_parentConstraint1.w0" "pSphere1_parentConstraint1.tg[0].tw"
		;
// End of testConstraint2.editMA
```

とくに編集することなく読み込んでみます。

```python
import maya.cmds as cmds
exportFilePath = "D:/test/testConstraint2.editMA"

cmds.file(exportFilePath,
        type = "editMA",
        i =True,
        applyTo = "sampleAsset_v01RN",
        f =True
        )

```

別のnamespaceに適用させる必要が無ければ、このままで問題なさそうですね。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1484234/7a5f8a6f-f5b8-a6c6-8380-5ff4547af213.png)