Exemplo n.º 1
0
def writeAnimation(filepath, human, config, animTrack):
    log.message("Exporting animation %s.", animTrack.name)
    numJoints = len(human.getSkeleton().getBones()) + 1

    animfilename = os.path.splitext(os.path.basename(filepath))[0]
    animfilename = animfilename + "_%s.md5anim" % (animTrack.name)
    foldername = os.path.dirname(filepath)
    animfilepath = os.path.join(foldername, animfilename)
    f = codecs.open(animfilepath, 'w', encoding="utf-8")
    f.write('MD5Version 10\n')
    f.write('commandline ""\n\n')
    f.write('numFrames %d\n' % animTrack.nFrames)
    f.write('numJoints %d\n' % numJoints)
    f.write('frameRate %d\n' % int(animTrack.frameRate))
    f.write('numAnimatedComponents %d\n\n' % (numJoints * 6))

    skel = human.getSkeleton()
    flags = 63
    f.write('hierarchy {\n')
    f.write('\t"origin" -1 %d 0\n' % flags)
    arrayIdx = 6
    for bIdx, bone in enumerate(skel.getBones()):
        #<string:jointName> <int:parentIndex> <int:flags> <int:startIndex>
        if bone.parent:
            f.write('\t"%s" %d %d %d\n' % (bone.name, bone.parent.index+1, flags, arrayIdx))
        else:
            f.write('\t"%s" %d %d %d\n' % (bone.name, 0, flags, arrayIdx))
        arrayIdx = arrayIdx + 6
    f.write('}\n\n')

    f.write('bounds {\n')
    bounds = human.meshData.calcBBox()
    if config.feetOnGround:
        bounds[:][1] += getFeetOnGroundOffset(human)
    if config.zUp:
        bounds[0] = bounds[0][[0,2,1]] * [1,-1,1]
        bounds[1] = bounds[1][[0,2,1]] * [1,-1,1]
    bounds = bounds * scale
    # TODO use bounds calculated for every frame
    for frameIdx in range(animTrack.nFrames):
        #( vec3:boundMin ) ( vec3:boundMax )
        f.write('\t( %f %f %f ) ( %f %f %f )\n' % (bounds[0][0], bounds[0][1], bounds[0][2], bounds[1][0], bounds[1][1], bounds[1][2]))
    f.write('}\n\n')

    f.write('baseframe {\n')
    f.write('\t( %f %f %f ) ( %f %f %f )\n' % (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))
    bases = []
    for bone in skel.getBones():
        pos = bone.getRestOffset() * scale
        if config.feetOnGround and not bone.parent:
            pos[1] += (getFeetOnGroundOffset(human) * scale)

        transformationMat = bone.matRestRelative.copy()
        if config.zUp:
            transformationMat = np.dot(ZYRotation, np.dot(transformationMat,la.inv(ZYRotation)))
            pos = pos[[0,2,1]] * [1,-1,1]
        orientationQuat = aljabr.matrix2Quaternion(transformationMat)

        qx = orientationQuat[0]
        qy = orientationQuat[1]
        qz = orientationQuat[2]
        w = orientationQuat[3]

        #( vec3:position ) ( vec3:orientation )
        f.write('\t( %f %f %f ) ( %f %f %f )\n' % (pos[0], pos[1], pos[2], qx, qy, qz))
        bases.append((pos, [qx, qy, qz, w]))
    f.write('}\n\n')

    for frameIdx in range(animTrack.nFrames):
        frame = animTrack.getAtFramePos(frameIdx)
        f.write('frame %d {\n' % frameIdx)
        f.write('\t%f %f %f %f %f %f\n' % (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))  # Transformation for origin joint
        for bIdx in range(numJoints-1):
            transformationMat = frame[bIdx].copy()
            pos = transformationMat[:3,3] * scale
            transformationMat[:3,3] = [0.0, 0.0, 0.0]

            if config.zUp:
                transformationMat = np.dot(ZYRotation, np.dot(transformationMat,la.inv(ZYRotation)))
                pos = pos[[0,2,1]] * [1,-1,1]

            #baseRot = aljabr.quaternion2Matrix(bases[bIdx][1])
            #transformationMat = np.dot(transformationMat[:3,:3], baseRot)

            pos += bases[bIdx][0]
            orientationQuat = aljabr.matrix2Quaternion(transformationMat)
            qx = orientationQuat[0]
            qy = orientationQuat[1]
            qz = orientationQuat[2]
            w = orientationQuat[3]

            if w > 0:
                qx = -qx
                qy = -qy
                qz = -qz

            # vec3:position vec3:orientation
            f.write('\t%f %f %f %f %f %f\n' % (pos[0], pos[1], pos[2], qx, qy, qz))
        f.write('}\n\n')

    f.close()
Exemplo n.º 2
0
 def getRestOrientationQuat(self):
     return aljabr.matrix2Quaternion(self.matRestGlobal)
