aboutsummaryrefslogtreecommitdiff
path: root/engine/src/blender/com/jme3/scene/plugins/blender/modifiers/ObjectAnimationModifier.java
blob: a41395c47fdaf9522e6763e1e46a991d5dc741ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.jme3.scene.plugins.blender.modifiers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme3.animation.AnimControl;
import com.jme3.animation.Animation;
import com.jme3.animation.SpatialTrack;
import com.jme3.scene.Node;
import com.jme3.scene.plugins.blender.BlenderContext;
import com.jme3.scene.plugins.blender.animations.Ipo;
import com.jme3.scene.plugins.blender.exceptions.BlenderFileException;
import com.jme3.scene.plugins.ogre.AnimData;

/**
 * This modifier allows to add animation to the object.
 * 
 * @author Marcin Roguski (Kaelthas)
 */
/* package */class ObjectAnimationModifier extends Modifier {
	private static final Logger	LOGGER	= Logger.getLogger(ObjectAnimationModifier.class.getName());

	/** Loaded animation data. */
	private AnimData			animData;

	/**
	 * This constructor reads animation of the object itself (without bones) and
	 * stores it as an ArmatureModifierData modifier. The animation is returned
	 * as a modifier. It should be later applied regardless other modifiers. The
	 * reason for this is that object may not have modifiers added but it's
	 * animation should be working. The stored modifier is an anim data and
	 * additional data is given object's OMA.
	 * 
	 * @param ipo
	 *            the object's interpolation curves
	 * @param objectAnimationName
	 *            the name of object's animation
	 * @param objectOMA
	 *            the OMA of the object
	 * @param blenderContext
	 *            the blender context
	 * @throws BlenderFileException
	 *             this exception is thrown when the blender file is somehow
	 *             corrupted
	 */
	public ObjectAnimationModifier(Ipo ipo, String objectAnimationName, Long objectOMA, BlenderContext blenderContext) throws BlenderFileException {
		int fps = blenderContext.getBlenderKey().getFps();

		// calculating track
		SpatialTrack track = (SpatialTrack) ipo.calculateTrack(-1, 0, ipo.getLastFrame(), fps, true);

		Animation animation = new Animation(objectAnimationName, ipo.getLastFrame() / fps);
		animation.setTracks(new SpatialTrack[] { track });
		ArrayList<Animation> animations = new ArrayList<Animation>(1);
		animations.add(animation);

		animData = new AnimData(null, animations);
		blenderContext.setAnimData(objectOMA, animData);
	}

	@Override
	public Node apply(Node node, BlenderContext blenderContext) {
		if (invalid) {
			LOGGER.log(Level.WARNING, "Armature modifier is invalid! Cannot be applied to: {0}", node.getName());
		}// if invalid, animData will be null
		if (animData != null) {
			// INFO: constraints for this modifier are applied in the
			// ObjectHelper when the whole object is loaded
			ArrayList<Animation> animList = animData.anims;
			if (animList != null && animList.size() > 0) {
				HashMap<String, Animation> anims = new HashMap<String, Animation>();
				for (int i = 0; i < animList.size(); ++i) {
					Animation animation = animList.get(i);
					anims.put(animation.getName(), animation);
				}

				AnimControl control = new AnimControl(null);
				control.setAnimations(anims);
				node.addControl(control);
			}
		}
		return node;
	}

	@Override
	public String getType() {
		return Modifier.OBJECT_ANIMATION_MODIFIER_DATA;
	}
}