gpt4 book ai didi

python - Maya python迭代大量顶点

转载 作者:行者123 更新时间:2023-12-01 04:04:51 29 4
gpt4 key购买 nike

我正在用 python 为 Maya 编写一个脚本,将顶点位置从一侧交换到另一侧。由于我希望翻转基于拓扑,因此我使用拓扑对称选择工具来查找顶点对应关系。我设法使用 filterExpand 和 xform 来做到这一点。问题是它在大型多边形计数网格上速度相当慢,我想知道如何使用 openMaya 来完成此操作。

将maya.cmds导入为cmd

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来获取和设置顶点。

下面是一个简单地沿 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 进行操作。我还应该注意,您还必须随后解决法线问题。

关于python - Maya python迭代大量顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35808363/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com