Export mesh to .obj

How it is possible to save a mesh to a .obj file?

My problem is I’ve a easy model. I have only the vertices, indices and normals. No lights, material, ambient and so on. If there is no exporter, then someone can help me writing an obj file. I have not so much knowledge about 3d programming.

1 Like

Did you create your mesh in Blender or in jMonkeyEngine? With Blender you can export your model in various formats. In jME just as an .j3o file.

1 Like

No my image information is coming from a 3d sensor. I display this data in an application with jmonkey. Now the user should have the possibility to export this mesh in different formats. I hoped there is an out of the box possiblity to save the mesh in file else I have to write it by hand.

My data:

Vector3f[] vertices;
int[] indices; // describing the triangles
// vertices[indices[0]]; vertices[indices[1]]; vertices[indices[2]] is the first triangle

1 Like

Sounds very cool what you are doing!

I don’t know of any built in exporter except of the binary exporter (for j3o files).

For what do you need to export it in another format? Do you want to edit it in blender or something like that? I am just interested :blush:

1 Like

If it should be of use outside of the Engine you Need to write your own exporter since this is no typical Application and hence probably hasnt been done yet.
Have you searched the forums for an obj exporter though?

1 Like

As the others said, you have to write it (or use one from an external library), it’s very very simple though.

It’s a plain/text file, first you write every position one per line in this way:

v [X] [Y] [Z] 

you have to replace [X] [Y] [Z] eg.

v 0.0 0.0 0.0 
v 1.0 0.0 0.0 
v 0.0 1.0 0.0 

then you write the uv

vt [X] [Y]

then the normals

vn [X] [Y] [Z] 

and then the indexes for every face

f A B C

And that’s all, you have your basic exporter.

Full references here: https://www.cs.cmu.edu/~mbz/personal/graphics/obj.html

4 Likes

If anyone implements OBJ export, I’d like to include it as a feature in my Maud editor.

3 Likes

Much thanks for your help. Here is my solution:

		@Override
	public void exportSelectedData(File outputFile, Vector3f[] vertices, Integer[] indices) throws IOException {
		FileWriter writer = new FileWriter(outputFile);
		for (int i = 0; i < indices.length; i += 3) {
			Vector3f v1 = new Vector3f(vertices[i]);
			Vector3f v2 = new Vector3f(vertices[i + 1]);
			Vector3f v3 = new Vector3f(vertices[i + 2]);

			Vector3f normal = computeNormal(v1, v2, v3);

			writer.write(String.format("v %f %f %f\n", v1.x, v1.y, v1.z));
			writer.write(String.format("v %f %f %f\n", v2.x, v2.y, v2.z));
			writer.write(String.format("v %f %f %f\n", v3.x, v3.y, v3.z));

			writer.write(String.format("vn %f %f %f\n", normal.x, normal.y, normal.z));

			int v = i + 1, vn = (i / 3) + 1;
			writer.write(String.format("f %d//%d %d//%d %d//%d\n", v, vn, v + 1, vn, v + 2, vn));
		}
		writer.close();
	}
5 Likes

Hey guys.

I understand the topic is old, but it’s a important for me.

I made a very large scene (4000х4000) with nodes, mesh and terrain. All meshs parsed from binary files contain very old models, the editor of which does not exist. It was hard work, but I got it. Thx for jmonkey)), only with this instrument i made it.

I want to export meshes with their current vertes coordinates to a 3D editor.

Can anyone help in the direction?!

scene view

Iterate over the scene. Get the mesh data. Write it to a format of your choosing. OBJ is the easiest one (see previous comments in this thread).