File size: 1,470 Bytes
c1e2af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tempfile
import unittest
from pathlib import Path

import numpy as np

from ardy.exports.bvh import write_bvh


class BvhExportTests(unittest.TestCase):
    def test_writes_blender_readable_hierarchy_and_motion(self):
        rotations = np.repeat(np.eye(3)[None, None], 4, axis=0)
        rotations = np.repeat(rotations, 2, axis=1)
        roots = np.array(
            [
                [0.0, 0.0, 0.0],
                [0.1, 0.0, 0.0],
                [0.2, 0.0, 0.0],
                [0.3, 0.0, 0.0],
            ]
        )

        with tempfile.TemporaryDirectory() as folder:
            output = Path(folder) / "motion.bvh"
            write_bvh(
                output,
                joint_names=["Hips", "Spine"],
                parents=[-1, 0],
                rest_positions=np.array([[0.0, 0.0, 0.0], [0.0, 0.5, 0.0]]),
                local_rot_mats=rotations,
                root_positions=roots,
                fps=20,
            )
            text = output.read_text(encoding="utf-8")

        self.assertIn("ROOT Hips", text)
        self.assertIn("JOINT Spine", text)
        self.assertIn("Frames: 4", text)
        self.assertIn("Frame Time: 0.050000000", text)
        motion_lines = text.split("Frame Time: 0.050000000\n", 1)[1].strip().splitlines()
        self.assertEqual(len(motion_lines), 4)
        self.assertTrue(motion_lines[-1].startswith("0.3000000 0.0000000 0.0000000"))


if __name__ == "__main__":
    unittest.main()