我们已经知道如何对物体进行选择,但如果对象是component呢,我们怎么去使用api来选择点、边、面等等的component呢?
在场景中创建一个多边形物体,然后选择它的点、边、面或UV,分开或一起选都行,在执行下面的代码
你会得到类似这样的结果
[quote]Object: pCubeShape1
0) -0.500 -0.500 0.500
1) 0.500 -0.500 0.500
2) -0.500 0.500 0.500
3) 0.500 0.500 0.500
Object: pCubeShape1
4) -0.500 0.500 -0.500
5) 0.500 0.500 -0.500
6) -0.500 -0.500 -0.500
7) 0.500 -0.500 -0.500[/quote]
如果你没有选择任何的component则只会得到所选物体的名称
[quote]Object: pCubeShape1[/quote]
上面用到的是MItGeometry迭代器,它可以迭代物体的points/CV/vertices,包括mesh(多边形), nurbs surface, nurbs curve(曲线), subdivision surface(细分面) 以及 lattice(晶格)。那如果我们要迭代其它的component呢,如边、面等等。Maya里还有其它的迭代器,如MItMeshEdge、MItMeshPolygon、MItSurfaceCV等
在maya中创建一个多边形和一条曲线,分别或同时选择曲线的点、多边形的边、多边形的面,分别对它们执行下面的代码
你会得到类似的结果
[quote]Object: pSphereShape1
381) 0.188 -0.969 -0.137 hard
401) 0.309 -0.921 -0.224 hard
421) 0.421 -0.850 -0.306 hard
441) 0.524 -0.758 -0.381 hard
461) 0.613 -0.647 -0.446 hard
481) 0.688 -0.521 -0.500 hard
501) 0.745 -0.382 -0.541 hard
521) 0.784 -0.233 -0.570 hard
541) 0.804 -0.078 -0.584 hard
561) 0.804 0.078 -0.584 hard
581) 0.784 0.233 -0.570 hard
601) 0.745 0.382 -0.541 hard
621) 0.688 0.521 -0.500 hard
641) 0.613 0.647 -0.446 hard
661) 0.524 0.758 -0.381 hard
681) 0.421 0.850 -0.306 hard
701) 0.309 0.921 -0.224 hard
721) 0.188 0.969 -0.137 hard
741) 0.063 -0.994 -0.046 hard
761) 0.063 0.994 -0.046 hard
Object: pTorusShape1
415) -2.374 -0.741 -2.727 smooth
435) -2.359 -0.594 -2.681 smooth
455) -2.331 -0.469 -2.595 smooth
475) -2.292 -0.378 -2.476 smooth
495) -2.247 -0.331 -2.336 smooth
515) -2.199 -0.331 -2.189 smooth
535) -2.154 -0.378 -2.050 smooth
555) -2.115 -0.469 -1.931 smooth
575) -2.087 -0.594 -1.844 smooth
595) -2.072 -0.741 -1.799 smooth
615) -2.072 -0.896 -1.799 smooth
635) -2.087 -1.043 -1.844 smooth
655) -2.115 -1.168 -1.931 smooth
675) -2.154 -1.258 -2.050 smooth
695) -2.199 -1.306 -2.189 smooth
715) -2.247 -1.306 -2.336 smooth
735) -2.292 -1.258 -2.476 smooth
755) -2.331 -1.168 -2.595 smooth
775) -2.359 -1.043 -2.681 smooth
795) -2.374 -0.896 -2.727 smooth
Object: pSphereShape1
137) 0.811 -0.380 0.413 planar
155) 0.435 -0.232 0.853 planar
156) 0.677 -0.232 0.677 planar
157) 0.853 -0.232 0.435 planar
174) 0.154 -0.078 0.970 planar
175) 0.446 -0.078 0.875 planar
176) 0.694 -0.078 0.694 planar
177) 0.875 -0.078 0.446 planar
193) -0.154 0.078 0.970 planar
194) 0.154 0.078 0.970 planar
195) 0.446 0.078 0.875 planar
196) 0.694 0.078 0.694 planar
197) 0.875 0.078 0.446 planar
212) -0.435 0.232 0.853 planar
213) -0.150 0.232 0.946 planar
214) 0.150 0.232 0.946 planar
215) 0.435 0.232 0.853 planar
216) 0.677 0.232 0.677 planar
217) 0.853 0.232 0.435 planar
231) -0.644 0.380 0.644 planar
232) -0.413 0.380 0.811 planar
233) -0.142 0.380 0.899 planar
234) 0.142 0.380 0.899 planar
235) 0.413 0.380 0.811 planar
236) 0.644 0.380 0.644 planar
237) 0.811 0.380 0.413 planar
251) -0.595 0.519 0.595 planar
252) -0.382 0.519 0.749 planar
253) -0.132 0.519 0.830 planar
254) 0.132 0.519 0.830 planar
255) 0.382 0.519 0.749 planar
256) 0.595 0.519 0.595 planar
257) 0.749 0.519 0.382 planar
271) -0.531 0.645 0.531 planar
272) -0.341 0.645 0.669 planar
273) -0.117 0.645 0.742 planar
274) 0.117 0.645 0.742 planar
275) 0.341 0.645 0.669 planar
276) 0.531 0.645 0.531 planar
292) -0.292 0.755 0.573 planar
293) -0.101 0.755 0.635 planar
294) 0.101 0.755 0.635 planar
295) 0.292 0.755 0.573 planar
312) -0.236 0.847 0.462 planar
313) -0.081 0.847 0.512 planar
314) 0.081 0.847 0.512 planar
Object: curveShape1
2) 6.073 0.000 -8.701
3) 11.819 0.000 -2.601
4) 10.852 0.000 9.138
5) 7.350 0.000 13.090
6) 3.782 0.000 14.419[/quote]
在场景中创建一个多边形物体,然后选择它的点、边、面或UV,分开或一起选都行,在执行下面的代码
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import maya.OpenMaya as om
# 创建选择列表
selList = om.MSelectionList()
# 获取当前所选物体
om.MGlobal.getActiveSelectionList(selList)
# 创建迭代器,对选择列表进行迭代
it = om.MItSelectionList(selList)
while not it.isDone():
dagPath = om.MDagPath() # 用来存储所选物体的路径
component = om.MObject() # 用来存储所选物体的组件的列表
# 返回一个transform或shape节点的路径到dagPath
# 返回物体的组件的列表,如果组件被选择的话
it.getDagPath(dagPath, component)
# 添加一个方法集来处理物体
fn = om.MFnDependencyNode(dagPath.node())
# 打印物体的名称
print "\nObject: %s" % fn.name()
# 如果组件的列表是有效的
if not component.isNull():
# 给组件列表创建一个geometry迭代器。
# 它可以处理所有的组件,如边、面等
itGeom = om.MItGeometry(dagPath, component)
# 迭代所有点
while not itGeom.isDone():
# 获取它们的世界坐标
point = itGeom.position(om.MSpace.kWorld)
# 打印点的索引和坐标
print "\t%d) %.3f %.3f %.3f" % (itGeom.index(),
point.x, point.y, point.z)
# 下一个
itGeom.next()
it.next()
你会得到类似这样的结果
[quote]Object: pCubeShape1
0) -0.500 -0.500 0.500
1) 0.500 -0.500 0.500
2) -0.500 0.500 0.500
3) 0.500 0.500 0.500
Object: pCubeShape1
4) -0.500 0.500 -0.500
5) 0.500 0.500 -0.500
6) -0.500 -0.500 -0.500
7) 0.500 -0.500 -0.500[/quote]
如果你没有选择任何的component则只会得到所选物体的名称
[quote]Object: pCubeShape1[/quote]
上面用到的是MItGeometry迭代器,它可以迭代物体的points/CV/vertices,包括mesh(多边形), nurbs surface, nurbs curve(曲线), subdivision surface(细分面) 以及 lattice(晶格)。那如果我们要迭代其它的component呢,如边、面等等。Maya里还有其它的迭代器,如MItMeshEdge、MItMeshPolygon、MItSurfaceCV等
在maya中创建一个多边形和一条曲线,分别或同时选择曲线的点、多边形的边、多边形的面,分别对它们执行下面的代码
# 创建选择列表
selList = om.MSelectionList()
# 获取当前所选物体
om.MGlobal.getActiveSelectionList(selList)
# 创建迭代器,对选择列表进行迭代
it = om.MItSelectionList(selList)
while not it.isDone():
dagPath = om.MDagPath() # 用来存储所选物体的路径
component = om.MObject() # 用来存储所选物体的组件的列表
# 返回一个transform或shape节点的路径到dagPath
# 返回物体的组件的列表,如果组件被选择的话
it.getDagPath(dagPath, component)
# 添加一个方法集来处理物体
fn = om.MFnDependencyNode(dagPath.node())
# 打印物体的名称
print "\nObject: %s" % fn.name()
# 如果组件的列表是有效的
if not component.isNull():
if component.apiType() == om.MFn.kMeshComponent:
itVertex = om.MItMeshVertex(dagPath, component)
while not itVertex.isDone():
# 获取它们的世界坐标
point = itVertex.position(om.MSpace.kWorld)
# 打印点的索引和坐标
print "\t%d) %.3f %.3f %.3f" % (itVertex.index(),
point.x, point.y, point.z)
itVertex.next()
elif component.apiType() == om.MFn.kCurveCVComponent:
itCurve = om.MItCurveCV(dagPath, component)
while not itCurve.isDone():
# 获取它们的世界坐标
point = itCurve.position(om.MSpace.kWorld)
# 打印点的索引和坐标
print "\t%d) %.3f %.3f %.3f" % (itCurve.index(),
point.x, point.y, point.z)
itCurve.next()
elif component.apiType() == om.MFn.kMeshEdgeComponent:
itEdge = om.MItMeshEdge(dagPath, component)
while not itEdge.isDone():
# 获取它们的世界坐标
point = itEdge.center(om.MSpace.kWorld)
# 打印点的索引和坐标
print "\t%d) %.3f %.3f %.3f %s" % (itEdge.index(),
point.x, point.y, point.z,
' smooth' if itEdge.isSmooth() else ' hard')
itEdge.next()
elif component.apiType() == om.MFn.kMeshPolygonComponent:
itPoly = om.MItMeshPolygon(dagPath, component)
while not itPoly.isDone():
# 获取它们的世界坐标
point = itPoly.center(om.MSpace.kWorld)
# 打印点的索引和坐标
print "\t%d) %.3f %.3f %.3f %s" % (itPoly.index(),
point.x, point.y, point.z,
' planar' if itPoly.isPlanar() else ' non-planar')
itPoly.next()
# 还有一大堆的component我就不一一列出了
it.next()
你会得到类似的结果
[quote]Object: pSphereShape1
381) 0.188 -0.969 -0.137 hard
401) 0.309 -0.921 -0.224 hard
421) 0.421 -0.850 -0.306 hard
441) 0.524 -0.758 -0.381 hard
461) 0.613 -0.647 -0.446 hard
481) 0.688 -0.521 -0.500 hard
501) 0.745 -0.382 -0.541 hard
521) 0.784 -0.233 -0.570 hard
541) 0.804 -0.078 -0.584 hard
561) 0.804 0.078 -0.584 hard
581) 0.784 0.233 -0.570 hard
601) 0.745 0.382 -0.541 hard
621) 0.688 0.521 -0.500 hard
641) 0.613 0.647 -0.446 hard
661) 0.524 0.758 -0.381 hard
681) 0.421 0.850 -0.306 hard
701) 0.309 0.921 -0.224 hard
721) 0.188 0.969 -0.137 hard
741) 0.063 -0.994 -0.046 hard
761) 0.063 0.994 -0.046 hard
Object: pTorusShape1
415) -2.374 -0.741 -2.727 smooth
435) -2.359 -0.594 -2.681 smooth
455) -2.331 -0.469 -2.595 smooth
475) -2.292 -0.378 -2.476 smooth
495) -2.247 -0.331 -2.336 smooth
515) -2.199 -0.331 -2.189 smooth
535) -2.154 -0.378 -2.050 smooth
555) -2.115 -0.469 -1.931 smooth
575) -2.087 -0.594 -1.844 smooth
595) -2.072 -0.741 -1.799 smooth
615) -2.072 -0.896 -1.799 smooth
635) -2.087 -1.043 -1.844 smooth
655) -2.115 -1.168 -1.931 smooth
675) -2.154 -1.258 -2.050 smooth
695) -2.199 -1.306 -2.189 smooth
715) -2.247 -1.306 -2.336 smooth
735) -2.292 -1.258 -2.476 smooth
755) -2.331 -1.168 -2.595 smooth
775) -2.359 -1.043 -2.681 smooth
795) -2.374 -0.896 -2.727 smooth
Object: pSphereShape1
137) 0.811 -0.380 0.413 planar
155) 0.435 -0.232 0.853 planar
156) 0.677 -0.232 0.677 planar
157) 0.853 -0.232 0.435 planar
174) 0.154 -0.078 0.970 planar
175) 0.446 -0.078 0.875 planar
176) 0.694 -0.078 0.694 planar
177) 0.875 -0.078 0.446 planar
193) -0.154 0.078 0.970 planar
194) 0.154 0.078 0.970 planar
195) 0.446 0.078 0.875 planar
196) 0.694 0.078 0.694 planar
197) 0.875 0.078 0.446 planar
212) -0.435 0.232 0.853 planar
213) -0.150 0.232 0.946 planar
214) 0.150 0.232 0.946 planar
215) 0.435 0.232 0.853 planar
216) 0.677 0.232 0.677 planar
217) 0.853 0.232 0.435 planar
231) -0.644 0.380 0.644 planar
232) -0.413 0.380 0.811 planar
233) -0.142 0.380 0.899 planar
234) 0.142 0.380 0.899 planar
235) 0.413 0.380 0.811 planar
236) 0.644 0.380 0.644 planar
237) 0.811 0.380 0.413 planar
251) -0.595 0.519 0.595 planar
252) -0.382 0.519 0.749 planar
253) -0.132 0.519 0.830 planar
254) 0.132 0.519 0.830 planar
255) 0.382 0.519 0.749 planar
256) 0.595 0.519 0.595 planar
257) 0.749 0.519 0.382 planar
271) -0.531 0.645 0.531 planar
272) -0.341 0.645 0.669 planar
273) -0.117 0.645 0.742 planar
274) 0.117 0.645 0.742 planar
275) 0.341 0.645 0.669 planar
276) 0.531 0.645 0.531 planar
292) -0.292 0.755 0.573 planar
293) -0.101 0.755 0.635 planar
294) 0.101 0.755 0.635 planar
295) 0.292 0.755 0.573 planar
312) -0.236 0.847 0.462 planar
313) -0.081 0.847 0.512 planar
314) 0.081 0.847 0.512 planar
Object: curveShape1
2) 6.073 0.000 -8.701
3) 11.819 0.000 -2.601
4) 10.852 0.000 9.138
5) 7.350 0.000 13.090
6) 3.782 0.000 14.419[/quote]