Skip to main content

Export from Java3D to .obj

5 replies [Last post]
paterczm
Offline
Joined: 2007-02-24
Points: 0

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
Marek

Reply viewing options

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

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=http://sweethome3d.cvs.sourceforge.net/sweethome3d/SweetHome3D/src/com/eteks/sweethome3d/j3d/OBJWriter.java?view=markup]OBJWriter[/url] class, a subclass of FilterWriter JDK class. It's available under GNU GPL.

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

// Write one node or more if you want
writer.writeNode(java3DNode1);
writer.writeNode(java3DNode2);
// ...

// Close file. This will create the MTL file and texture files in same directory
writer.close();
[/code]

If you look for a real world example, look at exportToOBJ method in [url= http://sweethome3d.cvs.sourceforge.net/viewvc/sweethome3d/SweetHome3D/src/com/eteks/sweethome3d/swing/HomePane.java?view=markup]HomePane.java[/url].

For your information, I discovered two bugs in [url=http://download.java.net/media/java3d/javadoc/1.5.2/com/sun/j3d/loaders/objectfile/ObjectFile.html]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=http://local.wasp.uwa.edu.au/~pbourke/dataformats/mtl/]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=http://download.java.net/media/java3d/javadoc/1.5.2/javax/media/j3d/TransparencyAttributes.html#setTransparency(float)]TransparencyAttributes[/url], an object is fully transparent when that value is 1. From [url=https://j3d-core-utils.dev.java.net/source/browse/j3d-core-utils/src/classes/share/com/sun/j3d/loaders/objectfile/ObjectFileMaterials.java?rev=1.6&view=markup]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=http://sweethome3d.cvs.sourceforge.net/sweethome3d/SweetHome3D/src/com/eteks/sweethome3d/swing/ModelManager.java?view=markup]ModelManager[/url] ;) ).

[2009-04-01 Updated links]

timothy99
Offline
Joined: 2010-12-27
Points: 0

Hi!
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,

puybaret
Offline
Joined: 2004-12-04
Points: 0

SourceForge.net 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.

pepe
Offline
Joined: 2003-06-10
Points: 0

Hello.
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?

paterczm
Offline
Joined: 2007-02-24
Points: 0

> 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!