home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 6433 / Bakemonogatari.7z / fmt_Bakemonogatari_amo.py next >
Encoding:
Python Source  |  2013-05-28  |  3.6 KB  |  111 lines

  1. from inc_noesis import *
  2. import noesis
  3. import rapi
  4. import tex_Bakemonogatari_amt
  5.  
  6. def registerNoesisTypes():
  7.     handle = noesis.register("Bakemonogatari Portable", ".AMO")
  8.     noesis.setHandlerTypeCheck(handle, amoCheckType)
  9.     noesis.setHandlerLoadModel(handle, amoLoadModel)
  10.     #noesis.logPopup()
  11.     return 1
  12.  
  13.  
  14.  
  15. def amoCheckType(data):
  16.     bs = NoeBitStream(data)
  17.     magic = bs.readBytes(4).decode("ASCII")
  18.     if magic != '#AMO':
  19.         return 0
  20.     return 1     
  21.  
  22. def amoLoadModel(data, mdlList):
  23.     ctx = rapi.rpgCreateContext()
  24.  
  25.     basePath = rapi.getDirForFilePath(rapi.getInputName())
  26.     baseName = rapi.getExtensionlessName(rapi.getLocalFileName(rapi.getInputName()))
  27.     amtFile = basePath + (("%0.8X" % ((int(baseName, 16)) + 1)) + ".amt")
  28.     matList = []
  29.     texList = []
  30.     if (rapi.checkFileExists(amtFile)):
  31.         texData = rapi.loadIntoByteArray(amtFile)
  32.         if tex_Bakemonogatari_amt.AMTLoadRGBA(texData, texList) == 1:
  33.             for a in range(0, len(texList)):
  34.                 material = NoeMaterial("mat" + str(a), "")
  35.                 material.setTexture(texList[a].name)
  36.                 material.setFlags(0, 1)
  37.                 matList.append(material)
  38.     bs = NoeBitStream(data)
  39.     bs.seek(16, NOESEEK_ABS)
  40.     c1, o1, c2, o2, c3, o3 = bs.read("6I")
  41.  
  42.     meshName = []
  43.     bs.seek(o3, NOESEEK_ABS)
  44.     for a in range(0, c1):
  45.         meshName.append(bs.readBytes(0x20).decode("ASCII").rstrip("\0"))
  46.     #print(meshName)
  47.  
  48.     bs.seek(o1, NOESEEK_ABS)
  49.     for a in range(0, c1):
  50.         Info = bs.read("8I")
  51.  
  52.     bs.seek(o2, NOESEEK_ABS)
  53.     amgOff = bs.read(str(c2) + "I")
  54.     for a in range(0, c2):
  55.         bs.seek(amgOff[a], NOESEEK_ABS)
  56.         amgInfo = bs.read("8I")
  57.         bs.seek(amgOff[a] + amgInfo[5], NOESEEK_ABS)
  58.         chnkInfo = []
  59.         for b in range(0, amgInfo[4]):
  60.             chnkInfo.append(bs.read("12f4B7I"))
  61.         amgMesh = []
  62.         amfBone = []
  63.         for b in range(0, amgInfo[4]):
  64.             bs.seek(amgOff[a] + chnkInfo[b][16], NOESEEK_ABS)
  65.             amgInfo2 = bs.read("8I")
  66.             if amgInfo2[1] != 0:
  67.                 amgMesh.append(amgInfo2)
  68.             else:
  69.                 amfBone.append(amgInfo2)
  70.         for b in range(0, len(amgMesh)):
  71.             bs.seek(amgOff[a] + amgMesh[b][5], NOESEEK_ABS)
  72.             bs.seek(amgOff[a] + amgMesh[b][1], NOESEEK_ABS);  amgBase = bs.tell()
  73.             meshCount, meshTbl = bs.read("2I")
  74.             bs.seek(amgBase + meshTbl, NOESEEK_ABS)
  75.             meshOff = bs.read(str(meshCount) + "I")
  76.             for c in range(0, meshCount):
  77.                 rapi.rpgSetName(str(amgMesh[b][0]) + " - " + str(c))
  78.                 bs.seek(amgBase + meshOff[c], NOESEEK_ABS)
  79.                 tmp = bs.read("6f")
  80.                 bs.seek(0x14, NOESEEK_REL)
  81.                 texID = bs.readInt()
  82.                 if texID != -1:
  83.                     rapi.rpgSetMaterial("mat" + str(texID))
  84.                 cUnk0, cUnk1, cUnk2, chnkCount, cUnk3, cUnk4 = bs.read("2I4H")
  85.                 for d in range(0, chnkCount):
  86.                     meshInfo = bs.read("3IH2B")
  87.                     bs.seek(amgBase + meshInfo[1], NOESEEK_ABS)
  88.                     pspVert = rapi.decodePSPVert(meshInfo[2])
  89.                     if pspVert.uvType == 2:
  90.                         rapi.rpgSetUVScaleBias(NoeVec3 ((2.0, 2.0, 1.0)), NoeVec3 ((0.0, 0.0, 0.0)))
  91.                     elif pspVert.uvType == 3:
  92.                         rapi.rpgSetUVScaleBias(NoeVec3 ((1.0, 1.0, 1.0)), NoeVec3 ((0.0, 0.0, 0.0)))
  93.                     vertData = bs.readBytes(pspVert.vertexSize * meshInfo[3])
  94.                     if vertData is not None and len(vertData) > 0:
  95.                         pspVert.bindBuffers(vertData)
  96.                         if meshInfo[4] == 3:
  97.                             rapi.rpgCommitTriangles(None, noesis.RPGEODATA_SHORT, meshInfo[3], noesis.RPGEO_TRIANGLE, 1)
  98.                         else:
  99.                             rapi.rpgCommitTriangles(None, noesis.RPGEODATA_SHORT, meshInfo[3], noesis.RPGEO_TRIANGLE_STRIP, 1)
  100.                     bs.seek(amgBase + meshInfo[0], NOESEEK_ABS)
  101.                     rapi.rpgClearBufferBinds()
  102.  
  103.  
  104.     try:
  105.         mdl = rapi.rpgConstructModel()
  106.     except:
  107.         mdl = NoeModel()
  108.     mdl.setModelMaterials(NoeModelMaterials(texList, matList))
  109.     mdlList.append(mdl)    
  110.     rapi.rpgClearBufferBinds()    
  111.     return 1