md5importer

Can anyone assist me on the importer i have i borrowed and slightly modified the unifiedModelLoader. It seems to load the character if i change the null statement in the md5mesh if statement to body and edit out the jbin conversion. However, I'm unable to properly load the textures or save a jbin can anyone help.



package com.tps1.RandomCharacter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import test.model.md5.Test;

import com.jme.scene.Node;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.AseToJme;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.Md2ToJme;
import com.jmex.model.converters.Md3ToJme;
import com.jmex.model.converters.MilkToJme;
import com.jmex.model.converters.ObjToJme;
import com.jmex.model.util.ModelLoader;
import com.model.md5.ModelNode;
import com.model.md5.importer.MD5Importer;
import com.tps1.aMain.Main;

public class UnifiedModelLoader extends Test{
   static /*
    *  This method opens a model in various format evaluating the extension
    *  In case in the same directory is already presents the same model in jbin format loads it
    *  Otherways load the model and save a jbin copy for the next time.
    * 
    *  Attention : in case the original model is changed you'll have to delete the jbin one the reload it.
    */      
   ModelNode body2;
   public static Node loadModel (String modelFile){
      Node         loadedModel   = null;
      FormatConverter      formatConverter = null;      
      ByteArrayOutputStream    BO       = new ByteArrayOutputStream();
      String         modelFormat    = modelFile.substring(modelFile.lastIndexOf(".") + 1, modelFile.length());
      String         modelBinary   = modelFile.substring(0, modelFile.lastIndexOf(".") + 1) + "jbin";
      URL         modelURL   = Main.class.getClassLoader().getResource(modelBinary);
      
      
      
      //verify the presence of the jbin model
      if ((modelURL == null)&&(!modelFormat.equals("md5mesh"))){
         
         modelURL      = Main.class.getClassLoader().getResource(modelFile);
         
         //evaluate the format
         if (modelFormat.equals("3ds")){
            formatConverter = new MaxToJme();
         } else if (modelFormat.equals("md2")){
            formatConverter = new Md2ToJme();
         } else if (modelFormat.equals("md3")){
            formatConverter = new Md3ToJme();
         } else if (modelFormat.equals("ms3d")){
            formatConverter = new MilkToJme();
         } else if (modelFormat.equals("ase")){
            formatConverter = new AseToJme();
         } else if (modelFormat.equals("obj")){
            formatConverter = new ObjToJme();
         }
         formatConverter.setProperty("mtllib", modelURL);
         
         try {
            formatConverter.convert(modelURL.openStream(), BO);
            loadedModel = (Node) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            
            //save the jbin format
            BinaryExporter.getInstance().save((Savable)loadedModel, new File(modelBinary));
         } catch (IOException e) {            
            e.printStackTrace();
            return null;
         }
         //Loads md5 model
      }else if (modelFormat.equals("md5mesh")){
         
         modelURL      = Main.class.getClassLoader().getResource(modelFile);
      
         try {
            MD5Importer.getInstance().loadMesh(modelURL, "ModelNode");
         } catch (IOException e) {
            e.printStackTrace();
         }
         ModelNode body = MD5Importer.getInstance().getModelNode();
         MD5Importer.getInstance().cleanup();         
         body.flagUpdate();
         
         formatConverter.setProperty("mtllib", modelURL);
         try {
            formatConverter.convert(modelURL.openStream(), BO);
            loadedModel = (Node) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            
            //save the jbin format
            BinaryExporter.getInstance().save((Savable)loadedModel, new File(modelBinary));
         } catch (IOException e) {            
            e.printStackTrace();
         }
         
         return null;            
         
       }else{
          
         try {
            //load the jbin format
            loadedModel = (Node) BinaryImporter.getInstance().load(modelURL.openStream());
         } catch (IOException e) {
            return null;
         }
      }
      
      return loadedModel;
   }

   @Override
   protected ModelNode loadModel() {
      // TODO Auto-generated method stub
      return body2;
   }

   @Override
   protected void setupGame() {
      // TODO Auto-generated method stub
      
   }

}



neakor's Test class presumably to load textures

package test.model.md5;

import java.net.URISyntaxException;

import com.jme.app.SimpleGame;
import com.jme.scene.Node;
import com.jme.util.resource.MultiFormatResourceLocator;
import com.jme.util.resource.ResourceLocatorTool;
import com.model.md5.ModelNode;
import com.model.md5.importer.MD5Importer;

