Using Blender to Create Java3D Models

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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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!

Be the First to Share

    Recommendations

    • Tiny Speed Challenge

      Tiny Speed Challenge
    • Clocks Contest

      Clocks Contest
    • PCB Design Challenge

      PCB Design Challenge

    9 Discussions

    0
    admin
    admin

    10 years ago

    This is a great Instructable, but you need to add a main image of the final project to the intro step. Please do that and leave me a message when you have so that we can publish your work. Thanks!

    0
    ghostbust555
    ghostbust555

    Reply 10 years ago

    Ok I put the final picture on the intro.

    0
    catzova
    catzova

    Question 11 months ago on Step 3

    Hi there I was wondering can I use java plugin in blender for modelling thanks

    0
    Infinity Wolf Productions
    Infinity Wolf Productions

    Reply 2 years ago

    Also does it need to have a material in order to work.

    0
    SkylarAdams
    SkylarAdams

    4 years ago

    I still dont get it , could you now explain how to use the 3d object?

    0
    eggyeggyeggy
    eggyeggyeggy

    9 years ago on Introduction

    Yi]ou said that you just learned java a week ago... Where did you learn it/ how did you learn it? I cant find anything at all on how to program in java.

    0
    AlternateLives
    AlternateLives

    Reply 8 years ago on Introduction

    If you want to learn a bit of java:

    1.Get Minecraft, if you haven't done so already.
    2.Get MCP (Minecraft Coder Pack)
    3.Get ModLoader
    4.Look up on YouTube how to set it up, as well as various tutorials on how to mod certain aspects of the game.
    5.Make a mod.

    Follow these steps, and you will have learned some Java!