Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This script requires Python 3.x
- # It will convert a RTM-file (Operation Flashpoint animation) to a JSON-file
- # and back.
- #
- # Examples:
- # python rtm.py cesnapilot.rtm
- # this creates a "cesnapilot.js"
- #
- # python rtm.py cesnapilot.js
- # this creates a "cesnapilot.rtm"
- import struct
- import json
- def clean_string(s):
- return s[:s.find(b"\0")]
- class RtmLoadException(Exception):
- pass
- class RTM:
- def __init__(self):
- self.move = [0.0, 0.0, 0.0]
- self.bones = []
- self.frames = []
- def readFromPath(self, path):
- with open(path, "rb") as file:
- self.readFromFile(file)
- def readFromFile(self, file):
- magic = struct.unpack("8s", file.read(8))[0]
- if magic != b"RTM_0101":
- raise RtmLoadException("Unexpected magic value %s" % magic)
- self.move = struct.unpack("fff", file.read(4*3))
- number_of_frames = struct.unpack("i", file.read(4))[0]
- number_of_bones = struct.unpack("i", file.read(4))[0]
- for i in range(number_of_bones):
- name = clean_string(struct.unpack("32s", file.read(32))[0]).decode("cp1252")
- self.bones.append(name)
- for i in range(number_of_frames):
- frame_time = struct.unpack("f", file.read(4))[0]
- bone_transform = {}
- for ib in range(number_of_bones):
- bone = clean_string(struct.unpack("32s", file.read(32))[0]).decode("cp1252")
- transform = struct.unpack("ffffffffffff", file.read(12*4))
- bone_transform[bone] = transform
- self.frames.append({"frame_time": frame_time, "transforms": bone_transform})
- def writeToPath(self, path):
- with open(path, "wb") as file:
- self.writeToFile(file)
- def writeToFile(self, file):
- file.write(b"RTM_0101")
- file.write(struct.pack("fff", *self.move))
- file.write(struct.pack("i", len(self.frames)))
- file.write(struct.pack("i", len(self.bones)))
- for b in self.bones:
- file.write(struct.pack("32s", b.encode("cp1252")))
- for frame in self.frames:
- file.write(struct.pack("f", frame["frame_time"]))
- for bone, transform in frame["transforms"].items():
- file.write(struct.pack("32s", bone.encode("cp1252")))
- file.write(struct.pack("ffffffffffff", *transform))
- def to_json(self):
- return json.dumps({
- "move": self.move,
- "bones": self.bones,
- "frames": self.frames
- }, sort_keys=True, indent=4)
- def from_json(self, json_string):
- data = json.loads(json_string)
- self.move = data["move"]
- self.bones = data["bones"]
- self.frames = data["frames"]
- if __name__ == '__main__':
- import sys
- if len(sys.argv) <= 1:
- print("USAGE:")
- print("python rtm.py INPUT.rtm")
- print("\tConverts to JSON, creates INPUT.js\n")
- print("python rtm.py INPUT.js")
- print("\tConverts to RTM, creates INPUT.rtm")
- else:
- import os.path
- import_path = sys.argv[1]
- root, ext = os.path.splitext(import_path)
- ext = ext.lower()
- rtm = RTM()
- if ext == ".rtm":
- export_path = "{}.js".format(root)
- rtm.readFromPath(import_path)
- with open(export_path, "w", encoding="utf-8") as file:
- file.write(rtm.to_json())
- print("Exported {} successfully to {}".format(import_path, export_path))
- elif ext == ".js":
- export_path = "{}.rtm".format(root)
- rtm.from_json(open(import_path,"r",encoding="utf-8").read())
- rtm.writeToPath(export_path)
- print("Exported {} successfully to {}".format(import_path, export_path))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement