Problem in exporting PQ Torus mesh as an obj file

I used this code to export mesh as an obj file:

    PQTorus mesh = new PQTorus(3, 8, 2f, 1f, 32, 32);

    Geometry geom = new Geometry("PQTorus", mesh);

    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Blue);
    geom.setMaterial(mat);

    rootNode.attachChild(geom);

    int size = mesh.getTriangleCount();

    StringBuilder str = new StringBuilder();

    VertexBuffer pvb = mesh.getBuffer(VertexBuffer.Type.Position);
    FloatBuffer positions = (FloatBuffer) pvb.getData();
    Vector3f[] buf = BufferUtils.getVector3Array(positions);

    str.append("o PQTorus \n");

    for (Vector3f v : buf) {

        str.append("v\t").append(v.x).append("\t").append(v.y).append("\t").append(v.z).append("\n");
    }

    for (int i = 0; i < size; i++) {

        int[] indices = new int[3];
        mesh.getTriangle(i, indices);

        str.append("f\t").append(indices[0] + 1).append("\t").append(indices[1] + 1).append("\t").append(indices[2] + 1).append("\n");

    }

    System.out.println(str.toString());

    try {
        String path = "/path/to/PQTorus.obj/file";

        Files.write(Paths.get(path), str.toString().getBytes());
    } catch (IOException ex) {
        System.out.println(ex);
    }

and the result after importing to blender is :

I think there is a problem in shape exporting code and the pipes should not be connected together by a face.
and also there are some hole in center of pipes.
how can I fix it and fill second problem?

You can try this. It looks like it will give you an idea what you should be doing. Haven’t ran the code but it looks ok at a glance.

Maybe look at this also,

Best thing to do is start with a simple object with known vertices so you can figure out what your doing.

Basically create something simple in blender and export it as obj so you can open up the file in a text editor and compare it to what your are doing. Make adjustments to your script and soon your can write any size obj you want.

Edit: Crap, guess it would help to include the format stuff…
https://www.cs.cmu.edu/~mbz/personal/graphics/obj.html

Also I have noticed jmePhonon has a way to export scene as obj in case it helps

Have not test it myself.

1 Like