[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [27040] trunk/blender/release/scripts/io/ import_anim_bvh.py: BVH import working again.

Campbell Barton ideasman42 at gmail.com
Sat Feb 20 23:51:37 CET 2010


Revision: 27040
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=27040
Author:   campbellbarton
Date:     2010-02-20 23:51:36 +0100 (Sat, 20 Feb 2010)

Log Message:
-----------
BVH import working again.
- euler/quat rotation option
- scale, startframe options back.
- fix for adding an armature which used operators can could fail, use the data api instead.
- remove old junk

Modified Paths:
--------------
    trunk/blender/release/scripts/io/import_anim_bvh.py

Modified: trunk/blender/release/scripts/io/import_anim_bvh.py
===================================================================
--- trunk/blender/release/scripts/io/import_anim_bvh.py	2010-02-20 22:51:22 UTC (rev 27039)
+++ trunk/blender/release/scripts/io/import_anim_bvh.py	2010-02-20 22:51:36 UTC (rev 27040)
@@ -19,22 +19,12 @@
 # <pep8 compliant>
 
 import math
+from math import radians
 
-# import Blender
 import bpy
-# import BPyMessages
 import Mathutils
-Vector = Mathutils.Vector
-Euler = Mathutils.Euler
-Matrix = Mathutils.Matrix
-RotationMatrix = Mathutils.RotationMatrix
-TranslationMatrix = Mathutils.TranslationMatrix
+from Mathutils import Vector, Euler, Matrix, RotationMatrix, TranslationMatrix
 
-# NASTY GLOBAL
-ROT_STYLE = 'QUAT'
-
-DEG2RAD = 0.017453292519943295
-
 class bvh_node_class(object):
     __slots__=(\
     'name',# bvh joint name
@@ -89,7 +79,7 @@
 def eulerRotate(x,y,z, rot_order):
 
     # Clamp all values between 0 and 360, values outside this raise an error.
-    mats=[RotationMatrix(math.radians(x % 360), 3, 'X'), RotationMatrix(math.radians(y % 360),3,'Y'), RotationMatrix(math.radians(z % 360), 3, 'Z')]
+    mats=[RotationMatrix(radians(x % 360), 3, 'X'), RotationMatrix(radians(y % 360),3,'Y'), RotationMatrix(radians(z % 360), 3, 'Z')]
     # print rot_order
     # Standard BVH multiplication order, apply the rotation in the order Z,X,Y
 
@@ -101,7 +91,7 @@
 
     return eul
 
-def read_bvh(context, file_path, GLOBAL_SCALE=1.0):
+def read_bvh(context, file_path, ROT_MODE='XYZ', GLOBAL_SCALE=1.0):
     # File loading stuff
     # Open the file for importing
     file = open(file_path, 'rU')
@@ -244,7 +234,7 @@
             if channels[3] != -1 or channels[4] != -1 or channels[5] != -1:
                 rx, ry, rz = float(line[channels[3]]), float(line[channels[4]]), float(line[channels[5]])
 
-                if ROT_STYLE != 'NATIVE':
+                if ROT_MODE != 'NATIVE':
                     rx, ry, rz = eulerRotate(rx, ry, rz, bvh_node.rot_order)
 
                 # Make interpolation not cross between 180d, thjis fixes sub frame interpolation and time scaling.
@@ -301,8 +291,6 @@
             bvh_node.rest_tail_local.y = bvh_node.rest_tail_local.y + GLOBAL_SCALE/10
             bvh_node.rest_tail_world.y = bvh_node.rest_tail_world.y + GLOBAL_SCALE/10
 
-
-
     return bvh_nodes
 
 
@@ -353,7 +341,7 @@
             rest_head_local= bvh_node.rest_head_local
             bvh_node.temp.loc= rest_head_local.x+lx, rest_head_local.y+ly, rest_head_local.z+lz
 
-            bvh_node.temp.rot= rx*DEG2RAD,ry*DEG2RAD,rz*DEG2RAD
+            bvh_node.temp.rot= radians(rx), radians(ry), radians(rz)
 
             bvh_node.temp.insertIpoKey(Blender.Object.IpoKeyTypes.LOCROT) # XXX invalid
 
@@ -362,38 +350,29 @@
 
 
 
-def bvh_node_dict2armature(context, bvh_nodes, IMPORT_START_FRAME= 1, IMPORT_LOOP= False):
+def bvh_node_dict2armature(context, bvh_nodes, ROT_MODE='XYZ', IMPORT_START_FRAME= 1, IMPORT_LOOP= False):
 
     if IMPORT_START_FRAME<1:
         IMPORT_START_FRAME= 1
 
-
     # Add the new armature,
     scn = context.scene
 #XXX	scn.objects.selected = []
     for ob in scn.objects:
         ob.selected = False
 
+    scn.set_frame(IMPORT_START_FRAME)
 
-#XXX	arm_data= bpy.data.armatures.new()
-#XXX	arm_ob = scn.objects.new(arm_data)
-    bpy.ops.object.armature_add()
-    arm_ob= scn.objects[-1]
-    arm_data= arm_ob.data
+    arm_data= bpy.data.armatures.new("MyBVH")
+    arm_ob= bpy.data.objects.new("MyBVH", 'ARMATURE')
+    arm_ob.data = arm_data
+    
+    scn.objects.link(arm_ob)
 
-
-
-
-#XXX	scn.objects.context = [arm_ob]
-#XXX	scn.objects.active = arm_ob
     arm_ob.selected= True
     scn.objects.active= arm_ob
     print(scn.objects.active)
 
-
-    # Put us into editmode
-#XXX	arm_data.makeEditable()
-
     bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
     bpy.ops.object.mode_set(mode='EDIT', toggle=False)
 
@@ -480,40 +459,37 @@
     pose= arm_ob.pose
     pose_bones= pose.bones
 
-
-    if ROT_STYLE=='NATIVE':
+    print('ROT_MODE', ROT_MODE)
+    if ROT_MODE=='NATIVE':
+        print(1)
         eul_order_lookup = {\
             (0,1,2):'XYZ',
             (0,2,1):'XZY',
             (1,0,2):'YXZ',
             (1,2,0):'YZX',
             (2,0,1):'ZXY',
-            (2,1,0):'ZYZ'}
+            (2,1,0):'ZYX'}
 
         for bvh_node in bvh_nodes.values():
             bone_name= bvh_node.temp # may not be the same name as the bvh_node, could have been shortened.
             pose_bone= pose_bones[bone_name]
             pose_bone.rotation_mode  = eul_order_lookup[tuple(bvh_node.rot_order)]
 
-    elif ROT_STYLE=='XYZ':
+    elif ROT_MODE=='XYZ':
+        print(2)
         for pose_bone in pose_bones:
-            pose_bone.rotation_mode  = 'XYZ'
+            pose_bone.rotation_mode = 'XYZ'
     else:
         # Quats default
+        print(3)
         pass
+    
+    context.scene.update()
 
-
     bpy.ops.pose.select_all() # set
     bpy.ops.anim.keyframe_insert_menu(type=-4) # XXX -     -4 ???
 
 
-
-
-
-    #for p in pose_bones:
-    #	print(p)
-
-
 #XXX	action = Blender.Armature.NLA.NewAction("Action")
 #XXX	action.setActive(arm_ob)
 
@@ -523,19 +499,13 @@
     # arm_ob.animation_data.action = action
     action = arm_ob.animation_data.action
 
-
-
-
-    #xformConstants= [ Blender.Object.Pose.LOC, Blender.Object.Pose.ROT ]
-
     # Replace the bvh_node.temp (currently an editbone)
     # With a tuple  (pose_bone, armature_bone, bone_rest_matrix, bone_rest_matrix_inv)
     for bvh_node in bvh_nodes.values():
         bone_name= bvh_node.temp # may not be the same name as the bvh_node, could have been shortened.
         pose_bone= pose_bones[bone_name]
         rest_bone= arm_data.bones[bone_name]
-#XXX		bone_rest_matrix = rest_bone.matrix['ARMATURESPACE'].rotation_part()
-        bone_rest_matrix = rest_bone.matrix.rotation_part()
+        bone_rest_matrix = rest_bone.matrix_local.rotation_part()
 
 
         bone_rest_matrix_inv= Matrix(bone_rest_matrix)
@@ -547,14 +517,6 @@
 
 
     # Make a dict for fast access without rebuilding a list all the time.
-    '''
-    xformConstants_dict={
-    (True,True):	[Blender.Object.Pose.LOC, Blender.Object.Pose.ROT],\
-    (False,True):	[Blender.Object.Pose.ROT],\
-    (True,False):	[Blender.Object.Pose.LOC],\
-    (False,False):	[],\
-    }
-    '''
 
     # KEYFRAME METHOD, SLOW, USE IPOS DIRECT
     # TODO: use f-point samples instead (Aligorith)
@@ -563,8 +525,8 @@
     for current_frame in range(len(bvh_node.anim_data)-1): # skip the first frame (rest frame)
         # print current_frame
 
-        #if current_frame==150: # debugging
-        #	break
+        # if current_frame==40: # debugging
+        # 	break
 
         # Dont neet to set the current frame
         for bvh_node in bvh_nodes.values():
@@ -572,69 +534,25 @@
             lx,ly,lz,rx,ry,rz= bvh_node.anim_data[current_frame+1]
 
             if bvh_node.has_rot:
-
-                if ROT_STYLE=='QUAT':
-                    # Set the rotation, not so simple
-                    bone_rotation_matrix= Euler(math.radians(rx), math.radians(ry), math.radians(rz)).to_matrix()
-
-                    bone_rotation_matrix.resize4x4()
-                    #XXX ORDER CHANGE???
-                    #pose_bone.rotation_quaternion= (bone_rest_matrix * bone_rotation_matrix * bone_rest_matrix_inv).to_quat() # ORIGINAL
-                    # pose_bone.rotation_quaternion= (bone_rest_matrix_inv * bone_rotation_matrix * bone_rest_matrix).to_quat()
-                    # pose_bone.rotation_quaternion= (bone_rotation_matrix * bone_rest_matrix).to_quat() # BAD
-                    # pose_bone.rotation_quaternion= bone_rotation_matrix.to_quat() # NOT GOOD
-                    # pose_bone.rotation_quaternion= bone_rotation_matrix.to_quat() # NOT GOOD
-
-                    #pose_bone.rotation_quaternion= (bone_rotation_matrix * bone_rest_matrix_inv * bone_rest_matrix).to_quat()
-                    #pose_bone.rotation_quaternion= (bone_rest_matrix_inv * bone_rest_matrix * bone_rotation_matrix).to_quat()
-                    #pose_bone.rotation_quaternion= (bone_rest_matrix * bone_rotation_matrix * bone_rest_matrix_inv).to_quat()
-
-                    #pose_bone.rotation_quaternion= ( bone_rest_matrix* bone_rest_matrix_inv * bone_rotation_matrix).to_quat()
-                    #pose_bone.rotation_quaternion= (bone_rotation_matrix * bone_rest_matrix  * bone_rest_matrix_inv).to_quat()
-                    #pose_bone.rotation_quaternion= (bone_rest_matrix_inv * bone_rotation_matrix  * bone_rest_matrix ).to_quat()
-
+                bone_rotation_matrix= Euler(radians(rx), radians(ry), radians(rz)).to_matrix()
+                bone_rotation_matrix.resize4x4()
+                if ROT_MODE=='QUATERNION':
                     pose_bone.rotation_quaternion= (bone_rest_matrix_inv * bone_rotation_matrix * bone_rest_matrix).to_quat()
-
                 else:
-                    bone_rotation_matrix= Euler(math.radians(rx), math.radians(ry), math.radians(rz)).to_matrix()
-                    bone_rotation_matrix.resize4x4()
+                    pose_bone.rotation_euler= (bone_rest_matrix_inv * bone_rotation_matrix * bone_rest_matrix).to_euler(pose_bone.rotation_mode)
 
-                    eul= (bone_rest_matrix * bone_rotation_matrix * bone_rest_matrix_inv).to_euler()
+            if bvh_node.has_loc:
+                pose_bone.location= (bone_rest_matrix_inv * TranslationMatrix(Vector(lx, ly, lz) - bvh_node.rest_head_local )).translation_part()
 
-                    #pose_bone.rotation_euler = math.radians(rx), math.radians(ry), math.radians(rz)
-                    pose_bone.rotation_euler = eul
-
-                print("ROTATION" + str(Euler(math.radians(rx), math.radians(ry), math.radians(rz))))
-
             if bvh_node.has_loc:
-                # Set the Location, simple too
+                pose_bone.keyframe_insert("location")
+            if bvh_node.has_rot:
+                if ROT_MODE == 'QUATERNION':
+                    pose_bone.keyframe_insert("rotation_quaternion")
+                else:
+                    pose_bone.keyframe_insert("rotation_euler")
 
-                #XXX ORDER CHANGE
-                # pose_bone.location= (TranslationMatrix(Vector(lx, ly, lz) - bvh_node.rest_head_local ) * bone_rest_matrix_inv).translation_part() # WHY * 10? - just how pose works

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list