Interesting … I’m starting to think that there is a problem with the Euler module of Blender, or the way I’m using it, here is why:
My code for loading the motions of the BVH file has two parts: one part uses NLA to manipulate the Armature version of the skeleton model, the other part uses IPO curves to manipulate the Emtpies/Spheres version of the skeleton model.
All the angles in the BVH file are in degrees.
For NLA part of the code, what I do is the following:
for each bone:
for each frame:
rx,ry,rz = the corresponding angles for the right bone at the right frame
e = Euler(rx,ry,rz)
pose_bone.localMatrix = e.toMatrix()
pose_bone.insertKey(ob, frame, Object.Pose.ROT)
The last line of the previous code is interchangeable with the quat.
For the IPO part of the code, what I do is the following:
for each bone:
for each frame:
rx,ry,rz = the corresponding angles for the right bone at the right frame
ex, ey,ez = eulerRotate.eulerRotate(rx,ry,rz)
set ex, ey, ez at the right frame for the corresponding IPO curve.
The eulerRotate script is the following:
"""
Name: 'Motion Capture (.bvh)...'
Blender: 232
Group: 'Import'
Tip: 'Import a (.bvh) motion capture file'
"""
__author__ = "Campbell Barton"
__url__ = ("blender", "elysiun")
__version__ = "1.0 03/25/04"
__bpydoc__ = """\
This script imports BVH motion capture data to Blender.
Supported:<br>
Missing:<br>
Known issues:<br>
Notes:<br>
"""
# $Id: bvh_import.py,v 1.4 2004/11/07 16:31:13 ianwill Exp $
#
#===============================================#
# BVH Import script 1.0 by Campbell Barton #
# 25/03/2004, euler rotation code taken from #
# Reevan Mckay's BVH import script v1.1 #
# if you have any questions about this script #
# email me [email protected] #
#===============================================#
#===============================================#
# TODO: #
# * Create bones when importing #
# * Make an IPO jitter removal script #
# * Work out a better naming system #
#===============================================#
# --------------------------------------------------------------------------
# BVH Import v0.9 by Campbell Barton (AKA Ideasman)
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
import string
import math
import Blender
from Blender import Window, Object, Scene, Ipo
from Blender.Scene import Render
# # PSYCO IS CRASHING ON MY SYSTEM
# # Attempt to load psyco, speed things up
# try:
# print 'using psyco to speed up BVH importing'
# import psyco
# psyco.full()
#
# except:
# print 'psyco is not present on this system'
# Update as we load?
debug = 0
# Global scale facctor # sHOULD BE 1 BY DEFAULT
scale = 1
# Get the current scene.
scn = Scene.GetCurrent()
context = scn.getRenderingContext()
# Here we store the Ipo curves in the order they load.
channelCurves = []
# Object list
# We need this so we can loop through the objects and edit there IPO's
# Chenging there rotation to EULER rotation
objectList = []
def MAT(m):
if len(m) == 3:
return Blender.Mathutils.Matrix(m[0], m[1], m[2])
elif len(m) == 4:
return Blender.Mathutils.Matrix(m[0], m[1], m[2], m[3])
#===============================================#
# eulerRotation: converts X, Y, Z rotation #
# to eular Rotation. This entire function #
# is copied from Reevan Mckay's BVH script #
#===============================================#
# Vars used in eular rotation funtcion
DEG_TO_RAD = math.pi/180.0
RAD_TO_DEG = 180.0/math.pi
PI=3.14159
def eulerRotate(x,y,z):
#=================================
def RVMatMult3 (mat1,mat2):
#=================================
mat3=[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]
for i in range(3):
for k in range(3):
for j in range(3):
mat3[i][k]=mat3[i][k]+mat1[i][j]*mat2[j][k]
mat1 = mat2 = i = k = j = None # Save memory
return mat3
#=================================
def RVAxisAngleToMat3 (rot4):
# Takes a direction vector and
# a rotation (in rads) and
# returns the rotation matrix.
# Graphics Gems I p. 466:
#=================================
mat3=[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]
if math.fabs(rot4[3])>0.01:
s=math.sin(rot4[3])
c=math.cos(rot4[3])
t=1.0-math.cos(rot4[3])
else:
s=rot4[3]
c=1.0
t=0.0
x=rot4[0]; y=rot4[1]; z=rot4[2]
mat3[0][0]=t*x*x+c
mat3[0][1]=t*x*y+s*z
mat3[0][2]=t*x*z-s*y
mat3[1][0]=t*x*y-s*z
mat3[1][1]=t*y*y+c
mat3[1][2]=t*y*z+s*x
mat3[2][0]=t*x*z+s*y
mat3[2][1]=t*y*z-s*x
mat3[2][2]=t*z*z+c
rot4 = s = c = t = x = y = z = None # Save some memory
return mat3
eul = [x,y,z]
for jj in range(3):
while eul[jj] < 0:
eul[jj] = eul[jj] + 360.0
while eul[jj] >= 360.0:
eul[jj] = eul[jj] - 360.0
eul[0] = eul[0]*DEG_TO_RAD
eul[1] = eul[1]*DEG_TO_RAD
eul[2] = eul[2]*DEG_TO_RAD
xmat=RVAxisAngleToMat3([1,0,0,eul[0]])
ymat=RVAxisAngleToMat3([0,1,0,eul[1]])
zmat=RVAxisAngleToMat3([0,0,1,eul[2]])
mat=[[1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,0.0,1.0]]
# Standard BVH multiplication order
mat=RVMatMult3 (zmat,mat)
mat=RVMatMult3 (xmat,mat)
mat=RVMatMult3 (ymat,mat)
'''
# Screwy Animation Master BVH multiplcation order
mat=RVMatMult3 (ymat,mat)
mat=RVMatMult3 (xmat,mat)
mat=RVMatMult3 (zmat,mat)
'''
mat = MAT(mat)
eul = mat.toEuler()
x =- eul[0]/-10
y =- eul[1]/-10
z =- eul[2]/-10
eul = mat = zmat = xmat = ymat = jj = None
return x, y, z # Returm euler roration values.
The NLA code produces the wrong animation, the IPO cod produces the right animation. For the IPO code, I have to first transform the original angles to the euler angles, otherwise the animation would be beyond wrong. The only difference I can see here is the Euler code. Maybe that’s the problem?