How to delete a mesh face in Python script

I have a mesh with 2.2M vertices. I draw a closed curve around a region of interest and collect the curve points and the mesh vertices inside the curve in order to construct a patch mesh. If the curve has concave sides, Rhino adds mesh faces to prevent the patch from having concave sides. I need to remove these in order to get the 3D area of the patch over the closed curve. The way I do this now is to compute the centroid of each mesh face (typically 330K in the region of interest) and test if the centroid is inside the closed curve. I keep the faces found to be inside the curve and construct a new mesh. This takes some time. A faster way would be to just delete the unwanted mesh faces if this could be done inside the Python script. I have not found a Rhinoscriptsyntax command or Rhino Common command that will do this. It can be done with
rs.Command(‘NoEcho -DeleteMeshFaces {},{} Enter’.format(ptc.X,ptc.Y))
where ptc is a face centroid but after it finishes the mesh looks unchanged. If I manually execute the command everything works.

What I really want is a rs.DeleteMeshFace or Rhino Common Mesh.DeleteMeshFace method to get this job done as working with rs.Command is always hazardous (needs to be the right viewport, the viewport needs to already exist, etc.).

Any ideas?

Regards,
Terry.

This is probably what you’re after:

http://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Collections_MeshFaceList_DeleteFaces.htm

You access the method on the mesh i.e nameOfMesh.Faces.DeleteFaces(faceIndicesToDelete).

2 Likes

Anders,

Thanks for the reference. This was a big help.

Regards,
Terry.

1 Like

Is there a Python version for this?

The RhinoCommon method linked above can be accessed via Rhino/Python. There is no rhinoscriptsyntax method available for this.

Thank you Helvetosaur, you mean the c++ command can be run from Python? Do you have a reference where I can check how to do this? Thanks in advance,

No, I mean the method referenced above can be called directly by Python (running inside Rhino).
Use import Rhino to import the entire RhinoCommon module and all of its classes and subclasses.

Here is a quick example:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def TestDeleteMeshFaces():
    mesh_id=rs.GetObject("Select mesh to process",32,preselect=True)
    if not mesh_id: return
    mesh=rs.coercemesh(mesh_id)
    faces=mesh.Faces
    faces.DeleteFaces([0,2,6,8]) #this line calls the RhinoCommon method
    sc.doc.Objects.Replace(mesh_id,mesh)
    sc.doc.Views.Redraw()
TestDeleteMeshFaces()

Try it on this:
9FaceMesh.3dm (2.1 MB)

1 Like

Fantastic! It seems like it would be straghtforward to select mesh faces outside a bounding box and delete those, right? I´d like to do this and then run this script on 20 or so files in batch hopefully with no GUI so my lack of GPU power doesn´t kill me. Thanks a lot,

Shouldn’t be too hard, you will need to create the box, then iterate through the mesh face list and get each face, for each face get all of the associated vertices, and test each for “insideness”.

Beware, mesh vertices are Point3f objects, you will need to convert them to Point3d for the test.
If even one of the vertex points is not inside, you can add the mesh face index to a list and continue to the next face index. Then delete the list of indices found to be outside.