udoobu
March 1, 2013, 4:03pm
1
Greetings! I’m new to importing from Blender, etc. So far, I can load my model, but the issue I’m having is loading the animation. I can see the model and animation in the Scene Explorer just fine. But, the code I’m using to attempt to load the animation is causing a NullPointerException. Can anyone take a glance at this and tell me what I’m missing please?
[java]
Spatial gun1 = assetManager.loadModel(“Models/Game Gun 1/Game Gun 1.j3o”);
Material m_gun1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
Texture t_gun = assetManager.loadTexture(“Textures/Sub.jpg”);
m_gun1.setTexture("ColorMap", t_gun);
gun1.setMaterial(m_gun1);
gun1.setLocalTranslation(Vector3f.ZERO);
rootNode.attachChild(gun1);
control = gun1.getControl(AnimControl.class);
channel = control.createChannel(); //ERROR HAPPENS HERE
channel.setAnim("Fire");
[/java]
udoobu
March 1, 2013, 4:04pm
2
For reference, here is my whole file:
[java]
package mygame;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.app.SimpleApplication;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.light.;
import com.jme3.texture.Texture;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.scene.Node;
import java.util.logging. ;
public class Main extends SimpleApplication {
private static final Logger logger = Logger.getLogger(Main.class.getName());
private AnimChannel channel;
private AnimControl control;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(20);
flyCam.setZoomSpeed(0);
viewPort.setBackgroundColor(ColorRGBA.DarkGray);
inputManager.addMapping("Fire", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "Fire");
Spatial gun1 = assetManager.loadModel("Models/Game Gun 1/Game Gun 1.j3o");
Material m_gun1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture t_gun = assetManager.loadTexture("Textures/Sub.jpg");
m_gun1.setTexture("ColorMap", t_gun);
gun1.setMaterial(m_gun1);
gun1.setLocalTranslation(Vector3f.ZERO);
rootNode.attachChild(gun1);
control = gun1.getControl(AnimControl.class);
channel = control.createChannel();
channel.setAnim("Fire");
//logger.log(Level.INFO, control.getAnimationNames().toString());
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Fire") && !keyPressed) {
//channel.setAnim("");
}
}
};
private Spatial findNode(Node rootNode, String name) {
if (name.equals(rootNode.getName())) {
return rootNode;
}
return rootNode.getChild(name);
}
@Override
public void simpleUpdate(float tpf) {
}
@Override
public void simpleRender(RenderManager rm) {
}
}
[/java]
normen
March 1, 2013, 4:04pm
3
Look at the model in the SceneExplorer, the AnimControl probably isn’t attached to the root node but a sub-node. You can grab it by using Spatial.getChild(name).
1 Like
udoobu
March 1, 2013, 4:18pm
4
@normen Hm, I did’t get a “getChild()” method option on the “gun1” spatial…
udoobu
March 1, 2013, 4:20pm
5
@normen Also, FYI… in the Scene Explorer… the hierarchy is:
GUNMLP > AnimControl > Fire
normen
March 1, 2013, 4:22pm
6
Uh, right, its obviously on Node, so you have to cast it to Node, sorry ^^ GetChild() works recursively and gets you the first spatial by that name.
udoobu
March 1, 2013, 4:29pm
7
@normen
Ok, I’ve updated my code. I set another spatial to the getChild(“Fire”). And, it NULLs out.
[java]
Spatial gun1 = assetManager.loadModel(“Models/Game Gun 1/Game Gun 1.j3o”);
Material m_gun1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
Texture t_gun = assetManager.loadTexture(“Textures/Sub.jpg”);
m_gun1.setTexture("ColorMap", t_gun);
gun1.setMaterial(m_gun1);
gun1.setLocalTranslation(Vector3f.ZERO);
rootNode.attachChild(gun1);
Spatial animFire = rootNode.getChild("Fire");
control = animFire.getControl(AnimControl.class);
channel = control.createChannel();
channel.setAnim("Fire");
[/java]
normen
March 1, 2013, 4:30pm
8
Nah, “GUNMLP” seems to be the spatial from what you say. “Fire” is the animation.
udoobu
March 1, 2013, 4:33pm
9
@normen Ok, ok… thank you for opening my eyes. That was the connection I was missing. I appreciate it - works great!