2018年5月26日土曜日

namespaceを指定してFBXインポート

手動で行う分には問題なく行える操作ですが、スクリプトで実行しようとしたとき、若干はまりポイントだと思うので、メモを。
以下ソース
# -*- coding: utf-8 -*-
import maya.cmds as mc
import maya.mel as mel
def FBXImportToTargetNamespace(ns='targetNamespace'):
#set namespace
currentNs = mc.namespaceInfo(cur=True)
if not mc.namespace(ex=':%s'%ns):
mc.namespace(add=':%s'%ns)
mc.namespace(set=':%s'%ns)
#import FBX
pathList = mc.fileDialog2(fm=1,ds=2,ff=('FBX(*.fbx)'))
if not pathList:
return
mel.eval('FBXImport -f "%s"'%pathList[0])
#return current ns
mc.namespace(set=currentNs)
FBXImportToTargetNamespace(ns='testNs')

fileコマンドからインポートした時、namespaceを指定しても無反応。
カレントネームスペースを設定したうえでインポートするところがポイント。

2018年5月17日木曜日

Maya2017 systemからフォルダを開くと無限ループで落ちるMayaバグ

無印Maya2017では起こらなかったのですが、Maya2017 update4で以下ソースを実行すると、落ちます。
proc testProc(){
string $userYN = `confirmDialog -b "OK"`;
if ($userYN == "OK"){
system("load C:\Autodesk");
}
}

systemからエクスプローラーでダイアログを開こうとすると、なぜか処理がconfirmDialogのところへ戻って、以下無限ループ。
キャンセルなどして、systemの処理をスキップしようとすると、Mayaが固まって落ちました。

処理をpythonに逃がして対応できないかと模索したところ、
os.startfileの処理では、同様の結果となり、Mayaが落ちました。
しかし、subprocess.popenから処理を行ったところ、無事にクラッシュを回避できました。(以下ソース)
proc testProc(){
string $userYN = `confirmDialog -b "YES"`;
if ($userYN == "YES"){
string $path = "C:\Autodesk";
python("import subprocess");
python("subprocess.Popen('explorer \""+$path+"\"')");
//system("load C:\Autodesk");
}
}
testProc();

subprocessとos、melのsystemの大きな違いは、既定値でshellへ処理を飛ばすかどうか。
subprocessは既定ではshellで処理をしなかったはずなので、そのあたりに原因があるかも。

なお、最新のMaya2017 update5で発生するかどうかは未検証。 後日もう少し探ってみることにする。

2018年5月6日日曜日

OpenMaya:頂点処理の速度比較

以前海外の記述で見つけた記述を参考に、選択頂点で、設定値より近接している頂点の検索処理を、OpenMayaとmayaPythonで比較してみました。

処理内容はどちらも
①選択頂点の座標を取得
②openMayaのMVectorクラスを使って閾値以上近接している頂点を検索
③エラーがあればリストに格納して返す

以下ソース
mayaPython
import maya.cmds as mc
import maya.api.OpenMaya as om2
def checkNearVtx():
errorList=[]
vtxList = mc.ls(sl=True,fl=True)
vtxList2 = vtxList[:]
for vtx1 in vtxList:
pos1 = mc.pointPosition(vtx1)
vec1 = om2.MVector(pos1)
for vtx2 in vtxList2:
if vtx1 == vtx2:
continue
pos2 = mc.pointPosition(vtx2)
vec2 = om2.MVector(pos2)
vec3 = vec1-vec2
if vec3.length()<0.001:
errorList.extend([vtx1,vtx2])
vtxList2.remove(vtx1)
return errorList
start = time.time()
print checkNearVtx()
elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
view raw checkNearVtx hosted with ❤ by GitHub
openMaya
#_ Import required modules
import maya.cmds as mc
import maya.api.OpenMaya as om2
import time
def retunrnSelectVtxPos():
orgSelection = mc.ls(sl=True)
#getVtx IndxList
tmp = mc.polyListComponentConversion(orgSelection,tv=True)
selVtxList = mc.ls(tmp,fl=True)
selVtxList = sorted(selVtxList)
selDict = {}
for vtx in selVtxList:
shape = vtx.split('.')[0]
if not shape in selDict.keys():
selDict[shape] = []
indx = int(vtx.split('[')[-1].split(']')[0])
selDict[shape].append(indx)
returnDict={}
for shape in selDict.keys():
mc.select(shape)
selection = om2.MGlobal.getActiveSelectionList()
selObj = selection.getDagPath(0)
mfnObject = om2.MFnMesh(selObj)
posList = mfnObject.getPoints(om2.MSpace.kWorld)
returnDict[shape]=[]
for vtx in selDict[shape]:
returnDict[shape].append([vtx,posList[vtx]])
mc.select(orgSelection)
return returnDict
def checkNearVtxList(vtxDict):
errorList=[]
for key in vtxDict.keys():
MposList = vtxDict[key]
MposList2 = MposList[:]
for vtxInfo1 in MposList:
indx1 = vtxInfo1[0]
vec1 = om2.MVector(vtxInfo1[1])
for vtxInfo2 in MposList2:
indx2 = vtxInfo2[0]
if vtxInfo1==vtxInfo2:
continue
vec2 = om2.MVector(vtxInfo2[1])
vec3 = vec1-vec2
if vec3.length()<0.001:
vtx1 = '%s.vtx[%s]'%(key,indx1)
vtx2 = '%s.vtx[%s]'%(key,indx2)
errorList.extend([vtx1,vtx2])
MposList2.remove(vtxInfo1)
errorList = list(set(errorList))
return errorList
start = time.time()
dict = retunrnSelectVtxPos()
print checkNearVtxList(dict)
elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

で、結果。
mayaPythonの結果: elapsed_time:1.26999998093[sec]
openMayaの結果: elapsed_time:0.103000164032[sec]

うーん! これだけで10倍以上の処理速度の差が。
 ソースはもっと洗練できると思う。 もっと早くなるんじゃないだろか。
参考;
http://jensvhansen.com/fastest-way-to-query-vertex-position-in-maya/
https://boomrigs.com/blog/2016/1/12/how-to-get-mesh-vertex-position-through-maya-api