You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
322 lines
12 KiB
Python
322 lines
12 KiB
Python
#! /usr/bin/python3
|
|
import sys
|
|
|
|
try:
|
|
from .bones import *
|
|
from .util import Data
|
|
from .geomesh import GeoMesh, GeoFace, GeoVertex
|
|
from .compression_anim import *
|
|
except:
|
|
from bones import *
|
|
from util import *
|
|
from geomesh import GeoMesh, GeoFace, GeoVertex
|
|
from compression_anim import *
|
|
|
|
|
|
#.anim header
|
|
#Offset Size Description
|
|
#0 4 (int32) headerSize Size of this header plus BoneAnimTrack data and skeleton hierarchy data.
|
|
#4 256 (asciiz) name
|
|
#260 256 (asciiz) baseAnimName
|
|
#516 4 (float32) max_hip_displacement
|
|
#520 4 (float32) length
|
|
#524 4 (ptr u32) bone_tracks Offset to array of BoneAnimTrack structures.
|
|
#528 4 (int32) bone_track_count
|
|
#532 4 (int32) rotation_compression_type ??
|
|
#536 4 (int32) position_compression_type ??
|
|
#540 4 (ptr u32) skeletonHeirarchy ?? SkeletonHeirarchy
|
|
#Below are reserved for use by the game.
|
|
#544 4 (ptr u32) backupAnimTrack *SkeletonAnimTrack
|
|
#548 4 (int32) loadstate
|
|
#552 4 (float32) lasttimeused
|
|
#556 4 (int32) fileAge
|
|
#560 4*9 (int32[9]) spare_room
|
|
#596 -
|
|
|
|
#BoneAnimTrack
|
|
#0 4 (ptr u32) rot_idx ?? Offset to the rotation data. Format depends on compression method.
|
|
#4 4 (ptr u32) pos_idx ?? Offset to the position data. Format depends on compression method.
|
|
#8 2 (u16) rot_fullkeycount ??
|
|
#10 2 (u16) pos_fullkeycount ??
|
|
#12 2 (u16) rot_count ??
|
|
#14 2 (u16) pos_count ??
|
|
#16 1 (char) id Bone ID.
|
|
#17 1 (char) flags ??
|
|
#18 2 (u16) pack_pad Padding to push the structure to 4 byte alignment.
|
|
#20 -
|
|
|
|
#SkeletonHeirarchy
|
|
#0 4 (int) heirarchy_root ?? First bone in the heirarchy.
|
|
#4 12*100 (BoneLink[BONES_ON_DISK])
|
|
#1204 -
|
|
#BONES_ON_DISK = 100
|
|
|
|
#BoneLink
|
|
#0 4 (int) child ?? First child bone?
|
|
#4 4 (int) next ?? The next sibling bone?
|
|
#8 4 (int/BoneId) id The ID of this bone.
|
|
#12 -
|
|
|
|
ROTATION_UNCOMPRESSED = 1 << 0
|
|
ROTATION_COMPRESSED_TO_5_BYTES = 1 << 1
|
|
ROTATION_COMPRESSED_TO_8_BYTES = 1 << 2
|
|
POSITION_UNCOMPRESS = 1 << 3
|
|
POSITION_COMPRESSED_TO_6_BYTES = 1 << 4
|
|
ROTATION_DELTACODED = 1 << 5
|
|
POSITION_DELTACODED = 1 << 6
|
|
ROTATION_COMPRESSED_NONLINEAR = 1 << 7
|
|
|
|
ROTATION_MASK = ROTATION_UNCOMPRESSED | ROTATION_COMPRESSED_TO_5_BYTES | ROTATION_COMPRESSED_TO_8_BYTES | ROTATION_DELTACODED | ROTATION_COMPRESSED_NONLINEAR
|
|
POSITION_MASK = POSITION_UNCOMPRESS | POSITION_COMPRESSED_TO_6_BYTES | POSITION_DELTACODED
|
|
|
|
|
|
class BoneLink:
|
|
def __init__(self, data_or_child = None, next = None, boneid = None):
|
|
self.child = -1
|
|
self.next = -1
|
|
self.boneid = -1
|
|
if data_or_child is None:
|
|
return
|
|
if next is None:
|
|
(self.child, self.next, self.boneid) = data_or_child.decode("<iii")
|
|
else:
|
|
self.child = data_or_child
|
|
self.next = next
|
|
self.boneid = boneid
|
|
|
|
def dump(self):
|
|
print("bone: %s (%s)" % (self.boneid, BONES_LIST[self.boneid]))
|
|
print(" child: %s (%s)" % (self.child, BONES_LIST[self.child]))
|
|
print(" next: %s (%s)" % (self.next, BONES_LIST[self.next]))
|
|
|
|
class SkeletonHierarchy:
|
|
def __init__(self):
|
|
self.root = -1
|
|
self.bones = []
|
|
def parseData(self, data):
|
|
self.root = data.decode("<i")[0]
|
|
self.bones = []
|
|
for i in range(70): #BONES_ON_DISK):
|
|
self.bones.append(BoneLink(data))
|
|
def dump(self):
|
|
print("SkeletonHierarchy: root: %s (%s)" % (self.root, BONES_LIST[self.root]))
|
|
for b in self.bones:
|
|
b.dump()
|
|
|
|
class BoneAnimTrack:
|
|
def __init__(self, data = b"", srcdata = None):
|
|
self.rawdata = data
|
|
self.srcdata = srcdata
|
|
self.rotations_data_offset = 0
|
|
self.positions_data_offset = 0
|
|
self.rotations = []
|
|
self.positions = []
|
|
self.rot_fullkeycount = 0
|
|
self.pos_fullkeycount = 0
|
|
self.rot_count = 0
|
|
self.pos_count = 0
|
|
self.bone_id = 0
|
|
self.flags = 0
|
|
self.padding = b""
|
|
if len(data):
|
|
self.parseData()
|
|
def parseData(self):
|
|
self.data = Data(self.rawdata)
|
|
self.rotations_data_offset = self.data.decode("I")[0]
|
|
self.positions_data_offset = self.data.decode("I")[0]
|
|
self.rot_fullkeycount = self.data.decode("H")[0]
|
|
self.pos_fullkeycount = self.data.decode("H")[0]
|
|
self.rot_count = self.data.decode("H")[0]
|
|
self.pos_count = self.data.decode("H")[0]
|
|
self.bone_id = self.data.decode("B")[0]
|
|
self.flags = self.data.decode("B")[0]
|
|
self.padding = self.data.read(2)
|
|
self.parseRotations()
|
|
self.parsePositions()
|
|
def parseRotations(self):
|
|
self.rotations = []
|
|
offset = self.srcdata.offset
|
|
self.srcdata.seek(self.rotations_data_offset)
|
|
method = self.flags & ROTATION_MASK
|
|
if method == ROTATION_UNCOMPRESSED:
|
|
for i in range(self.rot_count):
|
|
self.rotations.append(self.srcdata.decode("<ffff"))
|
|
pass
|
|
elif method == ROTATION_COMPRESSED_TO_5_BYTES:
|
|
for i in range(self.rot_count):
|
|
self.rotations.append(decompressQuaternion_5Byte(self.srcdata.read(5)))
|
|
pass
|
|
elif method == ROTATION_COMPRESSED_TO_8_BYTES:
|
|
for i in range(self.rot_count):
|
|
self.rotations.append(decompressQuaternion_8Byte(self.srcdata.read(8)))
|
|
pass
|
|
elif method == ROTATION_DELTACODED:
|
|
raise
|
|
pass
|
|
elif method == ROTATION_COMPRESSED_NONLINEAR:
|
|
raise Exception("Unsupported rotation compression: ROTATION_COMPRESSED_NONLINEAR")
|
|
pass
|
|
else:
|
|
#raise Exception("bone track rotation type 0x%2.2x unknown" % (self.flags, ))
|
|
pass
|
|
self.srcdata.seek(offset)
|
|
def parsePositions(self):
|
|
self.positions = []
|
|
offset = self.srcdata.offset
|
|
self.srcdata.seek(self.positions_data_offset)
|
|
method = self.flags & POSITION_MASK
|
|
if method == POSITION_UNCOMPRESS:
|
|
for i in range(self.pos_count):
|
|
self.positions.append(self.srcdata.decode("<fff"))
|
|
pass
|
|
elif method == POSITION_COMPRESSED_TO_6_BYTES:
|
|
for i in range(self.pos_count):
|
|
self.positions.append(decompressVector3_6Byte(self.srcdata.read(6)))
|
|
pass
|
|
elif method == POSITION_DELTACODED:
|
|
raise
|
|
pass
|
|
else:
|
|
#raise Exception("bone track position type 0x%2.2x unknown" % (self.flags, ))
|
|
pass
|
|
self.srcdata.seek(offset)
|
|
def dump(self):
|
|
print("rotations_data_offset: %s" % (self.rotations_data_offset, ))
|
|
print("positions_data_offset: %s" % (self.positions_data_offset, ))
|
|
print("rot_fullkeycount: %s" % (self.rot_fullkeycount, ))
|
|
print("pos_fullkeycount: %s" % (self.pos_fullkeycount, ))
|
|
print("rot_count: %s" % (self.rot_count, ))
|
|
print("pos_count: %s" % (self.pos_count, ))
|
|
if self.bone_id > len(BONES_LIST):
|
|
print("bone_id: %s (?)" % (self.bone_id, ))
|
|
else:
|
|
print("bone_id: %s %s" % (self.bone_id, BONES_LIST[self.bone_id]))
|
|
print("flags: 0x%2.2x" % (self.flags, ))
|
|
print("padding: %s" % (self.padding, ))
|
|
print("rotations: %s" % (self.rotations, ))
|
|
print("positions: %s" % (self.positions, ))
|
|
|
|
|
|
class Anim:
|
|
def __init__(self):
|
|
self.data = b""
|
|
self.offset = 0
|
|
self.version = -1
|
|
|
|
self.header_size = -1
|
|
self.header_name = ""
|
|
self.header_base_anim_name = ""
|
|
self.header_max_hip_displacement = 0
|
|
self.header_length = 0
|
|
self.header_bone_tracks_offset = 0
|
|
self.header_bone_track_count = 0
|
|
self.header_rotation_compression_type = 0
|
|
self.header_position_compression_type = 0
|
|
self.header_skeleton_heirarchy_offset = 0
|
|
self.header_backup_anim_track = 0
|
|
self.header_load_state = 0
|
|
self.header_last_time_used = 0
|
|
self.header_file_age = 0
|
|
self.header_spare_room = b""
|
|
|
|
self.bone_tracks = []
|
|
self.skeleton_heirarchy = []
|
|
|
|
pass
|
|
def loadFromData(self, data):
|
|
self.data = Data(data)
|
|
self.offset = 0
|
|
self.parseData()
|
|
pass
|
|
def loadFromFile(self, filein):
|
|
self.data = Data(filein.read())
|
|
self.offset = 0
|
|
self.parseData()
|
|
pass
|
|
def saveToData(self):
|
|
self.encode()
|
|
return self.data
|
|
def saveToFile(self, fileout):
|
|
self.encode()
|
|
fileout.write(self.data)
|
|
def parseData(self):
|
|
self.data.seek(0)
|
|
self.header_size = self.data.decode("i")[0]
|
|
self.header_name = extractString(self.data.read(256))
|
|
self.header_base_anim_name = extractString(self.data.read(256))
|
|
self.header_max_hip_displacement = self.data.decode("f")[0]
|
|
self.header_length = self.data.decode("f")[0]
|
|
self.header_bone_tracks_offset = self.data.decode("I")[0]
|
|
self.header_bone_track_count = self.data.decode("i")[0]
|
|
self.header_rotation_compression_type = self.data.decode("i")[0]
|
|
self.header_position_compression_type = self.data.decode("i")[0]
|
|
self.header_skeleton_heirarchy_offset = self.data.decode("I")[0]
|
|
self.header_backup_anim_track = self.data.decode("I")[0]
|
|
self.header_load_state = self.data.decode("i")[0]
|
|
self.header_last_time_used = self.data.decode("f")[0]
|
|
self.header_file_age = self.data.decode("f")[0]
|
|
self.header_spare_room = self.data.read(4 * 9)
|
|
|
|
self.bone_tracks = []
|
|
if self.header_bone_tracks_offset > 0:
|
|
self.data.seek(self.header_bone_tracks_offset)
|
|
for i in range(self.header_bone_track_count):
|
|
self.bone_tracks.append(BoneAnimTrack(self.data.read(20), self.data))
|
|
self.skeleton_heirarchy = None
|
|
if self.header_skeleton_heirarchy_offset > 0:
|
|
self.data.seek(self.header_skeleton_heirarchy_offset)
|
|
self.skeleton_heirarchy = SkeletonHierarchy()
|
|
self.skeleton_heirarchy.parseData(self.data)
|
|
pass
|
|
def encode(self):
|
|
self.data = None
|
|
|
|
def dump(self):
|
|
print("header_size: %s" % (self.header_size, ))
|
|
print("header_name: %s" % (self.header_name, ))
|
|
print("header_base_anim_name: %s" % (self.header_base_anim_name, ))
|
|
print("header_max_hip_displacement: %s" % (self.header_max_hip_displacement, ))
|
|
print("header_length: %s" % (self.header_length, ))
|
|
print("header_bone_tracks_offset: %s" % (self.header_bone_tracks_offset, ))
|
|
print("header_bone_track_count: %s" % (self.header_bone_track_count, ))
|
|
print("header_rotation_compression_type: %s" % (self.header_rotation_compression_type, ))
|
|
print("header_position_compression_type: %s" % (self.header_position_compression_type, ))
|
|
print("header_skeleton_heirarchy_offset: %s" % (self.header_skeleton_heirarchy_offset, ))
|
|
print("header_backup_anim_track: %s" % (self.header_backup_anim_track, ))
|
|
print("header_load_state: %s" % (self.header_load_state, ))
|
|
print("header_last_time_used: %s" % (self.header_last_time_used, ))
|
|
print("header_file_age: %s" % (self.header_file_age, ))
|
|
print("header_spare_room: %s" % (self.header_spare_room, ))
|
|
|
|
print("Bone Tracks:")
|
|
for bt in self.bone_tracks:
|
|
bt.dump()
|
|
if self.skeleton_heirarchy is not None:
|
|
self.skeleton_heirarchy.dump()
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) <= 1 or len(sys.argv) > 3:
|
|
print("Usage:")
|
|
print(" %s <file_in> [<file_out>]" % (sys.argv[0], ))
|
|
print("Test loads a .anim file, dumps its content, and optionally writes its content out.")
|
|
exit(0)
|
|
fh = open(sys.argv[1], "rb")
|
|
anim = Anim()
|
|
anim.loadFromFile(fh)
|
|
fh.close()
|
|
#print(sys.argv)
|
|
if len(sys.argv) <= 2:
|
|
fho = None
|
|
else:
|
|
fho = open(sys.argv[2], "wb")
|
|
if fho is not None:
|
|
data = anim.saveToData()
|
|
#anim.dump()
|
|
fho.write(data)
|
|
else:
|
|
anim.dump()
|
|
#print("%s" % [anim.header_data])
|
|
|