jME SceneLoading with OgreImporter (Part II)

<– back to part one

exporting the scene

The export-process is split up into two parts. Export the scene and export the meshes.

(If you didn’t install the ogre-blender-scripts until now do so and restart blender)

Go to File->Export->OGRE SCENE.

In the OgreSceneExporter press the All button in order to select all elements that should be exported. Also select “Fix Up Axis To Y“. Blender’s Up-Axis is Z in jME the Up-Axis is Y what has the effect that the rotations are wrong. With that selected all vertices are recalcualted with the swapped axis.

Select a directory where to store the ogre-scene. Don’t forget to name a filename. Here “ogre.scene

Then press Export….

Now we have written a file to the directory that knows where to posiiton all elements we selected. But the elements itselfs have to be exported as well. For that select in “object-mode” Cube and Plane. Go to File->Export->Ogre Meshes

In OgreMeshesExporter in the Selected choicebox choose CUBE1 and Press Add in the Skeleton Window. That adds the Cube-Animation also to be exported. The You also see the Name under that you can later use that animation “Action“.

Choose an export-directory (the same where you just stored the ogre.scene-file), select Fix Up Axis To Y and Export.

export meshes

export meshes

Now we have 5 files exported:

exported files

exported files

ogre.scene - The scene-file that knows how to arrange the meshes

Plane.001.mesh.xml (this maybe is at your just Plane.mesh.xml) and CUBE1.mesh.xml that is a XML-File with the mesh-data.

CUBE1.skeleton - contains the armature-data and the animation-data

Scene.material - data about the materials

Here is an archive with all the files exported.

That’s it for the blender side of life. Now let’s start eclipse.

importing the scene

Start eclipse and create a new java-project. Let’s name it OgreSceneTest. I assume that you already installed jME2- and the ogreimporter-Project. Add the projects to the build-path or the libraries.

Let’s create a package “ogre” and add a new Class “SceneLoaderTest”. Add another package ogre.data and copy all exported file there. (Don’t forget to refresh the project so that eclipse add the externally added files)

Replace the “SceneLoaderTest” by this and read the comments.

package ogre;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import com.jme.app.SimpleGame;
import com.jme.scene.Node;
import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import com.jmex.model.ogrexml.SceneLoader;
import com.jmex.model.ogrexml.anim.MeshAnimationController;

public class SceneLoaderTest extends SimpleGame
{
		private Node sceneNode;

		@Override
		protected void simpleInitGame()
		{
	        try
	        {
	        	// set the path to the textures
	        	ResourceLocatorTool.addResourceLocator(
	                    ResourceLocatorTool.TYPE_TEXTURE,
	                    new SimpleResourceLocator(SceneLoaderTest.class
	                            .getClassLoader().getResource(
	                            		"ogre/data/")));
	        	// set the path to the models
	            ResourceLocatorTool.addResourceLocator(
	                    ResourceLocatorTool.TYPE_MODEL,
	                    new SimpleResourceLocator(SceneLoaderTest.class
	                            .getClassLoader().getResource(
	                            		"ogre/data/")));
	        }
	        catch (URISyntaxException e1)
	        {
	        	e1.printStackTrace();
	        }
			SceneLoader scene = new SceneLoader();
			try
			{
				// specify here the Scene-File
				URL sceneURL = SceneLoaderTest.class.getClassLoader().getResource("ogre/data/ogre.scene");
				scene.load(sceneURL.openStream());
				sceneNode = (Node)scene.getScene();
				rootNode.attachChild(sceneNode);
				// go down the Node-Path to the Mesh where the AnimationController is attached
				Node ogM = (Node)sceneNode.getChild("CUBE1");
				MeshAnimationController cont = (MeshAnimationController)ogM.getController(0);
				// call the Action you just made
				cont.setAnimation("Action");
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		public static void main(String[] args)
		{
			new SceneLoaderTest().start();
		}
}

Run it and if everything went the right way you will see the scene. Watchout! As the plane is on the xz-plane navigate with mouse and awsd in order to look around in the scene and then you will see the plane also.

Here is an archive (ogrescenetest.zip) with the eclipse-project including the models. Maybe you have to modify the build-path to your jme2- and ogreimporter-installation.

Actually I still don’t have much experiences with the ogreScene. I found it today and after creating the first scene I wrote this tutorial. Don’t know what happens when the scenes are getting more complex.

Have fun with it and 10000millions of “Thank yous” to Momoko_Fan

I won’t give up my wish of the one importer that rules em all….

<– back to part one