Exemplo n.º 3
0
def writeAnimation(filepath, human, config, animTrack):
    log.message("Exporting animation %s.", animTrack.name)
    numJoints = len(human.getSkeleton().getBones()) + 1

    animfilename = os.path.splitext(os.path.basename(filepath))[0]
    animfilename = animfilename + "_%s.md5anim" % (animTrack.name)
    foldername = os.path.dirname(filepath)
    animfilepath = os.path.join(foldername, animfilename)
    f = open(animfilepath, 'w')
    f.write('MD5Version 10\n')
    f.write('commandline ""\n\n')
    f.write('numFrames %d\n' % animTrack.nFrames) 
    f.write('numJoints %d\n' % numJoints)
    f.write('frameRate %d\n' % int(animTrack.frameRate))
    f.write('numAnimatedComponents %d\n\n' % (numJoints * 6))

    skel = human.getSkeleton()
    flags = 63
    f.write('hierarchy {\n')
    f.write('\t"origin" -1 %d 0\n' % flags)
    arrayIdx = 6
    for bIdx, bone in enumerate(skel.getBones()):
        #<string:jointName> <int:parentIndex> <int:flags> <int:startIndex>
        if bone.parent:
            f.write('\t"%s" %d %d %d\n' % (bone.name, bone.parent.index+1, flags, arrayIdx))
        else:
            f.write('\t"%s" %d %d %d\n' % (bone.name, 0, flags, arrayIdx))
        arrayIdx = arrayIdx + 6
    f.write('}\n\n')

    f.write('bounds {\n')
    bounds = human.meshData.calcBBox()
    if config.feetOnGround:
        bounds[:][1] += getFeetOnGroundOffset(human)
    if config.zUp:
        bounds[0] = bounds[0][[0,2,1]] * [1,-1,1]
        bounds[1] = bounds[1][[0,2,1]] * [1,-1,1]
    bounds = bounds * scale
    # TODO use bounds calculated for every frame
    for frameIdx in xrange(animTrack.nFrames):
        #( vec3:boundMin ) ( vec3:boundMax )
        f.write('\t( %f %f %f ) ( %f %f %f )\n' % (bounds[0][0], bounds[0][1], bounds[0][2], bounds[1][0], bounds[1][1], bounds[1][2]))
    f.write('}\n\n')

    f.write('baseframe {\n')
    f.write('\t( %f %f %f ) ( %f %f %f )\n' % (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))
    bases = []
    for bone in skel.getBones():
        pos = bone.getRestOffset() * scale
        if config.feetOnGround and not bone.parent:
            pos[1] += (getFeetOnGroundOffset(human) * scale)

        transformationMat = bone.matRestRelative.copy()
        if config.zUp:
            transformationMat = np.dot(ZYRotation, np.dot(transformationMat,la.inv(ZYRotation)))
            pos = pos[[0,2,1]] * [1,-1,1]
        orientationQuat = aljabr.matrix2Quaternion(transformationMat)

        qx = orientationQuat[0]
        qy = orientationQuat[1]
        qz = orientationQuat[2]
        w = orientationQuat[3]

        #( vec3:position ) ( vec3:orientation )
        f.write('\t( %f %f %f ) ( %f %f %f )\n' % (pos[0], pos[1], pos[2], qx, qy, qz))
        bases.append((pos, [qx, qy, qz, w]))
    f.write('}\n\n')

    for frameIdx in xrange(animTrack.nFrames):
        frame = animTrack.getAtFramePos(frameIdx)
        f.write('frame %d {\n' % frameIdx)
        f.write('\t%f %f %f %f %f %f\n' % (0.0, 0.0, 0.0, 0.0, 0.0, 0.0))  # Transformation for origin joint
        for bIdx in xrange(numJoints-1):
            transformationMat = frame[bIdx].copy()
            pos = transformationMat[:3,3] * scale
            transformationMat[:3,3] = [0.0, 0.0, 0.0]

            if config.zUp:
                transformationMat = np.dot(ZYRotation, np.dot(transformationMat,la.inv(ZYRotation)))
                pos = pos[[0,2,1]] * [1,-1,1]

            #baseRot = aljabr.quaternion2Matrix(bases[bIdx][1])
            #transformationMat = np.dot(transformationMat[:3,:3], baseRot)

            pos += bases[bIdx][0]
            orientationQuat = aljabr.matrix2Quaternion(transformationMat)
            qx = orientationQuat[0]
            qy = orientationQuat[1]
            qz = orientationQuat[2]
            w = orientationQuat[3]

            if w > 0:
                qx = -qx
                qy = -qy
                qz = -qz

            # vec3:position vec3:orientation
            f.write('\t%f %f %f %f %f %f\n' % (pos[0], pos[1], pos[2], qx, qy, qz))
        f.write('}\n\n')

    f.close()
Exemplo n.º 4
0
 def getRestOrientationQuat(self):
     return aljabr.matrix2Quaternion(self.matRestGlobal)