Advertisement
Guest User

RTM-JSON conversion script

a guest
Jun 20th, 2012
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. # This script requires Python 3.x
  2. # It will convert a RTM-file (Operation Flashpoint animation) to a JSON-file
  3. # and back.
  4. #
  5. # Examples:
  6. #     python rtm.py cesnapilot.rtm
  7. # this creates a "cesnapilot.js"
  8. #
  9. #     python rtm.py cesnapilot.js
  10. # this creates a "cesnapilot.rtm"
  11.  
  12. import struct
  13. import json
  14.  
  15. def clean_string(s):
  16.    return s[:s.find(b"\0")]
  17.  
  18. class RtmLoadException(Exception):
  19.    pass
  20.  
  21. class RTM:
  22.    def __init__(self):
  23.       self.move = [0.0, 0.0, 0.0]
  24.       self.bones = []
  25.       self.frames = []
  26.    
  27.    def readFromPath(self, path):
  28.       with open(path, "rb") as file:
  29.          self.readFromFile(file)
  30.    
  31.    def readFromFile(self, file):
  32.       magic = struct.unpack("8s", file.read(8))[0]
  33.       if magic != b"RTM_0101":
  34.          raise RtmLoadException("Unexpected magic value %s" % magic)
  35.      
  36.       self.move = struct.unpack("fff", file.read(4*3))
  37.       number_of_frames = struct.unpack("i", file.read(4))[0]
  38.       number_of_bones = struct.unpack("i", file.read(4))[0]
  39.      
  40.       for i in range(number_of_bones):
  41.          name = clean_string(struct.unpack("32s", file.read(32))[0]).decode("cp1252")
  42.          self.bones.append(name)
  43.      
  44.       for i in range(number_of_frames):
  45.          frame_time = struct.unpack("f", file.read(4))[0]
  46.          bone_transform = {}
  47.          for ib in range(number_of_bones):
  48.             bone = clean_string(struct.unpack("32s", file.read(32))[0]).decode("cp1252")
  49.             transform = struct.unpack("ffffffffffff", file.read(12*4))
  50.             bone_transform[bone] = transform
  51.          self.frames.append({"frame_time": frame_time, "transforms": bone_transform})
  52.    
  53.    def writeToPath(self, path):
  54.       with open(path, "wb") as file:
  55.          self.writeToFile(file)
  56.    
  57.    def writeToFile(self, file):
  58.       file.write(b"RTM_0101")
  59.       file.write(struct.pack("fff", *self.move))
  60.      
  61.       file.write(struct.pack("i", len(self.frames)))
  62.       file.write(struct.pack("i", len(self.bones)))
  63.      
  64.       for b in self.bones:
  65.          file.write(struct.pack("32s", b.encode("cp1252")))
  66.      
  67.       for frame in self.frames:
  68.          file.write(struct.pack("f", frame["frame_time"]))
  69.          for bone, transform in frame["transforms"].items():
  70.             file.write(struct.pack("32s", bone.encode("cp1252")))
  71.             file.write(struct.pack("ffffffffffff", *transform))
  72.    
  73.    def to_json(self):
  74.       return json.dumps({
  75.          "move": self.move,
  76.          "bones": self.bones,
  77.          "frames": self.frames
  78.          }, sort_keys=True, indent=4)
  79.    
  80.    def from_json(self, json_string):
  81.       data = json.loads(json_string)
  82.       self.move = data["move"]
  83.       self.bones = data["bones"]
  84.       self.frames = data["frames"]
  85.  
  86. if __name__ == '__main__':
  87.    import sys
  88.    if len(sys.argv) <= 1:
  89.       print("USAGE:")
  90.       print("python rtm.py INPUT.rtm")
  91.       print("\tConverts to JSON, creates INPUT.js\n")
  92.       print("python rtm.py INPUT.js")
  93.       print("\tConverts to RTM, creates INPUT.rtm")
  94.    else:
  95.       import os.path
  96.       import_path = sys.argv[1]
  97.       root, ext = os.path.splitext(import_path)
  98.       ext = ext.lower()
  99.      
  100.       rtm = RTM()
  101.      
  102.       if ext == ".rtm":
  103.          export_path = "{}.js".format(root)
  104.          rtm.readFromPath(import_path)
  105.          with open(export_path, "w", encoding="utf-8") as file:
  106.             file.write(rtm.to_json())
  107.             print("Exported {} successfully to {}".format(import_path, export_path))
  108.       elif ext == ".js":
  109.          export_path = "{}.rtm".format(root)
  110.          rtm.from_json(open(import_path,"r",encoding="utf-8").read())
  111.          rtm.writeToPath(export_path)
  112.          print("Exported {} successfully to {}".format(import_path, export_path))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement