home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 4983 / fmt_virtualCoptest_bin.7z / fmt_virtualCoptest_bin.py
Encoding:
Python Source  |  2012-01-13  |  3.0 KB  |  97 lines

  1. '''rapi model template'''
  2.  
  3. from inc_noesis import *
  4. import noesis
  5. import rapi
  6. import struct
  7.  
  8. def registerNoesisTypes():
  9.     '''Register the plugin. Just change the Game name and extension.'''
  10.     
  11.     handle = noesis.register("Virtual Cop Elite", ".bin")
  12.     noesis.setHandlerTypeCheck(handle, noepyCheckType)
  13.     noesis.setHandlerLoadModel(handle, noepyLoadModel)
  14.     return 1
  15.  
  16. def noepyCheckType(data):
  17.     '''Verify that the format is supported by this plugin. Default yes'''
  18.     
  19.     return 1
  20.  
  21. def noepyLoadModel(data, mdlList):
  22.     '''Build the model, set materials, bones, and animations. You do not
  23.     need all of them as long as they are empty lists (they are by default)'''
  24.     
  25.     ctx = rapi.rpgCreateContext()
  26.     parser = ModelParser(data)
  27.     parser.parse_file()
  28.     mdl = rapi.rpgConstructModel()
  29.     mdl.setModelMaterials(NoeModelMaterials(parser.texList, parser.matList))
  30.     mdl.setBones(parser.boneList)
  31.     mdl.setAnims(parser.animList)
  32.     mdlList.append(mdl)
  33.     return 1
  34.  
  35. class ModelParser(object):
  36.     
  37.     def __init__(self, data):    
  38.         '''Initialize some data'''
  39.         
  40.         self.inFile = NoeBitStream(data)
  41.         self.meshList = []
  42.         self.uvList = []
  43.         self.normList = []
  44.         self.vertList = []
  45.         self.idxList = []
  46.         self.animList = []
  47.         self.texList = []
  48.         self.matList = []
  49.         self.boneList = []
  50.         
  51.     def parse_file(self):
  52.         '''Main parser method'''
  53.         
  54.     def read_name(self):
  55.             
  56.             return noeStrFromBytes(self.inFile.readBytes(12))
  57.         
  58.     def parse_vertices(self, numVerts):
  59.         
  60.         positions = self.inFile.readBytes(numVerts*16)
  61.         normals = self.inFile.readBytes(numVerts*16)
  62.         uvs = self.inFile.readBytes(numVerts*16)
  63.         unk = self.inFile.readBytes(numVerts*16)
  64.         
  65.         idxBuff = []
  66.         for i in range(numVerts - 2):
  67.             idxBuff.append(i)
  68.             idxBuff.append(i+1)
  69.             idxBuff.append(i+2)
  70.             
  71.         idxStrip = rapi.createTriStrip(idxBuff)
  72.         buff = bytes()
  73.         for idx in idxStrip:
  74.             buff += struct.pack("L", idx)
  75.         
  76.         rapi.rpgBindPositionBufferOfs(positions, noesis.RPGEODATA_FLOAT, 16, 0)
  77.         rapi.rpgCommitTriangles(buff, noesis.RPGEODATA_UINT, numVerts, noesis.RPGEO_TRIANGLE_STRIP, 1)
  78.         
  79.         rapi.rpgClearBufferBinds()
  80.         
  81.     def parse_file(self):
  82.         '''Main parser method'''
  83.         
  84.         self.inFile.read('4L')
  85.         for i in range(3):
  86.             self.read_name()
  87.             self.inFile.readUInt()
  88.         self.read_name()
  89.         self.inFile.readBytes(404)
  90.         
  91.         for i in range(180): #3000 is near the upper limit
  92.             unk1, unk2, unk3, numVerts = self.inFile.read('4L')
  93.             #print(unk1, unk2, unk3, numVerts, self.inFile.tell())
  94.             if numVerts == 0 or unk2 // numVerts != 16:
  95.                 self.inFile.seek(32, 1)
  96.             else:
  97.                 self.parse_vertices(numVerts)