""" MIT License Copyright (c) 2020 Benjamin Collins (kion @ dashgl.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import os import struct from NinjaBone import NinjaBone class NinjaModel: # { def __init__(self, filename): # { # File Information self.path = 'input/' + filename self.file = open(self.path, 'rb') self.length = os.path.getsize(self.path) # Textures self.tex_list = [] # Bone List self.bones = [] #Vertex List self.mat_list = [] self.index_lookup = [ None ] * 5000 self.vertex_list = [] self.face_list = [] return None # } def parse(self): # { while self.file.tell() < self.length: # { bytes = self.file.read(4) if(bytes == 'NJTL') : print('Found Ninja Texture List') bytes = self.file.read(4) len = struct.unpack('I', bytes)[0] pos = self.file.tell() + len self.readNjtl() self.file.seek(pos, 0) elif (bytes == 'NJCM') : print('Found Ninja Chunk Model') bytes = self.file.read(4) len = struct.unpack('I', bytes)[0] pos = self.file.tell() + len self.pof = self.file.tell() self.readNjcm(None) self.file.seek(pos, 0) elif (bytes == 'NMDM') : print('Found Ninja Direct Motion') # } return None # } def readNjtl(self): pof = self.file.tell() bytes = self.file.read(8) n = struct.unpack('II', bytes) str_ofs = [] self.file.seek(n[0] + pof, 0) for i in range(0, n[1]): bytes = self.file.read(12) m = struct.unpack('III', bytes) str_ofs.append(m[0] + pof) for ofs in str_ofs: str = '' self.file.seek(ofs, 0) while 1: # { ch = self.file.read(1) if(ch == '\0'): break str += ch self.tex_list.append(str) return None def readNjcm(self, parentBone): bytes = self.file.read(52) b = struct.unpack('IIfffiiifffII', bytes) flags = b[0] model_ofs = b[1] pos = ( b[2], b[3], b[4] ) rot = ( b[5], b[6], b[7] ) scl = ( b[8], b[9], b[10] ) child_ofs = b[11] sibling_ofs = b[12] self.bone = NinjaBone() if ( (flags & 0x04) == 0): self.bone.setScale(scl) if ( (flags & 0x02) == 0): self.bone.setRotation(rot, flags & 0x20) if ( (flags & 0x01) == 0): self.bone.setPosition(pos) num = len(self.bones) self.bone.setName(num) self.bones.append(self.bone) print(self.bone.name) if parentBone: parentBone.add(self.bone) if model_ofs: self.file.seek(model_ofs + self.pof, 0) self.readModel() if child_ofs: self.file.seek(child_ofs + self.pof, 0) self.readNjcm(self.bone) if sibling_ofs: self.file.seek(sibling_ofs + self.pof, 0) self.readNjcm(parentBone) return None def readModel(self): bytes = self.file.read(24) m = struct.unpack('IIffff', bytes) vertex_ofs = m[0] chunk_ofs = m[1] center = (m[2], m[3], m[4]) radius = m[5] if vertex_ofs: self.file.seek(vertex_ofs + self.pof, 0) self.readVertexList() if chunk_ofs: self.file.seek(chunk_ofs + self.pof, 0) self.readChunkList() return None def readVertexList(self): print("Reading vertex list") bytes = self.file.read(8) c = struct.unpack('BBHHH', bytes) chunk_head = c[0] chunk_flag = c[1] chunk_len = c[2] vertex_ofs = c[3] vertex_count = c[4] if chunk_head != 41: print("ERROR!!! NEW VERTEX TYPE!!!!") print("Chunk Head: %d" % chunk_head) print("Chunk Flag: %d" % chunk_flag) print("Vertex Ofs: %d" % vertex_ofs) print("Vertex Count: %d" % vertex_count) for i in range(vertex_count): bytes = self.file.read(24) v = struct.unpack('ffffff', bytes) vertex = { 'pos' : [ v[0], v[1], v[2] ], 'norm' : [ v[3], v[4], v[5] ] } self.bone.apply(vertex['pos']) self.index_lookup[vertex_ofs] = len(self.vertex_list) vertex_ofs += 1 self.vertex_list.append(vertex) pos = self.file.tell() bytes = self.file.read(2) c = struct.unpack('BB', bytes) chunk_head = c[0] chunk_flag = c[1] if chunk_head != 255: print("ERROR ANOTHER VERTEX LIST DETECTED") print("End Chunk Head: %d" % chunk_head) print("End Chunk Flag: %d" % chunk_flag) return None def readChunkList(self): print("---- Reading Chunk List ---") print("Chunk Offset: 0x%08x" % self.file.tell()) while 1: bytes = self.file.read(2) c = struct.unpack('BB', bytes) chunk_head = c[0] chunk_flag = c[1] if chunk_head == 255: break elif chunk_head == 0: print("Null Chunk") continue elif chunk_head >= 17 and chunk_head <= 23: print("Chunk: Material") bytes = self.file.read(2) short_len = struct.unpack('H', bytes) self.material = {} dst_alpha = chunk_flag & 0x07 src_alpha = chunk_flag >> 3 if chunk_head & 0x01: bytes = self.file.read(4) c = struct.unpack('BBBB', bytes) self.material['diffuse'] = { 'b' : c[0] / 255.0, 'g' : c[1] / 255.0, 'r' : c[2] / 255.0, 'a' : c[3] / 255.0 } if chunk_head & 0x02: bytes = self.file.read(4) c = struct.unpack('BBBB', bytes) self.material['ambient'] = { 'b' : c[0] / 255.0, 'g' : c[1] / 255.0, 'r' : c[2] / 255.0, 'a' : c[3] / 255.0 } if chunk_head & 0x04: bytes = self.file.read(4) c = struct.unpack('BBBB', bytes) self.material['specular'] = { 'b' : c[0] / 255.0, 'g' : c[1] / 255.0, 'r' : c[2] / 255.0, 'coef' : c[3] / 255.0 } elif chunk_head >= 8 and chunk_head <= 9: print("Chunk: Tiny") bytes = self.file.read(2) chunk_body = struct.unpack('H', bytes)[0] mip_depth = chunk_flag & 0x07 clamp_u = chunk_flag & 0x08 clamp_v = chunk_flag & 0x10 flip_u = chunk_flag & 0x20 flip_v = chunk_flag & 0x40 self.material['tex_id'] = chunk_body & 0x1fff super_sample = chunk_body >> 13 & 0x01 filter_sample = chunk_body >> 14 & 0x03 elif chunk_head >= 64 and chunk_head <= 66: print("Chunk Strip!!!") bytes = self.file.read(4) h = struct.unpack('HH', bytes) chunk_len = h[0] * 2 chunk_body = h[1] ignore_light = chunk_flag & 0x01 ignore_specular = chunk_flag & 0x02 ignore_ambient = chunk_flag & 0x04 use_alpha = chunk_flag & 0x08 double_side = chunk_flag & 0x10 flat_shading = chunk_flag & 0x20 environment_mapping = chunk_flag & 0x20 strip_count = chunk_body & 0x3fff; user_offset = chunk_body >> 14 print("Strip count: %d" % strip_count) for i in range (strip_count): bytes = self.file.read(2) strip_len = struct.unpack('h', bytes)[0] counter_clockwise = strip_len < 0 strip_len = abs(strip_len) strip = [] for k in range (strip_len): bytes = self.file.read(2) index = struct.unpack('H', bytes)[0] index = self.index_lookup[index] if chunk_head == 64: strip.append({ 'index' : index }) continue bytes = self.file.read(4) uv = struct.unpack('hh', bytes) u = uv[0] v = uv[1] if chunk_head == 65: u = u / 255.0 v = v / 255.0 elif chunk_head == 66: u = u / 1023.0 v = v / 1023.0 strip.append({ 'index' : index, 'uv' : [ u, v ] }) for i in range(len(strip) - 2): #{ if i % 2 == 0 and counter_clockwise: a = strip[i + 0] b = strip[i + 1] c = strip[i + 2] else : a = strip[i + 1] b = strip[i + 0] c = strip[i + 2] va = self.vertex_list[a['index']] vb = self.vertex_list[a['index']] vc = self.vertex_list[a['index']] self.face_list.append({ 'index' : ( a['index'], b['index'], b['index'] ), 'norm' : ( va['norm'], vb['norm'], vc['norm'] ), uv : ( a['uv'], b['uv'], c['uv'] ) }) # } print("Strip End: 0x%08x" % self.file.tell()) else: print("Unknown Chunk Type %d" % chunk_head) print("Unknown Chunk Flag %d" % chunk_flag) print("Unknown File Position: 0x%08x" % self.file.tell()) break #end While return None