Advertisement
Guest User

animation-jx2

a guest
Feb 22nd, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.07 KB | None | 0 0
  1. from os.path import *
  2. from math import *
  3. from inc_noesis import *
  4. # created by jayn23 - Xentax forum/2D-3D Model
  5. class FileData:
  6.  
  7.     def __init__(self):
  8.         self.BoneID = []
  9.         self.Weights = []
  10.        
  11. def registerNoesisTypes():
  12.     handle = noesis.register("unk game", ".mesh")    
  13.     noesis.setHandlerTypeCheck(handle, noepyCheckType)
  14.     noesis.setHandlerLoadModel(handle, noepyLoadModel)
  15.     #opens debug console
  16.     noesis.logPopup()
  17.     return 1
  18.  
  19.  
  20. def noepyCheckType(data):
  21.     '''Verify that the format is supported by this plugin. Default yes'''
  22.     #I preform my check while reading mesh data
  23.     return 1
  24.    
  25. def noepyLoadModel(data, mdlList):
  26.      meshName = "test"
  27.      Positions = []
  28.      Normals = []
  29.      UV = []
  30.      PolygonIndex = []
  31.      meshes = []
  32.      VertData = []
  33.      
  34.      bs = NoeBitStream(data, NOE_LITTLEENDIAN)
  35.  
  36.      bs.seek(140,0)
  37.      numMesh = bs.readInt()
  38.      # print("numMesh ",numMesh)
  39.      numVerts = bs.readInt()
  40.      # print("numVerts ",numVerts)
  41.      numFace = bs.readInt()
  42.      # print("numFace ",numFace)
  43.      unk1 = bs.readInt()
  44.      vertOff = bs.readInt()
  45.      normOff = bs.readInt()
  46.      unk2 = bs.readInt()
  47.      UVOff = bs.readInt()
  48.      unk3 = bs.readInt()
  49.      unk4 = bs.readInt()
  50.      faceoff = bs.readInt()
  51.      unk5 = bs.readInt()
  52.      boneOff = bs.readInt()
  53.      # print("boneOff ",boneOff)
  54.      
  55.      bs.seek(vertOff,0)
  56.      for i in range(numVerts):
  57.         vx = bs.readFloat()
  58.         vy = bs.readFloat()
  59.         vz = bs.readFloat()
  60.         VertData.append(FileData())
  61.         # we need to invert the x coordinator to avoid facecull (inverted normal)
  62.         Positions.append(NoeVec3([-vx,vy,vz]))
  63.        
  64.      bs.seek(normOff,0)
  65.      for i in range(numVerts):
  66.         nx = bs.readFloat()
  67.         ny = bs.readFloat()
  68.         nz = bs.readFloat()
  69.         #print(str(nx ) + str(ny ) + str(nz))
  70.         Normals.append(NoeVec3([nx, ny, nz]))
  71.  
  72.      bs.seek(UVOff,0)
  73.      for i in range(numVerts):    
  74.         tu = bs.readFloat()
  75.         tv = bs.readFloat()
  76.         UV.append(NoeVec3([tu,tv,0.0]))
  77.            
  78.      bs.seek(faceoff,0)
  79.      for j in range(numFace*3):
  80.         PolygonIndex.append(bs.readInt())
  81.      # list of NoeBone objects      
  82.      boneList = []
  83.      parBoneMats = []
  84.      bs.seek(boneOff,0)
  85.      boneCount = bs.readUInt()
  86.      
  87.     # read bones loop
  88.      for i in range(boneCount):
  89.         # read bone name
  90.         boneName = bs.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  91.         # read parent name of the current bone
  92.         parentBoneName = bs.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  93.         # read the number of child bone
  94.         numChildren = bs.readUInt()
  95.         # read child bone name
  96.         for j in range(numChildren):
  97.             childBoneName = bs.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  98.         # read 4x4 bone matrix
  99.         boneMat = NoeMat44.fromBytes( bs.readBytes(0x40), NOE_LITTLEENDIAN ).swapHandedness()
  100.         # convert and inverse to 4x3 matrix
  101.         boneMat43 = boneMat.toMat43().inverse()
  102.         # read 4x4 parent bone matrix
  103.         parentMat = NoeMat44.fromBytes( bs.readBytes(0x40), NOE_LITTLEENDIAN )
  104.         # convert and inverse to 4x3 matrix
  105.         parentMat43 = parentMat.toMat43().inverse()
  106.         parBoneMats.append(parentMat43)
  107.  
  108.         # read the number of vertices deformed by this bone
  109.         numVertices = bs.readUInt()
  110.         if numVertices > 0:
  111.             weightList = []
  112.             VertexList = []
  113.             weights = []
  114.             # read vertex IDs
  115.             for j in range(numVertices):
  116.                 vertexID = bs.readUInt()
  117.                 VertData[vertexID].BoneID.append(i)
  118.                 VertexList.append(vertexID)
  119.             # read weights correspondent to vertex IDs
  120.             for j in range(numVertices):
  121.                 weight = bs.readFloat()
  122.                 VertData[VertexList[j]].Weights.append(weight)
  123.                 weights.append(weight)
  124.  
  125.         # set bones for Noesis to display      
  126.         boneList.append(NoeBone(i, boneName, boneMat43, parentBoneName, parentIndex = -1))
  127.      # correct parentIndex
  128.      for bi in range(boneCount):
  129.         if boneList[bi].parentName != 'Scene Root':
  130.             boneList[bi].parentIndex = [boneList[x].name for x in range(boneCount)].index(boneList[bi].parentName)
  131.      # print(boneList[:].__repr__())    
  132.      for k in range(numVerts):
  133.         weightList.append(NoeVertWeight(VertData[k].BoneID, VertData[k].Weights))
  134.  
  135.                
  136. # load animation -------------------------------------------------------------------
  137.      anims = rapi.loadPairedFile("JX2 ani", ".ani")
  138.      aniF = NoeBitStream(anims)
  139.      signature = aniF.readBytes(8)
  140.      numAnims = aniF.readUInt()
  141.      # print('numAnims: ', numAnims)
  142.      frameRate = aniF.readUInt()
  143.      # print('frameRate: ', frameRate)
  144.      #animName may not be read correctly since it is Chinese string when using ascii or utf-8,
  145.      animName = aniF.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  146.      # print('animName: ', animName)
  147.      numBones = aniF.readUInt()
  148.      # print('numBones: ', numBones)
  149.      numFrames = aniF.readUInt()
  150.      # print('numFrames: ', numFrames)
  151.      unk6 = aniF.readBytes(4)
  152.      
  153.      animBones = []
  154.      animFrameMats = []
  155.      
  156.      MatIndex = []  
  157.      MatIndex2 = []
  158.      
  159.      for i in range(numBones):    
  160.         animBoneName = aniF.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  161.         #print(animBoneName)
  162.         for j in range(len(boneList)):
  163.             if animBoneName == boneList[j].name:
  164.                 MatIndex.append(j)
  165.                 MatIndex2.append(i)
  166.                 #print(boneList[j].name)
  167.                 #print("boneList[j].index",boneList[j].index)
  168.                 break
  169.  
  170.      for i in range(numFrames):
  171.      
  172.         TempMat = []
  173.        
  174.         for j in range(len(boneList)):
  175.             # if boneList[j].parentIndex != -1:
  176.                 # parentMatrix = boneList[boneList[j].parentIndex].getMatrix()
  177.                 # TempMat.append(boneList[j].getMatrix().__mul__(parentMatrix.inverse()))
  178.             # else:
  179.             TempMat.append(boneList[j].getMatrix())
  180.         for k in range(len(MatIndex)):
  181.             frameMat44 = NoeMat44.fromBytes(aniF.readBytes(64))
  182.             frameMat43 = frameMat44.toMat43()
  183.             if k in MatIndex2:
  184.                 # print(k)
  185.                 TempMat[MatIndex[k]] =  TempMat[MatIndex[k]].__mul__(frameMat43.inverse())
  186.         # try to create right hierachy bones with parent above children        
  187.         # for k in range(0, len(MatIndex) - 1):
  188.             # for l in range(k + 1, len(MatIndex)):
  189.                 # if ((k in MatIndex2) and (l in MatIndex2)):
  190.                     # if boneList[MatIndex[k]].parentIndex == boneList[MatIndex[l]].index:
  191.                         # SwapMat = TempMat[MatIndex[k]]
  192.                         # TempMat[MatIndex[k]] = TempMat[MatIndex[l]]
  193.                         # TempMat[MatIndex[l]] =  SwapMat
  194.        
  195.         # for k in range(len(MatIndex)):
  196.             # if k in MatIndex2:
  197.                 # TempMat[MatIndex[k]] = TempMat[boneList[MatIndex[k]].parentIndex].inverse() * TempMat[MatIndex[k]]
  198.         # print(TempMat)
  199.         animFrameMats += TempMat
  200.  
  201.      anim = NoeAnim(animName, boneList, numFrames, animFrameMats, frameRate)
  202.      animList = []
  203.      animList.append(anim)
  204.      # --------------- set elements of the model
  205.      mesh = NoeMesh(PolygonIndex, Positions, meshName)
  206.      mesh.setWeights(weightList)
  207.      mesh.setNormals(Normals)
  208.      mesh.setUVs(UV)
  209.      meshes.append(mesh)
  210.      mdl = NoeModel(meshes)  
  211.      mdl.setBones(boneList)
  212.      #animation testing
  213.      # panims = [NoeProceduralAnim("Bip01 L Thigh", -30.0, 1, 0.1), NoeProceduralAnim("Bip01 R Thigh", 30.0, 1, 0.1)]
  214.      # animList = rapi.createProceduralAnim(boneList, panims, 100)
  215.      mdl.setAnims(animList)
  216.      #end of animation testing
  217.      mdlList.append(mdl)
  218.      
  219.      return 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement