MeshLib icon indicating copy to clipboard operation
MeshLib copied to clipboard

mrmeshpy.boolean can not union these meshes in Python

Open 6ther opened this issue 5 months ago • 3 comments

model0.obj.txt

model1.obj.txt

model2.obj.txt

model3.obj.txt

I union these meshes in Python, but I do not work it out.

6ther avatar Aug 13 '25 01:08 6ther

Hello!

The issue is that model3 is not closed:

Image

You can follow this code to unite them:

from meshlib import mrmeshpy as mm

models = []
models.append( mm.loadMesh("model0.obj") )
models.append( mm.loadMesh("model1.obj") )
models.append( mm.loadMesh("model2.obj") )
models.append( mm.loadMesh("model3.obj") )

ptrVec = mm.vectorConstMeshPtr()
ptrVec.resize(len(models))

for i in range(len(models)):
    mm.MeshBuilder.uniteCloseVertices(models[i],0.0)
    ptrVec[i] = models[i]

unitedMesh = mm.uniteManyMeshes(ptrVec)
mm.saveMesh(unitedMesh,"united.obj")

This should work for this case, but in general meshes like these have a lot of coincident planes and model1 has self-intersections

Image

so it is not guaranteed to be successfully united in general

Grantim avatar Aug 13 '25 07:08 Grantim

    import meshlib.mrmeshpy as mrmeshpy

    mrmesh1 = mrmeshpy.loadObj('model0.obj')
    mrmesh2 = mrmeshpy.loadObj('model0.obj')
    mrmesh3 = mrmeshpy.loadObj('model0.obj')
    mrmesh4 = mrmeshpy.loadObj('model0.obj')
    models = []
    models.append(mrmesh1)
    models.append(mrmesh2)
    models.append(mrmesh3)
    models.append(mrmesh4)
    mesh_len = len(models)
    rst_mrmesh = models[0]
    for i in range(1, mesh_len):
        mrmesh = models[i]
        hole_edges = mrmesh.topology.findHoleRepresentiveEdges()
        params = mrmeshpy.FillHoleParams()
        params.metric = mrmeshpy.getUniversalMetric(mrmesh)
        mrmeshpy.fillHoles(mrmesh, hole_edges, params)
        union = mrmeshpy.boolean(mrmesh, rst_mrmesh, mrmeshpy.BooleanOperation.Union)
        rst_mrmesh = union.mesh
    mrmeshpy.saveMesh(rst_mrmesh , "result_united.obj")

my code like this. and i do not know that difference between uniteManyMeshes and mrmeshpy.boolean

6ther avatar Aug 13 '25 08:08 6ther

Using fillHoles is not recommended for this case, because holes are fully degenerated, and filling them with new triangles will produce degenerated triangles, which might be treated as self-intersections in boolean. So it is better to use uniteCloseVertices which does not produce new triangles but unites degenerated boundaries with duplicated vertices (exactly what is present in this case)


difference between uniteManyMeshes and mrmeshpy.boolean

In general uniteManyMeshes is optimized version of boolean for several objects, calling boolean instead is OK.

Grantim avatar Aug 13 '25 08:08 Grantim