File size: 3,180 Bytes
a84fca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Parse a BVH file's HIERARCHY and MOTION headers.

Only the header is read — motion rows are skipped — so probing a whole library
costs almost nothing. Pass `motion=True` to also scan the rows and report whether
any channel actually changes: a clip can carry 91 frames that all hold the same
pose, and every T-pose does.
"""
import os, re, sys

def probe(path, motion=False):
    joints, ends, root = [], 0, None
    frames, frame_time = 0, 0.0
    depth, max_depth = 0, 0
    in_motion = False
    has_motion = False
    with open(path, "r", encoding="utf-8", errors="replace") as f:
        for line in f:
            s = line.strip()
            if not in_motion:
                if s.startswith("ROOT "):
                    root = s[5:].strip(); joints.append(root)
                elif s.startswith("JOINT "):
                    joints.append(s[6:].strip())
                elif s.startswith("End Site"):
                    ends += 1
                elif s == "{":
                    depth += 1; max_depth = max(max_depth, depth)
                elif s == "}":
                    depth -= 1
                elif s == "MOTION":
                    in_motion = True
            else:
                if s.startswith("Frames:"):
                    try: frames = int(s.split(":", 1)[1])
                    except ValueError: pass
                elif s.lower().startswith("frame time"):
                    try: frame_time = float(s.split(":", 1)[1])
                    except ValueError: pass
                    if not motion:
                        break                  # header done; skip the motion rows
                    lo = hi = None
                    for row in f:
                        try:
                            vals = [float(x) for x in row.split()]
                        except ValueError:
                            continue
                        if not vals:
                            continue
                        if lo is None:
                            lo, hi = list(vals), list(vals)
                        else:
                            n = min(len(lo), len(vals))
                            for i in range(n):
                                if vals[i] < lo[i]: lo[i] = vals[i]
                                if vals[i] > hi[i]: hi[i] = vals[i]
                    has_motion = bool(lo) and max(h - l for l, h in zip(lo, hi)) > 1e-6
                    break
    fps = round(1.0 / frame_time, 3) if frame_time > 0 else 0.0
    out = {
        "root": root or "", "num_joints": len(joints), "num_end_sites": ends,
        "max_depth": max_depth, "frames": frames,
        "frame_time": round(frame_time, 6), "fps": fps,
        "duration_sec": round(frames * frame_time, 4) if frame_time > 0 else 0.0,
        "joint_names": "|".join(joints)[:400],
    }
    if motion:
        out["has_motion"] = has_motion
    return out

if __name__ == "__main__":
    for p in sys.argv[1:]:
        r = probe(p)
        print("%-34s joints=%-4d frames=%-5d fps=%-5s %.2fs root=%s" % (
            os.path.basename(p), r["num_joints"], r["frames"], r["fps"],
            r["duration_sec"], r["root"]))