Purple_Ad
Purple_Ad

Reputation: 87

Convert mesh to polygon in python

I have a mesh and want to convert it into a polygon. I did some google search and found most answer on converting polygon to mesh. I want to do the opposite.Is there any library or any idea on how to achieve it

Upvotes: 1

Views: 725

Answers (1)

Musabbir Arrafi
Musabbir Arrafi

Reputation: 1885

Well you can use the Python FME library to take input as a mesh object and return a polygon

import fmeobjects

class MeshToPolygons(object):
    def input(self, feature):
        mesh = feature.getGeometry()
        if isinstance(mesh, fmeobjects.FMEMesh):
            vertices = mesh.getVertices()
            for part in mesh:
                indices = part.getVertexIndices()
                if indices:
                    boundary = fmeobjects.FMELine([vertices[i] for i in indices])
                    feature.setGeometry(fmeobjects.FMEPolygon(boundary))
                    self.pyoutput(feature)

If the above doesn't help, you can also check out the python TriMesh library. You could load the object as mesh using

trimesh.load(file_obj, file_type=None, resolver=None, force=None, **kwargs) 
   Return type: Trimesh, Path2D, Path3D, Scene
   Returns: geometry – Loaded geometry as trimesh classes

Now convert the mesh object into a sequence of connected points. After that you can use

trimesh.path.polygons.paths_to_polygons(paths, scale=None)
   Return type: (p,) list
   Returns: polys – Filled with Polygon or None

Hope it helps

Upvotes: 1

Related Questions