home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 6389 / data_fate_extra_v1.7z / data_fate_extra.py
Encoding:
Python Source  |  2013-05-07  |  13.2 KB  |  297 lines

  1. from inc_noesis import *
  2. import noesis
  3. import rapi
  4.  
  5. def registerNoesisTypes():
  6.     handle = noesis.register("Fate Extra", ".Im")
  7.     noesis.setHandlerTypeCheck(handle, noepyCheckType)
  8.     noesis.setHandlerLoadModel(handle, noepyLoadModel) #see also noepyLoadModelRPG
  9.  
  10.     noesis.logPopup()
  11.     return 1
  12.  
  13.  
  14. #check if it's this type based on the data
  15. def noepyCheckType(data):
  16.     bs = NoeBitStream(data)
  17.     return 1       
  18.  
  19. #load the model
  20. def noepyLoadModel(data, mdlList):
  21.     ctx = rapi.rpgCreateContext()
  22.     bs = NoeBitStream(data)
  23.     #rapi.rpgSetPosScaleBias((-1,-1,1),(0,0,0))
  24.     boneList = []
  25.  
  26.     boneCount = bs.readInt()
  27.     boneOffset = bs.tell() + bs.readInt()
  28.     texCount = bs.readInt()
  29.     texOffset = bs.tell() + bs.readInt()
  30.     matCount = bs.readInt()
  31.     matOffset = bs.tell() + bs.readInt()
  32.     meshCount = bs.readInt()
  33.     meshOffset = bs.tell() + bs.readInt()
  34.  
  35.     bs.seek(boneOffset, NOESEEK_ABS)
  36.     for i in range(0, boneCount):
  37.         boneName = bs.readBytes(0x20).decode("ASCII").rstrip("\0")
  38.         boneMtx = NoeMat44.fromBytes(bs.readBytes(0x40)).toMat43()
  39.         bs.readBytes(0x40)
  40.         boneParent = bs.readInt()
  41.         bs.readBytes(0x8)
  42.         pos = NoeVec3.fromBytes(bs.readBytes(12))
  43.         bs.readBytes(0x8)
  44.         quat = NoeQuat.fromBytes(bs.readBytes(16))
  45.         NoeVec3.fromBytes(bs.readBytes(12))
  46.         bs.readBytes(0x14)
  47.         boneMtx = quat.toMat43()
  48.         boneMtx[3] = pos
  49.         newBone = NoeBone(i, boneName, boneMtx, None, boneParent)
  50.         boneList.append(newBone)
  51.     boneList = rapi.multiplyBones(boneList)
  52.  
  53.     texInfo = []
  54.     texList = []
  55.     bs.seek(texOffset, NOESEEK_ABS)
  56.     for i in range(0, texCount):
  57.         texName = bs.readBytes(0x20).decode("ASCII").rstrip("\0")
  58.         texStart = bs.tell() + bs.readInt()
  59.         bs.readBytes(0xC)
  60.         texInfo.append([texName,texStart])
  61.     #print(texInfo)
  62.     for i in range(0, texCount):
  63.         bs.seek(texInfo[i][1], NOESEEK_ABS)
  64.         bs.readBytes(0xC)
  65.         palOff = bs.tell() + bs.readInt()
  66.         texType, unk02, pixWidth, pixHeight, unk03, unk04 = bs.read("6H")
  67.         texSize = bs.readInt()
  68.         texOff = bs.tell() + bs.readInt()
  69.         bs.seek(texOff, NOESEEK_ABS)
  70.         texData = bs.readBytes(texSize)
  71.         bs.seek(palOff, NOESEEK_ABS)
  72.         unk11, unk22 = bs.read("2H")
  73.         palSize, Null = bs.read("2I")
  74.         palStart = bs.tell() + bs.readInt()
  75.         bs.seek(palStart, NOESEEK_ABS)
  76.         palData = []
  77.         for a in range(0, palSize // 4):
  78.             pR, pG, pB, pA = bs.read("4B")
  79.             if pR == 0 and pG == 255 and pB == 0 and pA == 255:
  80.                 pG = 0
  81.                 pA = 0
  82.             palData.append(pR)
  83.             palData.append(pG)
  84.             palData.append(pB)
  85.             palData.append(pA)
  86.         palData = struct.pack("<" + 'B'*len(palData), *palData)
  87.         if texType == 5:
  88.             pix = rapi.imageUntwiddlePSP(texData, pixWidth, pixHeight, 8)
  89.             pix = rapi.imageDecodeRawPal(pix, palData, pixWidth, pixHeight, 8, "r8g8b8a8")
  90.         elif texType == 4:
  91.             pix = rapi.imageUntwiddlePSP(texData, pixWidth, pixHeight, 4)
  92.             pix = rapi.imageDecodeRawPal(pix, palData, pixWidth, pixHeight, 4, "r8g8b8a8")
  93.         texList.append(NoeTexture(texInfo[i][0], pixWidth, pixHeight, pix, noesis.NOESISTEX_RGBA32))
  94.  
  95.     matList = []
  96.     bs.seek(matOffset, NOESEEK_ABS)
  97.     for i in range(0, matCount):
  98.         matName = bs.readBytes(0x20).decode("ASCII").rstrip("\0")
  99.         bs.readBytes(0xC)
  100.         texID = bs.readInt()
  101.         unk01, unk02, unk03, unk04 = bs.read("4B")
  102.         bs.readBytes(0x8)
  103.         material = NoeMaterial(matName, "")
  104.         if texID >= 0:
  105.             material.setTexture(texInfo[texID][0])
  106.             #material.setOpacityTexture(texInfo[texID][0])
  107.         matList.append(material)
  108.  
  109.     meshList = []
  110.     bs.seek(meshOffset, NOESEEK_ABS)
  111.     for i in range(0, meshCount):
  112.         meshName = bs.readBytes(0x20).decode("ASCII").rstrip("\0")
  113.         mshCount = bs.readInt()
  114.         unk11 = bs.readInt()
  115.         meshStart = bs.tell() + bs.readInt()
  116.         bs.readBytes(0xC)
  117.         meshList.append([meshName, meshStart, mshCount, unk11])
  118.     #print(meshList)
  119.  
  120.     for a in range(0, len(meshList)):
  121.         mshInfo = []
  122.         #print(meshList[a][0])
  123.         bs.seek(meshList[a][1], NOESEEK_ABS)
  124.         for b in range(0, meshList[a][2]):
  125.             unk20 = bs.readInt()
  126.             matID = bs.readInt()
  127.             mshC = bs.readInt()
  128.             MshOff = bs.tell() + bs.readInt()
  129.             mshInfo.append([matID, mshC, MshOff])
  130.         #print(mshInfo)
  131.         mshInfo2 = []
  132.         for b in range(0, len(mshInfo)):
  133.             bs.seek(mshInfo[b][2], NOESEEK_ABS)
  134.             for c in range(0, mshInfo[b][1]):
  135.                 meshPc = bs.readInt()
  136.                 vType  = bs.readInt()
  137.                 meshPc2  = bs.readInt()
  138.                 mshPo  = bs.tell() + bs.readInt()
  139.                 mshPo2 = bs.tell() + bs.readInt()
  140.                 mshInfo2.append([meshPc, vType, meshPc2, mshPo, mshPo2, matList[(mshInfo[b][0])].name])
  141.         #print(mshInfo2)
  142.         for b in range(0, len(mshInfo2)):
  143.             mshInfo3 = []
  144.             rapi.rpgSetMaterial(mshInfo2[b][5])
  145.             bs.seek(mshInfo2[b][3], NOESEEK_ABS)
  146.             idxList = [[0, 0, 0, 0, 0, 0, 0, 0]]
  147.             for c in range(0, mshInfo2[b][2]):
  148.                 idxList[0][c] = bs.readInt()
  149.             bs.seek(mshInfo2[b][4], NOESEEK_ABS)
  150.             #print(idxList)
  151.             for c in range(0, mshInfo2[b][0]):
  152.                 mshId2     = bs.readInt()
  153.                 vertCount  = bs.readInt()
  154.                 vertStart  = bs.tell() + bs.readInt()
  155.                 mshInfo3.append([mshId2, vertCount, vertStart])
  156.             #print(mshInfo3)
  157.             for c in range(0, len(mshInfo3)):
  158.                 #print(mshInfo3[c])
  159.                 bs.seek(mshInfo3[c][2], NOESEEK_ABS)
  160.                 rapi.rpgSetName(meshList[a][0])
  161.                 #rapi.rpgSetName(meshList[a][0] + "_" + str(b) + "_" + str(c) + "_" + str(mshInfo2[b][1]))
  162.  
  163.                 vertStride = 0
  164.                 if mshInfo2[b][1] == 7:
  165.                     vertStride = 0x18
  166.                     vertBuff = bs.readBytes(mshInfo3[c][1] * vertStride)
  167.                     rapi.rpgBindPositionBufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0xC)
  168.                     rapi.rpgBindColorBufferOfs(vertBuff, noesis.RPGEODATA_UBYTE, vertStride, 0x8, 4)
  169.                     rapi.rpgBindUV1BufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0)
  170.                 elif mshInfo2[b][1] == 24:
  171.                     vertStride = 0x1C
  172.                     vertBuff = bs.readBytes(mshInfo3[c][1] * vertStride)
  173.                     rapi.rpgBindPositionBufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x10)
  174.                     rapi.rpgBindColorBufferOfs(vertBuff, noesis.RPGEODATA_UBYTE, vertStride, 0xC, 4)
  175.                     rapi.rpgBindUV1BufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x4)
  176.                 elif mshInfo2[b][1] == 25:
  177.                     vertStride = 0x1C
  178.                     vertBuff = bs.readBytes(mshInfo3[c][1] * vertStride)
  179.                     rapi.rpgBindPositionBufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x10)
  180.                     rapi.rpgBindColorBufferOfs(vertBuff, noesis.RPGEODATA_UBYTE, vertStride, 0xC, 4)
  181.                     rapi.rpgBindUV1BufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x4)
  182.                 elif mshInfo2[b][1] == 26:
  183.                     vertStride = 0x20
  184.                     vertBuff = bs.readBytes(mshInfo3[c][1] * vertStride)
  185.                     rapi.rpgBindPositionBufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x14)
  186.                     rapi.rpgBindColorBufferOfs(vertBuff, noesis.RPGEODATA_UBYTE, vertStride, 0x10, 4)
  187.                     rapi.rpgBindUV1BufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x8)
  188.                 elif mshInfo2[b][1] == 27:
  189.                     vertStride = 0x20
  190.                     vertBuff = bs.readBytes(mshInfo3[c][1] * vertStride)
  191.                     rapi.rpgBindPositionBufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x14)
  192.                     rapi.rpgBindColorBufferOfs(vertBuff, noesis.RPGEODATA_UBYTE, vertStride, 0x10, 4)
  193.                     rapi.rpgBindUV1BufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x8)
  194.                 elif mshInfo2[b][1] == 28:
  195.                     vertStride = 0x24
  196.                     vertBuff = bs.readBytes(mshInfo3[c][1] * vertStride)
  197.                     rapi.rpgBindPositionBufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x18)
  198.                     rapi.rpgBindColorBufferOfs(vertBuff, noesis.RPGEODATA_UBYTE, vertStride, 0x14, 4)
  199.                     rapi.rpgBindUV1BufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0xC)
  200.                 elif mshInfo2[b][1] == 29:
  201.                     vertStride = 0x24
  202.                     vertBuff = bs.readBytes(mshInfo3[c][1] * vertStride)
  203.                     rapi.rpgBindPositionBufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x18)
  204.                     rapi.rpgBindColorBufferOfs(vertBuff, noesis.RPGEODATA_UBYTE, vertStride, 0x14, 4)
  205.                     rapi.rpgBindUV1BufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0xC)
  206.                 elif mshInfo2[b][1] == 30:
  207.                     vertStride = 0x28
  208.                     vertBuff = bs.readBytes(mshInfo3[c][1] * vertStride)
  209.                     rapi.rpgBindPositionBufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x1C)
  210.                     rapi.rpgBindColorBufferOfs(vertBuff, noesis.RPGEODATA_UBYTE, vertStride, 0x18, 4)
  211.                     rapi.rpgBindUV1BufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x10)
  212.                 elif mshInfo2[b][1] == 23:
  213.                     vertStride = 0x28
  214.                     vertBuff = bs.readBytes(mshInfo3[c][1] * vertStride)
  215.                     rapi.rpgBindPositionBufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x1C)
  216.                     rapi.rpgBindColorBufferOfs(vertBuff, noesis.RPGEODATA_UBYTE, vertStride, 0x18, 4)
  217.                     rapi.rpgBindUV1BufferOfs(vertBuff, noesis.RPGEODATA_FLOAT, vertStride, 0x10)
  218.  
  219.                 else:
  220.                     print(bs.tell())
  221.                     print("unknown found " + str(mshInfo2[b][1]))
  222.                 
  223.                 vs = NoeBitStream(vertBuff)
  224.                 tmp = []
  225.                 tmp2 = []
  226.                 weight = []
  227.                 index = []
  228.                 if vertStride == 0x18:
  229.                     for d in range(0, mshInfo3[c][1]):
  230.                         w1, w2, w3, w4, w5, w6, w7, w8 = [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  231.                         index.append(idxList[0][0]); index.append(idxList[0][1]); index.append(idxList[0][2]); index.append(idxList[0][3])
  232.                         index.append(idxList[0][4]); index.append(idxList[0][5]); index.append(idxList[0][6]); index.append(idxList[0][7])
  233.                         weight.append(w1); weight.append(w2); weight.append(w3); weight.append(w4); weight.append(w5); weight.append(w6); weight.append(w7); weight.append(w8)
  234.                         vs.readBytes(0xC)
  235.                         tmp.append([vs.readUInt(), vs.readUInt(), vs.readUInt()])
  236.                 elif vertStride == 0x1C:
  237.                     for d in range(0, mshInfo3[c][1]):
  238.                         w1, w2, w3, w4, w5, w6, w7, w8 = [vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  239.                         index.append(idxList[0][0]); index.append(idxList[0][1]); index.append(idxList[0][2]); index.append(idxList[0][3])
  240.                         index.append(idxList[0][4]); index.append(idxList[0][5]); index.append(idxList[0][6]); index.append(idxList[0][7])
  241.                         weight.append(w1); weight.append(w2); weight.append(w3); weight.append(w4); weight.append(w5); weight.append(w6); weight.append(w7); weight.append(w8)
  242.                         vs.readBytes(0xC)
  243.                         tmp.append([vs.readUInt(), vs.readUInt(), vs.readUInt()])
  244.                 elif vertStride == 0x20:
  245.                     for d in range(0, mshInfo3[c][1]):
  246.                         w1, w2, w3, w4, w5, w6, w7, w8 = [vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, 0.0, 0.0, 0.0, 0.0]
  247.                         index.append(idxList[0][0]); index.append(idxList[0][1]); index.append(idxList[0][2]); index.append(idxList[0][3])
  248.                         index.append(idxList[0][4]); index.append(idxList[0][5]); index.append(idxList[0][6]); index.append(idxList[0][7])
  249.                         weight.append(w1); weight.append(w2); weight.append(w3); weight.append(w4); weight.append(w5); weight.append(w6); weight.append(w7); weight.append(w8)
  250.                         vs.readBytes(0xC)
  251.                         tmp.append([vs.readUInt(), vs.readUInt(), vs.readUInt()])
  252.                 elif vertStride == 0x24:
  253.                     for d in range(0, mshInfo3[c][1]):
  254.                         w1, w2, w3, w4, w5, w6, w7, w8 = [vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, 0.0, 0.0]
  255.                         index.append(idxList[0][0]); index.append(idxList[0][1]); index.append(idxList[0][2]); index.append(idxList[0][3])
  256.                         index.append(idxList[0][4]); index.append(idxList[0][5]); index.append(idxList[0][6]); index.append(idxList[0][7])
  257.                         weight.append(w1); weight.append(w2); weight.append(w3); weight.append(w4); weight.append(w5); weight.append(w6); weight.append(w7); weight.append(w8)
  258.                         vs.readBytes(0xC)
  259.                         tmp.append([vs.readUInt(), vs.readUInt(), vs.readUInt()])
  260.                 elif vertStride == 0x28:
  261.                     for d in range(0, mshInfo3[c][1]):
  262.                         w1, w2, w3, w4, w5, w6, w7, w8 = [vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF, vs.readUShort() / 0x7FFF]
  263.                         index.append(idxList[0][0]); index.append(idxList[0][1]); index.append(idxList[0][2]); index.append(idxList[0][3])
  264.                         index.append(idxList[0][4]); index.append(idxList[0][5]); index.append(idxList[0][6]); index.append(idxList[0][7])
  265.                         weight.append(w1); weight.append(w2); weight.append(w3); weight.append(w4); weight.append(w5); weight.append(w6); weight.append(w7); weight.append(w8)
  266.                         vs.readBytes(0xC)
  267.                         tmp.append([vs.readUInt(), vs.readUInt(), vs.readUInt()])
  268.                 indexBuff  = struct.pack('B'*len(index), *index)
  269.                 weightBuff = struct.pack('f'*len(weight), *weight)
  270.                 rapi.rpgBindBoneIndexBuffer(indexBuff, noesis.RPGEODATA_BYTE, 8, 8)
  271.                 rapi.rpgBindBoneWeightBuffer(weightBuff, noesis.RPGEODATA_FLOAT, 0x20, 8)
  272.                 flip = 0
  273.                 step = 1
  274.                 if mshInfo3[c][0] == 0:
  275.                     step = 3
  276.                 for d in range(0, mshInfo3[c][1] - 2, step):
  277.                     if (tmp[d] != tmp[(d + 1)]) and (tmp[d] != tmp[(d + 2)]) and (tmp[d + 1] != tmp[(d + 2)]):
  278.                         tmp2.append(d)
  279.                         tmp2.append(d + 1 + flip)
  280.                         tmp2.append(d + 2 - flip)
  281.                         if mshInfo3[c][0] != 0:
  282.                             flip = 1 - flip
  283.                     else:
  284.                         flip = 0
  285.                         #print(d)
  286.                 faceBuff = struct.pack('H'*len(tmp2), *tmp2)
  287.                 if len(faceBuff) > 3:
  288.                     rapi.rpgCommitTriangles(faceBuff, noesis.RPGEODATA_USHORT, len(tmp2), noesis.RPGEO_TRIANGLE, 1)
  289.                 rapi.rpgClearBufferBinds() 
  290.  
  291.     mdl = rapi.rpgConstructModel()
  292.     mdl.setModelMaterials(NoeModelMaterials(texList, matList))
  293.     mdlList.append(mdl)
  294.     mdl.setBones(boneList)
  295.  
  296.   
  297.     return 1