Skip to main content

Export from Java3D to .obj

5 replies [Last post]
Joined: 2007-02-24

Hello all

I'm writing a Java3D application that generates 3d fractals. I'd like to be able to export them to .obj file format (preferably). Is there any .obj file api for java? There is a lot of loaders for all kinds of files, maybe there is also some lib that'll help me with that.

Thanks a lot

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-12-04

I needed to be able to export 3D nodes in a file at OBJ format for Sweet Home 3D.
As this feature doesn't still exist in Java 3D, I decided to program the [url=]OBJWriter[/url] class, a subclass of FilterWriter JDK class. It's available under GNU GPL.

Very simple to use:
// Open the OBJ file "MyObject.obj"
OBJWriter writer = new OBJWriter("MyObject.obj");

// Write one node or more if you want
// ...

// Close file. This will create the MTL file and texture files in same directory

If you look for a real world example, look at exportToOBJ method in [url=][/url].

For your information, I discovered two bugs in [url=]com.sun.j3d.loaders.objectfile.ObjectFile[/url] during the programing of this class:

- the transparency specified by [i]d factor[/i] lines in MTL file is misinterpreted : from [url=]MTL specs[/url], [i]A factor of 1.0 is fully opaque[/i] and [i]A factor of 0.0 is fully dissolved[/i]. In Java 3D [url=]TransparencyAttributes[/url], an object is fully transparent when that value is 1. From [url=]com.sun.j3d.loaders.objectfile.ObjectFileMaterials[/url] source, these factors are the same (no operation is done on transparencyLevel field).

- the texture coordinates read in OBJ file aren't correctly handled, when the number of vt (text coords) doesn't match the number of vertices. This is a very annoying bug, because some OBJ files built with other tools such as Blender or AOI may not render correctly with ObjectFile loader (vertices are always correct but text coords may be incorrect).

One last thing : it would be nice to support lines starting with "o" in ObjectFile loader because these lines appear in Blender OBJ export tool. When you change these lines by "g" lines, Java 3D seems to handle them correctly (this is the trick I used in the class OPrefixToGPrefixFilterInputStream at end of [url=]ModelManager[/url] ;) ).

[2009-04-01 Updated links]

Joined: 2010-12-27

I'd like to save java3d object to .obj ,but I can't do it.
I read your write but I can't understand it,I try to visit your link but it doesn't works.Can you help me please if it is possible.
Thanks a lot,

Joined: 2004-12-04 CVS has been down for a few days, but the com.eteks.sweethome3d.j3d.OBJWriter class can be downloaded from Sweet Home 3D source code too.

Joined: 2003-06-10

I am not aware of many savers to known file format, or even at all.
Do you need to save geometry only or also surfaces?

Joined: 2007-02-24

> geometry only or also surfaces?
What did you mean by that? I need to save 3d shapes, like lots of cylinders for example.

I know, it's not a common thing to save geometry from Java3D to a file. But I was thinking there maybe is some library for easy .obj (or .3ds, or something else that most 3d packages understand) manipulation. I'd hate to read .obj file's reference and then work on byte stream...

Thanks for your replay!