2016-03-05 75 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。我还应该注意,你之后也必须解决法律问题。