2016-03-05 70 views
0

我正在寫python的瑪雅腳本來交換頂點位置從一邊到另一邊。 因爲我想要翻轉爲基於拓撲,我使用拓撲對稱選擇工具來查找頂點對應。 我設法使用filterExpand和xform來做到這一點。 問題是,它在一個大的聚合網格上很慢,我想知道如何使用openMaya來完成。瑪雅python迭代大量的頂點

進口maya.cmds作爲CMDS

def flipMesh(): 
    sel=cmds.ls(sl=1) 
    axis={'x':0,'y':1,'z':2} 
    reverse=[1.0,1.0,1.0] 
    #quring the axtive symmetry axis 
    activeaxis=cmds.symmetricModelling(q=1, axis=1) 
    reverse[axis[activeaxis]]=-1.0 
    #getting the vertex count 
    verts=cmds.polyEvaluate(v=1) 
    #selecting all vertex 
    cmds.select(sel[0]+'.vtx[0:'+str(verts)+']') 
    #getting all the positive vertex 
    posit=cmds.filterExpand(sm=31,ex=1,smp=1) 
    seam=cmds.filterExpand(sm=31,ex=1,sms=1) 
    #swapping position on the positive side with the negative side 
    for pos in posit: 
     cmds.select(pos, sym=True) 
     neg=cmds.filterExpand(sm=31,ex=1,smn=1) 
     posT=cmds.xform(pos, q=1, t=1) 
     negT=cmds.xform(neg[0], q=1, t=1) 
     cmds.xform(pos,t=[a*b for a,b in zip(negT,reverse)]) 
     cmds.xform(neg[0],t=[a*b for a,b in zip(posT,reverse)]) 
    #inverting position on the seam 
    for each in seam:  
     seamP=cmds.xform(each, q=1, t=1) 
     seaminvP=[a*b for a,b in zip(seamP,reverse)] 
     cmds.xform(each, t=(seaminvP)) 
    cmds.select(sel) 

感謝 莫里吉奧

+0

您是否試圖使鏡像平面的一側的頂點與另一側的頂點匹配?或者你是否試圖將vert從一側交換到另一側? – theodox

+0

該腳本實際上已經將頂點的位置從一側交換到另一側。問題更多的是如何使用開放的瑪雅圖書館來做到這一點 –

回答

0

您可以嘗試OpenMaya.MFnMesh獲取和設置您的頂點。

這裏是一個只會反映選定對象的所有點沿其Z軸爲例:

import maya.OpenMaya as OpenMaya 

# Get selected object 
mSelList = OpenMaya.MSelectionList() 
OpenMaya.MGlobal.getActiveSelectionList(mSelList) 
sel = OpenMaya.MItSelectionList(mSelList) 
path = OpenMaya.MDagPath() 
sel.getDagPath(path) 

# Attach to MFnMesh 
MFnMesh = OpenMaya.MFnMesh(path) 

# Create empty point array to store new points 
newPointArray = OpenMaya.MPointArray() 

for i in range(MFnMesh.numVertices()): 
    # Create a point, and mirror it 
    newPoint = OpenMaya.MPoint() 
    MFnMesh.getPoint(i, newPoint) 
    newPoint.z = -newPoint.z 
    newPointArray.append(newPoint) 

# Set new points to mesh all at once 
MFnMesh.setPoints(newPointArray) 

而不是移動它們在一個時刻,你可以使用MFnMesh.setPoints來設置他們的一次。你必須適應你的邏輯,但希望這可以幫助你控制Maya的api。我還應該注意,你之後也必須解決法律問題。