AnimControl – one for many objects

Ok i get that you want to switch parts of the model.

So I made a small test case to demonstrate what I mean.

I think you are over complicating something that is already fairly complicated…



[java]

package mygame;



import com.jme3.animation.AnimChannel;

import com.jme3.animation.AnimControl;

import com.jme3.animation.LoopMode;

import com.jme3.animation.SkeletonControl;

import com.jme3.app.SimpleApplication;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;



/**

  • test
  • @author Nehon

    */

    public class Main extends SimpleApplication {



    public static void main(String[] args) {

    Main app = new Main();

    app.start();

    }



    @Override

    public void simpleInitApp() {



    viewPort.setBackgroundColor(ColorRGBA.DarkGray);

    flyCam.setMoveSpeed(100);



    //loading the model

    final Node model = (Node) assetManager.loadModel(“Models/Sinbad/Sinbad.j3o”);

    rootNode.attachChild(model);



    //keeping the pants geometry in a variable

    final Geometry origPants = (Geometry) model.getChild(“Sinbad-geom-7”);



    //creating a clone (just for conveniance of the example, but this can be someting else

    //imported from blender that has bone index and bone weight for this skeleton

    final Geometry newPants = (Geometry) model.getChild(“Sinbad-geom-7”).clone(false);

    //setting a basic lighting material

    newPants.setMaterial(new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”));



    //fetching the skeleton control on the model

    final SkeletonControl skeletonControl = model.getControl(SkeletonControl.class);

    //removing it

    model.removeControl(skeletonControl);



    //removing the original pants from the model

    origPants.removeFromParent();

    //attaching the new pants to the model

    model.attachChild(newPants);



    //re-adding the control so that it gathers the geoms

    model.addControl(skeletonControl);



    //setting an animation to demostrate that the anim is not messed up…

    AnimChannel channel = model.getControl(AnimControl.class).createChannel();

    channel.setAnim(“Dance”);

    channel.setLoopMode(LoopMode.Loop);





    //even adding a key binding to switch pants at runtime

    inputManager.addListener(new ActionListener() {



    public void onAction(String name, boolean isPressed, float tpf) {

    if (name.equals(“switchPants”) && isPressed) {

    model.removeControl(skeletonControl);

    if (origPants.getParent() != null) {

    origPants.removeFromParent();

    model.attachChild(newPants);

    } else {

    newPants.removeFromParent();

    model.attachChild(origPants);

    }

    model.addControl(skeletonControl);

    }

    }

    }, “switchPants”);



    inputManager.addMapping(“switchPants”, new KeyTrigger(KeyInput.KEY_SPACE));



    }



    }



    [/java]



    Screenshot :

    http://i.imgur.com/aHlsX.png

    Now if you want to change the pants with another model, it just have to fit to the same skeleton. So you can have plenty different blender files (one for each armor set for example) has long as you use the same skeleton in those files.
5 Likes