Import Armature Animation Data

Hi all,

I am working with Blender 2.63 to animate a bipedal robot. I have no trouble animating it by hand, however my goal is to be able to import specific 3D data sets with time (or frames) to produce an animation out of this walking data (from Matlab). This data is in a .txt file, but can be converted to whatever format is needed. I have found an old script to perform this action, however it is from Blender 2.49, so the script is incompatible with 2.63. Is there a script out there that can perform this action in the latest Blender? If not, I have also included the old code below in the hopes that someone could help me convert it.

Thanks,
Dean


#!BPY
"""
Name: '01_My_Export__KeyData'
Blender: 249
Group: 'Export'
Tooltip: 'Export Anim Key Data'
"""
import Blender
import bpy
from Blender import Armature, Mathutils, Ipo

# Exports animation key data into a text file. Key data has the following format
# <frame>,<bone name>,<x-loc>,<y-loc>,<z-loc>,<x-rot>,<y-rot>,<z-rot>

def get_keys():
        f = open('A:\_EXPORT_keys.txt', 'w')                                    ###### file for export data ######
        arm = Blender.Object.Get('Scene Root')
        mypose = arm.getPose()
        bones_ipo = arm.getAction().getAllChannelIpos()
        keydata = []

        # iterate through all interesting bones
        for bone in ( 'Bip01 Op1.R', 'Bip01 Op1.L', 'Bip01 Op2.R', 'Bip01 Op2.L' ):
#       for bone, bonename in ( mypose.bones.items() ):
#       for bone in ( 'Bip01 Head', 'Bip01 Hand.L', 'Bip01 Hand.R', 'Bip01 UpperArm.L', 'Bip01 UpperArm.R', 'Bip01 ForearmTwist.L', 'Bip01 ForearmTwist.R', \
#               'Bip01 UpperArmTwist.L', 'Bip01 UpperArmTwist.R', 'Bip01 Forearm.L', 'Bip01 Forearm.R', 'Bip01 Spine', 'Bip01 Spine1', 'Bip01 Spine2', 'Bip01 Clavicle.L', 'Bip01 Clavicle.R' ):
                ipo = bones_ipo[bone]
                ipo_curves = ipo.curveConsts.values()
                maxframe = 0
                # read translation data from ipo(s) and add to key data
                for curve in ipo_curves:
                        if ipo[curve] is None:
#                               print >> f, "NOT a curve"
                                continue
                        for btriple in ipo[curve].bezierPoints:
                                frame = int(btriple.pt[0])
                                maxframe = frame
                                Blender.Set('curframe', frame)
                                ploc = mypose.bones[bone].loc
                                x1 = ploc[0]
                                y1 = ploc[1]
                                z1 = ploc[2]
                                myRot = mypose.bones[bone].quat.toEuler()
                                rx = myRot[0]
                                ry = myRot[1]
                                rz = myRot[2]
                                s = "%3d,%s,%.2f,%.2f,%.2f,%.0f,%.0f,%.0f" % (frame,bone,x1,y1,z1,rx,ry,rz)
                                keydata.append(s)

        # sort selected keydata and export
        dic={}
        for i in keydata: 
                dic[i]=''
        keydata=dic.keys()
        
        keydata.sort()
        for line in keydata:
                print >> f, line

get_keys()

mmmm, this script export animation data into a txt file. it’s what u need?? or you want to import it??

Thanks for pointing that out. I posted the wrong code. Here’s the importer code:


#!BPY
"""
Name: '0_My_Import_KeyData'
Blender: 249
Group: 'Import'
Tooltip: 'Import Anim Key Data'
"""
import Blender
from Blender import Armature, Mathutils

# Imports animation key data from a text file. Key data is sorted and has the following format:
# <frame>,<bone name>,<x-loc>,<y-loc>,<z-loc>,<x-rot>,<y-rot>,<z-rot>
# e.g.
# 3,Bip01 Op1.R,0.01,0.12,0.12, 4, 3, 0
# All bones get the Bip01 priority
# Note: Remove imported bones in action list before running this script