public abstract class Test extends SimpleGame{

   @Override
   protected void simpleInitGame() {
      this.overrideTextureKey();
      Node node = this.loadModel();
      node.setLocalTranslation(0, -40, -300);
      this.rootNode.attachChild(node);
      this.setupGame();
      MD5Importer.getInstance().cleanup();
   }
   
   protected void overrideTextureKey() {
      try {
         MultiFormatResourceLocator locator = new MultiFormatResourceLocator(this.getClass().getClassLoader().getResource("test/model/md5/data/texture/"),
               new String[]{".tga", ".bmp", ".png", ".jpg", ".texture", ".jme"});
         ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, locator);
      } catch (URISyntaxException e) {
         e.printStackTrace();
      }
   }

   abstract protected ModelNode loadModel();
   
   abstract protected void setupGame();
}



this is everything i have for it HERE


thx anywayz i figured it out

Hi Bonechilla. It would be good if you post your solution. Sort of the purpose of the forum :wink:



TIA

quakedeus said:

Hi Bonechilla. It would be good if you post your solution. Sort of the purpose of the forum ;)
TIA


oh srry i didn't think it was important i simply hacked a couple of classes into one in order to fit my needs also i haven't really been able to clean up the classes since i've been reset up my O/Ses and rig

package com.tps1.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import com.jme.scene.Node;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.resource.MultiFormatResourceLocator;
import com.jme.util.resource.ResourceLocatorTool;
import com.jmex.model.converters.AseToJme;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.Md2ToJme;
import com.jmex.model.converters.Md3ToJme;
import com.jmex.model.converters.MilkToJme;
import com.jmex.model.converters.ObjToJme;
import com.jmex.model.util.ModelLoader;
import com.model.md5.JointAnimation;
import com.model.md5.ModelNode;
import com.model.md5.controller.JointController;
import com.model.md5.importer.MD5Importer;
import com.tps1.aMain.Main;

public class UnifiedModelLoader{
    /*
    *  This method opens a model in various format evaluating the extension
    *  In case in the same directory is already presents the same model in jbin format loads it
    *  Otherways load the model and save a jbin copy for the next time.
    * 
    *  Attention : in case the original model is changed you'll have to delete the jbin one the reload it.
    */      
   
