[Bf-python] Using NLA module for exporting animations

Damien McGinnes mcginnes at netspeed.com.au
Fri Jul 23 07:31:12 CEST 2004


Hi,

Here is an extract from my modification to the Blender2Cal3d script.
Among other things I use the NLA module to parse the actions instead
of that horrible hackery that was there before

I also use curve.evaluate instead of scene.makeCurrent
This speeds things up probably a thousand fold (!)

As far as I know I havent yet seen an exporter that makes use of this
method, (most are based on an earlier cal3d export) so this is probably
relevant to many exporters.


You can find the full script at
http://home.netspeed.com.au/blender/Blend2Cal3d-0.7.py


note: the script requires a patch to blender to let
ipo.getCurve('LocX') etc. to work
See bug #1157 for details.


cheers,
Damien


--- extract -----------------

  ipoCurveType = ['LocX', 'LocY', 'LocZ', 'QuatX', 'QuatY', 'QuatZ', 'QuatW']
  actions = Blender.Armature.NLA.GetActions()

  for animation_name in actions:

    animation = ANIMATIONS[animation_name] = Animation(animation_name)

    ipos = actions[a].getAllChannelIpos()
    for bone_name in ipos:
      ipo = ipos[bone_name]
      try: nbez = ipo.getNBezPoints(0)
      except TypeError:
        print "No key frame for action %s, ipo %s, skipping..." % \
          (a, bone_name)
        nbez = 0

      bone = BONES[bone_name]
      track = animation.tracks.get(bone_name)
      if not track:
        track = animation.tracks[bone_name] = Track(animation, bone)
        track.finished = 0

      curve = []
      for ctype in ipoCurveType:
        curve.append(ipo.getCurve(ctype))

      for bez in range(nbez):
        time1 = ipo.getCurveBeztriple(0, bez)[3]
        time = (time1 - 1.0) / FPS

        if animation.duration < time:
          animation.duration = time

        loc = bone.loc
        rot = bone.rot

        if curve[0] :
            trans = vector_by_matrix((
              curve[0].evaluate(time1),
              curve[1].evaluate(time1),
              curve[2].evaluate(time1)), bone.matrix)

            loc = [             bone.loc[0] + trans[0],
              bone.loc[1] + trans[1],
              bone.loc[2] + trans[2]]

        if curve[3] :
            ipo_rot = [
              curve[3].evaluate(time1),
              curve[4].evaluate(time1),
              curve[5].evaluate(time1),
              curve[6].evaluate(time1)]

            # We need to blend the rotation from the bone rest state
            # (=bone.rot) with ipo_rot.

            rot = quaternion_multiply(ipo_rot, bone.rot)

        KeyFrame(track, time, loc, rot)





More information about the Bf-python mailing list