From 2ccadb7a7a6351105a13311448b82642f5818827 Mon Sep 17 00:00:00 2001 From: "Kae..pl" Date: Mon, 28 Oct 2013 14:18:48 +0000 Subject: [PATCH] Bugfix: fixed a bug (that occured during work on IK) that caused NPE to be raised when constraint other than IK was applied to bone. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10851 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../definitions/ConstraintDefinition.java | 32 +++++++++++++++++++ .../ConstraintDefinitionDistLimit.java | 8 ++--- .../ConstraintDefinitionLocLike.java | 6 ++-- .../ConstraintDefinitionLocLimit.java | 6 ++-- .../ConstraintDefinitionRotLike.java | 6 ++-- .../ConstraintDefinitionRotLimit.java | 6 ++-- .../ConstraintDefinitionSizeLike.java | 6 ++-- .../ConstraintDefinitionSizeLimit.java | 6 ++-- 8 files changed, 47 insertions(+), 29 deletions(-) diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinition.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinition.java index 75021b75b..a57bea7ec 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinition.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinition.java @@ -2,9 +2,11 @@ package com.jme3.scene.plugins.blender.constraints.definitions; import java.util.Set; +import com.jme3.animation.Bone; import com.jme3.math.Transform; import com.jme3.scene.plugins.blender.BlenderContext; import com.jme3.scene.plugins.blender.BlenderContext.LoadedFeatureDataType; +import com.jme3.scene.plugins.blender.animations.BoneContext; import com.jme3.scene.plugins.blender.constraints.ConstraintHelper; import com.jme3.scene.plugins.blender.constraints.ConstraintHelper.Space; import com.jme3.scene.plugins.blender.file.Structure; @@ -67,6 +69,36 @@ public abstract class ConstraintDefinition { return owner; } + /** + * The method gets the owner's transformation. The owner can be either bone or spatial. + * @param ownerSpace + * the space in which the computed transformation is given + * @return the constraint owner's transformation + */ + protected Transform getOwnerTransform(Space ownerSpace) { + if (this.getOwner() instanceof Bone) { + BoneContext boneContext = blenderContext.getBoneContext(ownerOMA); + return constraintHelper.getTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace); + } + return constraintHelper.getTransform(ownerOMA, null, ownerSpace); + } + + /** + * The method applies the given transformation to the owner. + * @param ownerTransform + * the transformation to apply to the owner + * @param ownerSpace + * the space that defines which owner's transformation (ie. global, local, etc. will be set) + */ + protected void applyOwnerTransform(Transform ownerTransform, Space ownerSpace) { + if (this.getOwner() instanceof Bone) { + BoneContext boneContext = blenderContext.getBoneContext(ownerOMA); + constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace, ownerTransform); + } else { + constraintHelper.applyTransform(ownerOMA, null, ownerSpace, ownerTransform); + } + } + /** * @return true if the definition is implemented and false * otherwise diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionDistLimit.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionDistLimit.java index 54aa2bcb5..b2ab36b6c 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionDistLimit.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionDistLimit.java @@ -4,7 +4,6 @@ import com.jme3.animation.Bone; import com.jme3.math.Transform; import com.jme3.math.Vector3f; import com.jme3.scene.plugins.blender.BlenderContext; -import com.jme3.scene.plugins.blender.animations.BoneContext; import com.jme3.scene.plugins.blender.constraints.ConstraintHelper.Space; import com.jme3.scene.plugins.blender.file.Structure; @@ -34,8 +33,7 @@ import com.jme3.scene.plugins.blender.file.Structure; return; } - BoneContext boneContext = blenderContext.getBoneContext(ownerOMA); - Transform ownerTransform = constraintHelper.getTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace); + Transform ownerTransform = this.getOwnerTransform(ownerSpace); Vector3f v = ownerTransform.getTranslation().subtract(targetTransform.getTranslation()); float currentDistance = v.length(); @@ -66,8 +64,8 @@ import com.jme3.scene.plugins.blender.file.Structure; default: throw new IllegalStateException("Unknown distance limit constraint mode: " + mode); } - - constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace, ownerTransform); + + this.applyOwnerTransform(ownerTransform, ownerSpace); } @Override diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionLocLike.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionLocLike.java index 7970fe569..43250fec2 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionLocLike.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionLocLike.java @@ -4,7 +4,6 @@ import com.jme3.animation.Bone; import com.jme3.math.Transform; import com.jme3.math.Vector3f; import com.jme3.scene.plugins.blender.BlenderContext; -import com.jme3.scene.plugins.blender.animations.BoneContext; import com.jme3.scene.plugins.blender.constraints.ConstraintHelper.Space; import com.jme3.scene.plugins.blender.file.Structure; @@ -51,8 +50,7 @@ import com.jme3.scene.plugins.blender.file.Structure; return; } - BoneContext boneContext = blenderContext.getBoneContext(ownerOMA); - Transform ownerTransform = constraintHelper.getTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace); + Transform ownerTransform = this.getOwnerTransform(ownerSpace); Vector3f ownerLocation = ownerTransform.getTranslation(); Vector3f targetLocation = targetTransform.getTranslation(); @@ -89,7 +87,7 @@ import com.jme3.scene.plugins.blender.file.Structure; ownerLocation.addLocal(startLocation); } - constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace, ownerTransform); + this.applyOwnerTransform(ownerTransform, ownerSpace); } @Override diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionLocLimit.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionLocLimit.java index 14b5bb49d..aff7e6b6e 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionLocLimit.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionLocLimit.java @@ -4,7 +4,6 @@ import com.jme3.animation.Bone; import com.jme3.math.Transform; import com.jme3.math.Vector3f; import com.jme3.scene.plugins.blender.BlenderContext; -import com.jme3.scene.plugins.blender.animations.BoneContext; import com.jme3.scene.plugins.blender.constraints.ConstraintHelper.Space; import com.jme3.scene.plugins.blender.file.Structure; @@ -61,8 +60,7 @@ import com.jme3.scene.plugins.blender.file.Structure; return; } - BoneContext boneContext = blenderContext.getBoneContext(ownerOMA); - Transform ownerTransform = constraintHelper.getTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace); + Transform ownerTransform = this.getOwnerTransform(ownerSpace); Vector3f translation = ownerTransform.getTranslation(); @@ -85,7 +83,7 @@ import com.jme3.scene.plugins.blender.file.Structure; translation.z -= (translation.z - limits[2][1]) * influence; } - constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace, ownerTransform); + this.applyOwnerTransform(ownerTransform, ownerSpace); } @Override diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionRotLike.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionRotLike.java index 9ef618f69..24a91bfb9 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionRotLike.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionRotLike.java @@ -3,7 +3,6 @@ package com.jme3.scene.plugins.blender.constraints.definitions; import com.jme3.math.Quaternion; import com.jme3.math.Transform; import com.jme3.scene.plugins.blender.BlenderContext; -import com.jme3.scene.plugins.blender.animations.BoneContext; import com.jme3.scene.plugins.blender.constraints.ConstraintHelper.Space; import com.jme3.scene.plugins.blender.file.Structure; @@ -30,8 +29,7 @@ import com.jme3.scene.plugins.blender.file.Structure; @Override public void bake(Space ownerSpace, Space targetSpace, Transform targetTransform, float influence) { - BoneContext boneContext = blenderContext.getBoneContext(ownerOMA); - Transform ownerTransform = constraintHelper.getTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace); + Transform ownerTransform = this.getOwnerTransform(ownerSpace); Quaternion ownerRotation = ownerTransform.getRotation(); ownerAngles = ownerRotation.toAngles(ownerAngles); @@ -70,7 +68,7 @@ import com.jme3.scene.plugins.blender.file.Structure; // TODO } - constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace, ownerTransform); + this.applyOwnerTransform(ownerTransform, ownerSpace); } @Override diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionRotLimit.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionRotLimit.java index 51ca94698..55a8f8147 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionRotLimit.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionRotLimit.java @@ -3,7 +3,6 @@ package com.jme3.scene.plugins.blender.constraints.definitions; import com.jme3.math.FastMath; import com.jme3.math.Transform; import com.jme3.scene.plugins.blender.BlenderContext; -import com.jme3.scene.plugins.blender.animations.BoneContext; import com.jme3.scene.plugins.blender.constraints.ConstraintHelper.Space; import com.jme3.scene.plugins.blender.file.Structure; @@ -70,8 +69,7 @@ import com.jme3.scene.plugins.blender.file.Structure; @Override public void bake(Space ownerSpace, Space targetSpace, Transform targetTransform, float influence) { - BoneContext boneContext = blenderContext.getBoneContext(ownerOMA); - Transform ownerTransform = constraintHelper.getTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace); + Transform ownerTransform = this.getOwnerTransform(ownerSpace); ownerTransform.getRotation().toAngles(angles); // make sure that the rotations are always in range [0, 2PI) @@ -111,7 +109,7 @@ import com.jme3.scene.plugins.blender.file.Structure; } ownerTransform.getRotation().fromAngles(angles); - constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace, ownerTransform); + this.applyOwnerTransform(ownerTransform, ownerSpace); } @Override diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionSizeLike.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionSizeLike.java index 9abcf2e46..2b86a5c86 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionSizeLike.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionSizeLike.java @@ -3,7 +3,6 @@ package com.jme3.scene.plugins.blender.constraints.definitions; import com.jme3.math.Transform; import com.jme3.math.Vector3f; import com.jme3.scene.plugins.blender.BlenderContext; -import com.jme3.scene.plugins.blender.animations.BoneContext; import com.jme3.scene.plugins.blender.constraints.ConstraintHelper.Space; import com.jme3.scene.plugins.blender.file.Structure; @@ -33,8 +32,7 @@ import com.jme3.scene.plugins.blender.file.Structure; @Override public void bake(Space ownerSpace, Space targetSpace, Transform targetTransform, float influence) { - BoneContext boneContext = blenderContext.getBoneContext(ownerOMA); - Transform ownerTransform = constraintHelper.getTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace); + Transform ownerTransform = this.getOwnerTransform(ownerSpace); Vector3f ownerScale = ownerTransform.getScale(); Vector3f targetScale = targetTransform.getScale(); @@ -56,7 +54,7 @@ import com.jme3.scene.plugins.blender.file.Structure; } ownerScale.addLocal(offset); - constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace, ownerTransform); + this.applyOwnerTransform(ownerTransform, ownerSpace); } @Override diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionSizeLimit.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionSizeLimit.java index 63ffee26a..564f77e70 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionSizeLimit.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/definitions/ConstraintDefinitionSizeLimit.java @@ -3,7 +3,6 @@ package com.jme3.scene.plugins.blender.constraints.definitions; import com.jme3.math.Transform; import com.jme3.math.Vector3f; import com.jme3.scene.plugins.blender.BlenderContext; -import com.jme3.scene.plugins.blender.animations.BoneContext; import com.jme3.scene.plugins.blender.constraints.ConstraintHelper.Space; import com.jme3.scene.plugins.blender.file.Structure; @@ -55,8 +54,7 @@ import com.jme3.scene.plugins.blender.file.Structure; @Override public void bake(Space ownerSpace, Space targetSpace, Transform targetTransform, float influence) { - BoneContext boneContext = blenderContext.getBoneContext(ownerOMA); - Transform ownerTransform = constraintHelper.getTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace); + Transform ownerTransform = this.getOwnerTransform(ownerSpace); Vector3f scale = ownerTransform.getScale(); if ((flag & LIMIT_XMIN) != 0 && scale.x < limits[0][0]) { @@ -78,7 +76,7 @@ import com.jme3.scene.plugins.blender.file.Structure; scale.z -= (scale.z - limits[2][1]) * influence; } - constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), ownerSpace, ownerTransform); + this.applyOwnerTransform(ownerTransform, ownerSpace); } @Override