   //Create a path to locate Textures
   static String texturepath ;
   /**
    * @description Loads a model file from a URL and saves it as a jbin for quicker loading
    * @param modelFile is the file path
    * @return loadedFile is the model created
    */
   public static Node loadModel (String modelFile){
      ModelNode         loadedModel   = null;
      FormatConverter      formatConverter = null;      
      ByteArrayOutputStream    BO       = new ByteArrayOutputStream();
      String         modelFormat    = modelFile.substring(modelFile.lastIndexOf(".") + 1, modelFile.length());
      String         modelBinary   = modelFile.substring(0, modelFile.lastIndexOf(".") + 1) + "jbin";
      URL         modelURL   = Main.class.getClassLoader().getResource(modelBinary);
      //Finds texture path using stored binary location
      texturepath = modelFile;      
      
      //verify the presence of the jbin model
      if ((modelURL == null)&&(!modelFormat.equals("md5mesh"))){
      
         modelURL      = Main.class.getClassLoader().getResource(modelFile);
         
         //evaluate the format
         if (modelFormat.equals("3ds")){
            formatConverter = new MaxToJme();
         } else if (modelFormat.equals("md2")){
            formatConverter = new Md2ToJme();
         } else if (modelFormat.equals("md3")){
            formatConverter = new Md3ToJme();
         } else if (modelFormat.equals("ms3d")){
            formatConverter = new MilkToJme();
         } else if (modelFormat.equals("ase")){
            formatConverter = new AseToJme();
         } else if (modelFormat.equals("obj")){
            formatConverter = new ObjToJme();
         }
         formatConverter.setProperty("mtllib", modelURL);
         
         try {
            formatConverter.convert(modelURL.openStream(), BO);
            loadedModel = (ModelNode) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            
            //save the jbin format
            BinaryExporter.getInstance().save((Savable)loadedModel, new File("src/"+modelBinary));
         } catch (IOException e) {            
            e.printStackTrace();
            return null;
         }
         System.out.println("Saved model successfully n Loading Model");   
      
      }else if ((modelURL == null)&&(modelFormat.equals("md5mesh"))){   //Loads md5 model
         
         modelURL      = Main.class.getClassLoader().getResource(modelFile);
      UnifiedModelLoader.overrideTextureKey();
         try {
            MD5Importer.getInstance().loadMesh(modelURL, "ModelNode");
         } catch (IOException e) {
            e.printStackTrace();
         }
         ModelNode body = MD5Importer.getInstance().getModelNode();
         //clean up importer
         MD5Importer.getInstance().cleanup();         
         body.flagUpdate();
      /**
         
         try {      
            loadedModel = body;
            //save the jbin format
            BinaryExporter.getInstance().save((Savable)loadedModel, new File("src/"+modelBinary));
      } catch (IOException e) {e.printStackTrace();
         return null;}
         System.out.println("Saved md5 model successfully n Loading Model");   
         */
         //temporary load until ready to create the jbin
         return body;
         
         
       }else{
          
         try {
            //load the jbin format
            loadedModel = (ModelNode) BinaryImporter.getInstance().load(modelURL.openStream());
         } catch (IOException e) {
            return null;
         }
         System.out.println("Loaded model successfully");
      }
      
      return loadedModel;
   }
//load md5 texture
   public static void overrideTextureKey() {
      try {
         MultiFormatResourceLocator locator = new MultiFormatResourceLocator(Main.class.getClassLoader().getResource(texturepath),
               new String[]{".tga", ".bmp", ".png", ".jpg", ".texture", ".jme"});
         ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, locator);
         System.out.println("Loading Model Texture");
      } catch (URISyntaxException e) {
         e.printStackTrace();
         System.out.println("Error loading Model Texture");
      }
   }
   
   /**
    * @author Kyle Williams
    * @description Creates a to Controller to load animations
    * @param modelNode2 is the original model
    * @return animate is the new JointController created
    */
   public static JointController doMd5Animation(Node modelNode2){      
   //Creates a Controller   
      JointController animate= new JointController(((ModelNode) modelNode2).getJoints());
      //by setting it to 1 means setting it to RT_WRAP
      animate.setRepeatType(1);
      modelNode2.addController(animate);
      MD5Importer.getInstance().cleanup();
      
      return animate;   
   }
   
   /**
    * @author Kyle Williams
    * @description load animations to a controller
    * @param AnimationName is the name the user wants to give the animation
    * @param animationpath is the URL of the aniamtion
    * @return animation is the new animation created
    */
   public static JointAnimation doMd5Animate(String animationName, URL animationpath){      
      //Loads the animation from a file and assign it a name   
      try {
            MD5Importer.getInstance().loadAnim(animationpath, animationName);
         } catch (IOException e) {
            System.out.println("Error....Loading Animation");
            e.printStackTrace();
         }
         JointAnimation animation = MD5Importer.getInstance().getAnimation();      
      MD5Importer.getInstance().cleanup();
      
         return animation;   
      }


}



I used it this way

public class RandomCharacterGameState extends playerGameState {
 
   private static ModelNode model;
   JointController mainController;
   private String modelpath;
   
     public RandomCharacterGameState() {
      super("RandomCharacterGameState", model);
      modelpath = "com/tps1/RandomCharacter/";
     
        //Loads model
      model = (ModelNode) UnifiedModelLoader.loadModel(modelpath+"Character.md5mesh");
      
      
   //sets up model to be deployed
        model.setModelBound(new BoundingBox());
        model.updateModelBound();
        Node RandomCharacterNode = new Node("RandomCharacterNode");
        getRootNode().attachChild(RandomCharacterNode);
        RandomCharacterNode.attachChild(model);
       
               
          //Temporary Rotation fix due to issues between blender and md5importer
         model.getLocalRotation().fromAngles(-3f, 0f, 0f);
         
         getRootNode().updateRenderState();
         getRootNode().updateWorldBound();
         //animates the character
         Animate();
     }
    //begins aniamtions
     public void Animate(){
       //Defines Animations Path
         URL walkcycle = Main.class.getClassLoader().getResource(modelpath+"walkcycle.md5anim"); 
         URL waveanim = Main.class.getClassLoader().getResource(modelpath+"wave.md5anim");
         //Creates Controller to handle animations
         mainController = UnifiedModelLoader.doMd5Animation(model);       
         //Creates Animation and applies them to a single controller
         mainController.addAnimation(UnifiedModelLoader.doMd5Animate("walk", walkcycle));
         mainController.addAnimation(UnifiedModelLoader.doMd5Animate("wave", waveanim));
         
         
       
     }