Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5938

GHPython mesh deconstruction

$
0
0

@Ben4 wrote:

Hi,

I’m using ghpython to extract the faces of a mesh, where each face becomes its own mesh. I found a component that did this in c#, but wanted to see if I could write the same thing in python. here is the resulting script, which works, but seems like it could be done much more efficiently. Any pointers much appreciated. I’ve done a lot of coding, but I’m new to coding within rhino/grasshopper.

input M : single Mesh
output FaceMeshes : list of Meshes representing the faces of the input mesh

import rhinoscriptsyntax as rs

def dePoint( p ):
  xyz = []
  xyz.append(p.X)
  xyz.append(p.Y)
  xyz.append(p.Z)
  return xyz

def meshExplode( m ):
  exploded = []
  for mf in m.Faces:
    v = []
    fv = []
    v.append(dePoint(m.Vertices[mf.A]))
    v.append(dePoint(m.Vertices[mf.B]))
    v.append(dePoint(m.Vertices[mf.C]))
    if mf.IsQuad: 
      v.append(dePoint(m.Vertices[mf.D]))
      fv.append( (0,1,2,3) )
    else:
      fv.append( (0,1,2) )
    exploded.append( rs.AddMesh( v, fv ) )
  return exploded

FaceMeshes = meshExplode( M )

in particular it seems odd that I had to write a function to extract the XYZ coords of the original mesh vertices, but if I dont and just try:

v.append(m.Vertices[mf.A])

When I then call rs.AddMesh, I get the error:
Runtime error (TypeErrorException): ‘Point3f’ object is not iterable

Also, the python script as I’ve written it seems to take about 10 times as long to execute as the C# script it was based on, which didnt seem have issues just passing the vertexes from the existing mesh to the new ones. The original C# script was written by Andrew Heumann. I’ve posted it below for reference:

List<Mesh> meshesOut = new List<Mesh>();
foreach(MeshFace mf in M.Faces){
  Mesh meshToAdd = new Mesh();
  meshToAdd.Vertices.Add(M.Vertices[mf.A]);
  meshToAdd.Vertices.Add(M.Vertices[mf.B]);
  meshToAdd.Vertices.Add(M.Vertices[mf.C]);
  if(mf.IsQuad){
    meshToAdd.Vertices.Add(M.Vertices[mf.D]);
    meshToAdd.Faces.AddFace(new MeshFace(0, 1, 2, 3));
  } else {
    meshToAdd.Faces.AddFace(new MeshFace(0, 1, 2));
  }
  meshesOut.Add(meshToAdd);
}
A = meshesOut;

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 5938

Trending Articles