Introduction: Using Blender to Create Java3D Models

If you're a Java programmer you probably have wanted to program in 3D at some point. But how? Well you could use Java3D and slowly type in each point in a 3D polygon (tried it trust me its a bad idea), or you could use Blender (http://blender.org) a free and open source 3D modeling program along with a script called Blend2Java (http://sourceforge.net/projects/blend2java/). However the documentation on Blend2Java is almost nonexistant so thats why I sit here writing this.

Step 1: Download the Programs

You will need Blender (http://blender.org) and Blend2Java (http://sourceforge.net/projects/blend2java/). Ok then once you have them both start off by making a simple model (or just using the standard cube) in Blender.

Step 2: Export Your Model to XML

Once you have a model you would like to use in Blender open Edit Mode and hit the "a" key to select all points. In the bottom panel select text editor.

Open a new file in the text editor. Open the blend2java.py file that you downloaded. Run.
Ok your object should now be in .XML wherever you saved it (if you have and error message make shure you only have 1 material for the object). Rename the object (it will have a weird name).

Step 3: Import the Object to Java

Shape3D fred = null;
try {
XMLDecoder e = new XMLDecoder(new BufferedInputStream(new FileInputStream("c:/HandShape3D.xml"))); //your file name here
fred = (Shape3D) e.readObject();
e.close();
} catch(Exception e) {
e.printStackTrace();
}

ColoringAttributes at=new ColoringAttributes();
Appearance ap = new Appearance();
Color3f col = new Color3f(1.0f, 0.0f, 1.0f);
ColoringAttributes ca = new ColoringAttributes(col, ColoringAttributes.NICEST);
ap.setColoringAttributes(ca);
fred.setAppearance(ap);
obj.addChild(fred);

insert this into any 3d code or use the whole code here

import java.io.*;
import java.beans.XMLDecoder;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;
import java.util.Enumeration;

public class MouseBehaviorApp extends Applet {

public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
TransformGroup objTransform = new TransformGroup();
objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

objRoot.addChild(objTransform);
Shape3D fred = null;
try {
XMLDecoder e = new XMLDecoder(new BufferedInputStream(new FileInputStream("c:/HandShape3D.xml")));
fred = (Shape3D) e.readObject();
e.close();
} catch(Exception e) {
e.printStackTrace();
}

ColoringAttributes at=new ColoringAttributes();
Appearance ap = new Appearance();
Color3f col = new Color3f(1.0f, 0.0f, 1.0f);
ColoringAttributes ca = new ColoringAttributes(col, ColoringAttributes.NICEST);
ap.setColoringAttributes(ca);
fred.setAppearance(ap);
objTransform.addChild(fred);

MouseRotate myMouseRotate = new MouseRotate();
myMouseRotate.setTransformGroup(objTransform);
myMouseRotate.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMouseRotate);

MouseTranslate myMouseTranslate = new MouseTranslate();
myMouseTranslate.setTransformGroup(objTransform);
myMouseTranslate.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMouseTranslate);

MouseZoom myMouseZoom = new MouseZoom();
myMouseZoom.setTransformGroup(objTransform);
myMouseZoom.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMouseZoom);

objRoot.compile();

return objRoot;
}

public MouseBehaviorApp() {
setLayout(new BorderLayout());
Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}

public static void main(String[] args) {
Frame frame = new MainFrame(new MouseBehaviorApp(), 256, 256);
}
}

use this for a complete program

tada! you are done!
and then you say "now what"?
I have no idea! I just learned Java a month ago lol!