Search code examples
openscad

Why is my "waterproof" polyhedron causing "WARNING: Object may not be a valid 2-manifold and may need repair!"?


In the script

difference() {
    polyhedron(
        points=[[0,0,0],
            [2,0,0],
            [2,1,0],
            [0,1,0],
            [0,0,2],
            [0,1,2]],
        faces=[[0,1,2,3],
            [5,4,1,2],
            [5,4,0,3],
            [0,1,4],
            [2,3,5]]);
    cube([1,1,1]);
};

the polyhedron alone works fine (is rendered without warnings), but adding the cube above causes the warning WARNING: Object may not be a valid 2-manifold and may need repair! to be logged and the output to only render some parts of some surfaces.

I'm using OpenSCAD 2015.03-1 on Ubuntu 16.04.


Solution

  • This is because your polyhedron has some faces pointing into the wrong direction, causing issues when calculating the difference().

    See the Manual and FAQ for details.

    Changing the winding order of the affected polygons fixes the polyhedron:

     difference() {
         polyhedron(
             points=[[0,0,0],
                 [2,0,0],
                 [2,1,0],
                 [0,1,0],
                 [0,0,2],
                 [0,1,2]],
             faces=[[0,1,2,3],
                 [2,1,4,5],
                 [5,4,0,3],
                 [0,4,1],
                 [2,5,3]]);
         cube([1,1,1]);
     };
    

    The difference is still non-manifold as cutting the cube results in 2 prism shaped objects just touching at one edge. That's also by definition not 2-manifold, so the warning remains.

    Depending on how the exported model is supposed to be used, you could choose to ignore this warning and hope the tool processing the 3d model can handle that.

    To remove the issue, for example the cube could be made a bit smaller like cube([1, 1, 0.999]).

    An unrelated, but still useful strategy for preventing issues later on is to always make the cutting object a bit larger to ensure that no very thin planes remain, e.g. use cube([2,3,1.999], center = true). That will also remove the display artifacts in preview mode.