File size: 2,576 Bytes
da855ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import sys, os, logging
import pyfbsdk

# Customs
# MoBu env
my_env = 'C:/Users/94086/.conda/envs/zeggs/Lib/site-packages'
sys.path.append(my_env)


# import numpy as np

logging.basicConfig(
    filename="compile_results.log",
    filemode="w",
    format="%(asctime)s %(levelname)-8s %(message)s",
    level=logging.DEBUG,
)

console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(logging.Formatter("%(asctime)s %(levelname)-8s %(message)s"))
logging.getLogger("").addHandler(console)


def bvh2fbx(animation_file, output_file, template_file, sound_file=None):

    pyfbsdk.FBApplication().FileNew()
    logging.info("Loading %s..." % str(template_file))

    if not pyfbsdk.FBApplication().FileOpen(str(template_file)):
        raise IOError("Could not open file: {}".format(str(template_file)))

    if sound_file is not None:
        # Load Audio
        logging.info("Loading %s..." % str(sound_file))
        audio = pyfbsdk.FBAudioClip(sound_file)
        if audio is None:
            raise IOError("Could not open file: {}".format(str(sound_file)))

        # Rescale Timespan
        pyfbsdk.FBSystem().CurrentTake.LocalTimeSpan = pyfbsdk.FBTimeSpan(
            pyfbsdk.FBTime(0), audio.Duration
        )

    # Set FPS
    pyfbsdk.FBPlayerControl().SetTransportFps(pyfbsdk.FBTimeMode.kFBTimeMode60Frames)
    pyfbsdk.FBPlayerControl().SnapMode = (
        pyfbsdk.FBTransportSnapMode.kFBTransportSnapModeSnapOnFrames
    )

    # Load BVH
    if not pyfbsdk.FBApplication().FileImport(animation_file, True):
        raise IOError("Could not open file: {}".format(str(animation_file)))

    # Save FBX
    pyfbsdk.FBApplication().FileSave(output_file)


if True:
    try:

        logging.info("======")
        logging.info("BVH2FBX")
        logging.info("======")

        results_path = "./Rendered"
        template_file = "./LaForgeFemale.fbx"

        # Characterizing all bvh files
        for animation_file in [f for f in os.listdir(results_path) if f.endswith(".bvh")]:
            sound_file = animation_file.replace(".bvh", ".wav")
            sound_file = results_path + "/" + sound_file
            if not os.path.exists(sound_file):
                sound_file = None
            bvh2fbx(
                results_path + "/" + animation_file,
                results_path + "/" + animation_file.replace(".bvh", ".fbx"),
                template_file,
                sound_file
            )

        pyfbsdk.FBApplication().FileExit()

    except Exception as e:
        logging.exception("FAILED:")
        raise e