python - 如何在 blender python 中获取选定顶点的排序数组

标签 python blender

我在 blender 中选择了一个顶点循环,我想将每个顶点逆时针移动到其相邻顶点的位置。

def marchVerticesACW():
    ob = bpy.context.scene.objects.active
    mesh = ob.data
    verts = [i.index for i in bpy.context.active_object.data.vertices if i.select]
    covar=mesh.vertices[verts[0]].co
    cx=covar.x
    cy=covar.y
    cz=covar.z
    for x in range(len(verts)-1):
        mesh.vertices[verts[x]].co=mesh.vertices[verts[x+1]].co
    mesh.vertices[verts[len(verts)-1]].co.x=cx
    mesh.vertices[verts[len(verts)-1]].co.y=cy
    mesh.vertices[verts[len(verts)-1]].co.z=cz

如果顶点碰巧以正确的顺序出现在列表中,但它们并不总是有效。如果顺序不对,有没有办法对列表进行排序?

最佳答案

我解决了这个问题,基于循环中的每个顶点都通过一条边连接到下一个顶点的事实,所以从任何顶点开始,我只搜索选定的边,寻找在一端具有该顶点的第一个边,使该边上的下一个顶点的注释,从集合中删除边,然后查找在一端具有新顶点的边。它只适用于闭环,我没有进行任何错误处理/检查,但认为基本思想可能对其他人有帮助

import bpy
import bmesh


def OrderSelectedVerts(_bm):
    edge=_edges[0]
    startvert=edge.verts[0]
    _edges.remove(edge)
    _orderedVerts=[]
    _orderedVerts.append(startvert)
    while len(_edges)>0:
        startvert=GetNextVert(startvert)
        if startvert is not None:
            _orderedVerts.append(startvert)
    return _orderedVerts

def GetNextVert(sv):
    for e in _edges:
        if e.verts[0]==sv or e.verts[1]==sv:
            if e.verts[0]==sv:
                d=e.verts[1]
            else:
                d=e.verts[0]
            _edges.remove(e)
            return d


# Get the active mesh
obj = bpy.context.edit_object
me = obj.data


# Get a BMesh representation
bm = bmesh.from_edit_mesh(me)
_edges=[i for i in bm.edges if i.select]

#Get the selected verts in order
orderedVerts=OrderSelectedVerts(bm)

#Use the ordered verts


#Update the bmesh
bmesh.update_edit_mesh(me, True)

关于python - 如何在 blender python 中获取选定顶点的排序数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27766655/

相关文章:

iphone - 如何在 iOS 的 Cocos2D 游戏中创建新的模型和纹理

python - 使用 Hadoop 的 Blender 静止图像渲染

python - Opencv或Numpy-有效替换图像中的像素列表

python - Mac Swampy(Python学习模块)安装

python - 将列表项转换为定义的数据类型

textures - 纹理是 "inverted"

Python:socket.recvfrom() 不返回地址

python - 获取最终重定向的 URL

c - 使用 openGL ES 1.1 渲染 .h Blender 导出到 iPhone

java - 我如何在 Java 中使用 Blender 模型?