使用RhinoPython向现有网格添加面

时间:2013-04-18 20:16:49

标签: python rhino3d

我正在使用RhinoPython和RhinoCommon尝试将面添加到现有网格中。一切似乎都有效,但创建的面部与我选择的点不在同一个位置。有人可以解释为什么选择点的索引号似乎不正确吗?

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext
import rhinoscript.utility as rhutil

def AddVertices(me):
    """Add face to a mesh"""
    mesh=rhutil.coercemesh(me)

    #select the vertices
    go=Rhino.Input.Custom.GetObject()
    go.GeometryFilter=Rhino.DocObjects.ObjectType.MeshVertex
    go.SetCommandPrompt("Get mesh vertex")
    go.GetMultiple(3,4)
    objrefs = go.Objects()
    point=[item.GeometryComponentIndex.Index for item in objrefs]
    go.Dispose()

    if len(point)==4:
        mesh.Faces.AddFace(point[0], point[1], point[2], point[3])
    else:
        mesh.Faces.AddFace(point[0], point[1], point[2])
    #replace mesh delete point
    scriptcontext.doc.Objects.Replace(me,mesh)
    mesh.Dispose()
    scriptcontext.doc.Views.Redraw()

if( __name__ == "__main__" ):
    me=rs.GetObject("Select a mesh to add face")
    AddVertices(me)

1 个答案:

答案 0 :(得分:3)

这是因为从“get”操作返回的是MeshTopologyVertex而不是MeshVertex。 MeshTopologyVertex表示恰好在空间中共享相同位置的一个或多个网格顶点。这是因为每个顶点都有一个顶点法线。想想网箱中的一个角落。该角有三个具有不同顶点法线的面,因此在该角有三个网格顶点但只有一个MeshTopologyVertex。我已经调整了脚本以使用顶点索引。

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext
import rhinoscript.utility as rhutil

def AddVertices(me):
    """Add face to a mesh"""
    mesh=rhutil.coercemesh(me)

    #select the vertices
    go=Rhino.Input.Custom.GetObject()
    go.GeometryFilter=Rhino.DocObjects.ObjectType.MeshVertex
    go.SetCommandPrompt("Get mesh vertex")
    go.GetMultiple(3,4)
    objrefs = go.Objects()
    topology_indices=[item.GeometryComponentIndex.Index for item in objrefs]
    go.Dispose()

    point = []
    for index in topology_indices:
        # in many cases there are multiple vertices in the mesh
        # for a single topology vertex. Just pick the first one
        # in this sample, you will probably have to make a better
        # decision that this for your specific case
        vertex_indices = mesh.TopologyVertices.MeshVertexIndices(index)
        point.append(vertex_indices[0])

    if len(point)==4:
        mesh.Faces.AddFace(point[0], point[1], point[2], point[3])
    else:
        mesh.Faces.AddFace(point[0], point[1], point[2])
    #replace mesh delete point
    scriptcontext.doc.Objects.Replace(me,mesh)
    mesh.Dispose()
    scriptcontext.doc.Views.Redraw()

if( __name__ == "__main__" ):
    me=rs.GetObject("Select a mesh to add face")
    AddVertices(me)