Maya python迭代大量顶点 [英] maya python iterating a big number of vertex
问题描述
我正在用python写一个脚本,用于玛雅人将顶点位置从一侧交换到另一侧. 由于我希望翻转是基于拓扑的,因此我使用拓扑对称性选择工具来查找顶点对应关系. 我设法使用filterExpand和xform做到了这一点. 问题在于,它在大型的多边形计数网格上非常慢,我想知道如何使用openMaya来做到这一点.
I am writing a script in python for maya to swap vertex position from one side to another. Since I want the flipping to be topology based I am using the topological symmetry selection tool to find the vertex correspondence. I managed to do that using filterExpand and xform. The problem is that it is quite slow on a large poly count mesh and I was wondering how this could be done using openMaya instead.
将maya.cmds导入为cmd
import maya.cmds as 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)
谢谢 毛里齐奥
推荐答案
您可以尝试OpenMaya.MFnMesh
获取并设置顶点.
You can try out OpenMaya.MFnMesh
to get and set your vertices.
下面是一个示例,该示例将简单地沿其z轴镜像所选对象的所有点:
Here's an example that will simply mirror all points of a selected object along their z axis:
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.我还应注意,您随后还必须解决法线问题.
Instead of moving them one at at time you can use MFnMesh.setPoints
to set them all at once. You'll have to adapt your logic to this, but hopefully this will help you out manipulating with Maya's api. I should also note that you would also have to resolve normals afterwards.
这篇关于Maya python迭代大量顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!