迭代所选的组件(component)

我们已经知道如何对物体进行选择,但如果对象是component呢,我们怎么去使用api来选择点、边、面等等的component呢?
在场景中创建一个多边形物体,然后选择它的点、边、面或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]
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 `v-for` 指令来循环渲染子组件。在父组件模板中,使用 `v-for` 指令迭代一个数组,并使用子组件的标签名来渲染子组件。 例如,假设我们有一个名为 `ChildComponent` 的子组件,我们可以在父组件模板中使用以下代码来循环渲染多个子组件: ```html <template> <div> <child-component v-for="item in items" :key="item.id" :data="item"></child-component> </div> </template> ``` 在上面的代码中,我们使用 `v-for` 指令迭代 `items` 数组并将其绑定到子组件的 `data` 属性中。`key` 属性是必需的,以确保每个子组件都有一个唯一的标识符,以便 Vue 可以正确地更新它们。 在子组件中,我们可以使用 `props` 来接收从父组件传递的数据。例如: ```html <template> <div> <p>{{ data.name }}</p> <p>{{ data.age }}</p> </div> </template> <script> export default { props: { data: { type: Object, required: true } } } </script> ``` 在上面的代码中,我们定义了一个名为 `data` 的 prop,类型为对象,并将其标记为必需的。在子组件模板中,我们可以使用 `data` 对象中的属性来显示数据。 ### 回答2: Vue3中,我们可以使用v-for指令在父组件中循环创建子组件。 首先,我们需要在父组件中声明一个数据对象,用于存储子组件的数据。例如,我们可以在父组件中定义一个数组`childComponents`来保存子组件的数据。 然后,在父组件的模板中,使用`v-for`指令来循环创建子组件。指令的形式为`v-for="item in childComponents"`,其中`item`是一个临时的变量名,代表循环过程中的每个子组件。 接下来,我们需要在循环中,使用`<child-component />`来创建子组件。例如,如果我们有一个名为`ChildComponent`的子组件,我们可以在循环中使用`<child-component :data="item" />`来创建子组件,并将循环中的每个子组件的数据传递给子组件的`data`属性。 最后,我们需要在父组件中的data选项中初始化`childComponents`数组,用于存储需要循环创建的子组件的数据。例如,我们可以将`childComponents`初始化为一个包含多个子组件数据的数组。 总之,Vue3中父组件循环子组件的步骤为: 1. 在父组件中声明一个数据对象来存储子组件的数据。 2. 在父组件模板中使用`v-for`指令循环创建子组件。 3. 在循环中使用`<child-component />`来创建子组件,并传递子组件的数据。 4. 在父组件的data选项中初始化子组件的数据数组。 以上就是使用Vue3进行父组件循环子组件的方法,希望对你有所帮助。 ### 回答3: 在Vue 3中,父组件循环子组件的方法可以使用`v-for`指令。`v-for`指令是在父组件的模板中使用的,用于遍历一个数组或对象,并将子组件渲染多次。 首先,我们在父组件的模板中使用`v-for`指令来循环子组件。假设我们有一个名为`items`的数组,包含了子组件的数据。可以像这样循环子组件: ``` <template> <div> <child-component v-for="item in items" :key="item.id" :data="item"></child-component> </div> </template> ``` 上述代码中,我们通过`v-for`指令遍历了`items`数组,并将每个数组元素赋值给`item`,然后在`child-component`标签上绑定了`:data`属性,将`item`对象作为数据传递给子组件。 接下来,我们需要在子组件中接收和使用父组件传递的数据。在子组件的模板中,我们可以通过`props`接收父组件传递的数据。例如,我们在子组件中定义一个名为`data`的`prop`,用于接收父组件传递的数据: ``` <template> <div> <p>{{ data }}</p> </div> </template> <script> export default { props: ['data'] } </script> ``` 在上述代码中,子组件通过`props`属性定义了一个名为`data`的属性,用于接收父组件传递的数据。然后,在模板中使用`{{ data }}`输出接收到的数据。 综上所述,我们可以通过在父组件中使用`v-for`指令来循环子组件,并在子组件中使用`props`接收和使用父组件传递的数据。这样就实现了父组件循环子组件的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值