def set_keys():
        arm = Blender.Object.Get('Scene Root')
        mypose = arm.getPose()
        f = open('A:\key_data.txt', 'r')                                        ###### file with import data ######

        # define string with priority constraint (Bip01 priority)       
        prio = "priority:50"
        for bonename, bone in ( mypose.bones.items() ):
                if bonename == "Bip01":
                        for constr in bone.constraints:
                                if constr.type == Blender.Constraint.Type.NULL and constr.name[:9] == "priority:":
                                        prio = constr.name
        print "OP %s" % prio

        # read import lines
        for line in f:
                kdata = line.split(',')
                pbone = mypose.bones[kdata[1]]
                # frame 1: define bone and set priority
                if int(kdata[0]) == 1:
                        pbone.localMatrix = Blender.Mathutils.Matrix().identity().resize4x4()
                        priorityconstr = None
                        for constr in pbone.constraints:
                                if constr.type == Blender.Constraint.Type.NULL and constr.name[:9] == "priority:":
                                        priorityconstr = constr
                                        break
                        if not priorityconstr:
                                priorityconstr = pbone.constraints.append(Blender.Constraint.Type.NULL)
                        priorityconstr.name = prio
                # set loc and rot, and insert key
                pbone.loc = Blender.Mathutils.Vector(float(kdata[2]),float(kdata[3]),float(kdata[4]))
                pbone.quat = Blender.Mathutils.Euler(float(kdata[5]), float(kdata[6]), float(kdata[7])).toQuat()
                pbone.insertKey(arm, int(kdata[0]), [Blender.Object.Pose.ROT, Blender.Object.Pose.LOC])
        mypose.update

set_keys()

Thanks for pointing that out. I accidentally posted the export code. I am trying to import the animation data to create an animation.



#!BPY
"""
Name: '0_My_Import_KeyData'
Blender: 249
Group: 'Import'
Tooltip: 'Import Anim Key Data'
"""
import Blender
from Blender import Armature, Mathutils

# Imports animation key data from a text file. Key data is sorted and has the following format:
# <frame>,<bone name>,<x-loc>,<y-loc>,<z-loc>,<x-rot>,<y-rot>,<z-rot>
# e.g.
# 3,Bip01 Op1.R,0.01,0.12,0.12, 4, 3, 0
# All bones get the Bip01 priority
# Note: Remove imported bones in action list before running this script

def set_keys():
        arm = Blender.Object.Get('Scene Root')
        mypose = arm.getPose()
        f = open('A:\key_data.txt', 'r')                                        ###### file with import data ######

        # define string with priority constraint (Bip01 priority)       
        prio = "priority:50"
        for bonename, bone in ( mypose.bones.items() ):
                if bonename == "Bip01":
                        for constr in bone.constraints:
                                if constr.type == Blender.Constraint.Type.NULL and constr.name[:9] == "priority:":
                                        prio = constr.name
        print "OP %s" % prio

        # read import lines
        for line in f:
                kdata = line.split(',')
                pbone = mypose.bones[kdata[1]]
                # frame 1: define bone and set priority
                if int(kdata[0]) == 1:
                        pbone.localMatrix = Blender.Mathutils.Matrix().identity().resize4x4()
                        priorityconstr = None
                        for constr in pbone.constraints:
                                if constr.type == Blender.Constraint.Type.NULL and constr.name[:9] == "priority:":
                                        priorityconstr = constr
                                        break
                        if not priorityconstr:
                                priorityconstr = pbone.constraints.append(Blender.Constraint.Type.NULL)
                        priorityconstr.name = prio
                # set loc and rot, and insert key
                pbone.loc = Blender.Mathutils.Vector(float(kdata[2]),float(kdata[3]),float(kdata[4]))
                pbone.quat = Blender.Mathutils.Euler(float(kdata[5]), float(kdata[6]), float(kdata[7])).toQuat()
                pbone.insertKey(arm, int(kdata[0]), [Blender.Object.Pose.ROT, Blender.Object.Pose.LOC])
        mypose.update

set_keys()