-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmd.py
65 lines (49 loc) · 2.26 KB
/
vmd.py
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
# VMD format & functions
# source : https://blog.goo.ne.jp/torisu_tetosuki/e/bc9f1c4d597341b394bd02b64597499d
from collections import namedtuple
from io import BufferedReader, TextIOWrapper
from struct import unpack
# VMD formats
VMD_HEADER = namedtuple("VMD_HEADER",
["VmdHeader", "VmdModelName"])
VMD_MOTION_COUNT = namedtuple("VMD_MOTION_COUNT",
["Count"])
VMD_MOTION = namedtuple("VMD_MOTION",
["BoneName", "FrameNo", "Location", "Rotation", "Interpolation"])
VMD_SKIN_COUNT = namedtuple("VMD_SKIN_COUNT",
["Count"])
VMD_SKIN = namedtuple("VMD_SKIN",
["SkinName", "FrameNo", "Weight"])
VMD_CAMERA_COUNT = namedtuple("VMD_CAMERA_COUNT",
["Count"])
VMD_CAMERA = namedtuple("VMD_CAMERA",
["FrameNo", "Length", "Location", "Rotation",
"Interpolation", "ViewingAngle", "Perspective"])
VMD_LIGHT_COUNT = namedtuple("VMDF_LIGHT_COUNT",
["Count"])
VMD_LIGHT = namedtuple("VMD_LIGHT",
["FrameNo", "RGB", "Location"])
VMD_SELF_SHADOW_COUNT = namedtuple("VMD_SELF_SHADOW_COUNT",
["Count"])
VMD_SELF_SHADOW = namedtuple("VMD_SELF_SHADOW",
["FrameNo", "Mode", "Distance"])
# function to decode bytes
def vmdread(file: BufferedReader, count: int, fmt: str) -> str|int|tuple:
data_bytes = file.read(count)
data = unpack(fmt, data_bytes)
if len(data) == 1:
if type(data[0]) == bytes:
return data[0].rstrip(b'\x00').decode("shift-jis", errors="ignore") # str
else:
return data[0] # int
else:
return data # tuple
# function to write data to output file
def vmdoutput(outputfile: TextIOWrapper, content: int|str|tuple):
if type(content) == tuple: # if needed to output each field
for i in range(len(content)):
print(content[i], end="", file=outputfile)
print(" ", sep="", end="", file=outputfile)
print("", file=outputfile)
else:
print(content, file=outputfile)