Home | History | Annotate | Download | only in modifiers
      1 package com.jme3.scene.plugins.blender.modifiers;
      2 
      3 import java.util.ArrayList;
      4 import java.util.HashMap;
      5 import java.util.logging.Level;
      6 import java.util.logging.Logger;
      7 
      8 import com.jme3.animation.AnimControl;
      9 import com.jme3.animation.Animation;
     10 import com.jme3.animation.SpatialTrack;
     11 import com.jme3.scene.Node;
     12 import com.jme3.scene.plugins.blender.BlenderContext;
     13 import com.jme3.scene.plugins.blender.animations.Ipo;
     14 import com.jme3.scene.plugins.blender.exceptions.BlenderFileException;
     15 import com.jme3.scene.plugins.ogre.AnimData;
     16 
     17 /**
     18  * This modifier allows to add animation to the object.
     19  *
     20  * @author Marcin Roguski (Kaelthas)
     21  */
     22 /* package */class ObjectAnimationModifier extends Modifier {
     23 	private static final Logger	LOGGER	= Logger.getLogger(ObjectAnimationModifier.class.getName());
     24 
     25 	/** Loaded animation data. */
     26 	private AnimData			animData;
     27 
     28 	/**
     29 	 * This constructor reads animation of the object itself (without bones) and
     30 	 * stores it as an ArmatureModifierData modifier. The animation is returned
     31 	 * as a modifier. It should be later applied regardless other modifiers. The
     32 	 * reason for this is that object may not have modifiers added but it's
     33 	 * animation should be working. The stored modifier is an anim data and
     34 	 * additional data is given object's OMA.
     35 	 *
     36 	 * @param ipo
     37 	 *            the object's interpolation curves
     38 	 * @param objectAnimationName
     39 	 *            the name of object's animation
     40 	 * @param objectOMA
     41 	 *            the OMA of the object
     42 	 * @param blenderContext
     43 	 *            the blender context
     44 	 * @throws BlenderFileException
     45 	 *             this exception is thrown when the blender file is somehow
     46 	 *             corrupted
     47 	 */
     48 	public ObjectAnimationModifier(Ipo ipo, String objectAnimationName, Long objectOMA, BlenderContext blenderContext) throws BlenderFileException {
     49 		int fps = blenderContext.getBlenderKey().getFps();
     50 
     51 		// calculating track
     52 		SpatialTrack track = (SpatialTrack) ipo.calculateTrack(-1, 0, ipo.getLastFrame(), fps, true);
     53 
     54 		Animation animation = new Animation(objectAnimationName, ipo.getLastFrame() / fps);
     55 		animation.setTracks(new SpatialTrack[] { track });
     56 		ArrayList<Animation> animations = new ArrayList<Animation>(1);
     57 		animations.add(animation);
     58 
     59 		animData = new AnimData(null, animations);
     60 		blenderContext.setAnimData(objectOMA, animData);
     61 	}
     62 
     63 	@Override
     64 	public Node apply(Node node, BlenderContext blenderContext) {
     65 		if (invalid) {
     66 			LOGGER.log(Level.WARNING, "Armature modifier is invalid! Cannot be applied to: {0}", node.getName());
     67 		}// if invalid, animData will be null
     68 		if (animData != null) {
     69 			// INFO: constraints for this modifier are applied in the
     70 			// ObjectHelper when the whole object is loaded
     71 			ArrayList<Animation> animList = animData.anims;
     72 			if (animList != null && animList.size() > 0) {
     73 				HashMap<String, Animation> anims = new HashMap<String, Animation>();
     74 				for (int i = 0; i < animList.size(); ++i) {
     75 					Animation animation = animList.get(i);
     76 					anims.put(animation.getName(), animation);
     77 				}
     78 
     79 				AnimControl control = new AnimControl(null);
     80 				control.setAnimations(anims);
     81 				node.addControl(control);
     82 			}
     83 		}
     84 		return node;
     85 	}
     86 
     87 	@Override
     88 	public String getType() {
     89 		return Modifier.OBJECT_ANIMATION_MODIFIER_DATA;
     90 	}
     91 }
     92