#!/usr/bin/python3 # by Joric, https://github.com/joric/io_scene_b3d import os import struct class B3DParser: def __init__(self): self.fp = None def gets(self): s = b'' while True: c = self.fp.read(1) if c == b'\x00': return s.decode(errors='ignore') s += c def i(self,n): return struct.unpack(n*'i', self.fp.read(n*4)) def f(self,n): return struct.unpack(n*'f', self.fp.read(n*4)) def next_chunk(self): pos = self.fp.tell() s1,s2,s3,s4, size = struct.unpack('4ci', self.fp.read(8)) chunk = ''.join([chr(ord(x)) for x in (s1,s2,s3,s4)]) next = pos + size + 8 return chunk, pos, size, next def cb_result(self): return True def parse(self, filepath): filesize = os.stat(filepath).st_size self.fp = open(filepath,'rb') stack = [] while self.fp.tell() <= filesize-8: while stack and stack[-1]==self.fp.tell(): del stack[-1] self.cb_prev() chunk, pos, size, next = self.next_chunk() if chunk=='BB3D': self.cb_data(chunk, {'version': self.i(1)[0]}) continue if chunk=='ANIM': flags, frames = self.i(2) fps = self.f(1)[0] self.cb_data(chunk, {'flags':flags, 'frames':frames, 'fps':fps}) elif chunk=='TEXS': data = [] while self.fp.tell()