Advertisement
Guest User

jx3-anim-4

a guest
Feb 16th, 2020
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.12 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", ".ksw")    
  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.  
  26. def noepyLoadModel(data, mdlList):
  27.      meshName = "test"
  28.      Positions = []
  29.      Normals = []
  30.      UV = []
  31.      PolygonIndex = []
  32.      meshes = []
  33.      VertData = []
  34.      
  35.      bs = NoeBitStream(data, NOE_LITTLEENDIAN)
  36.  
  37.      bs.seek(140,0)
  38.      numMesh = bs.readInt()
  39.      # print("numMesh ",numMesh)
  40.      numVerts = bs.readInt()
  41.      # print("numVerts ",numVerts)
  42.      numFace = bs.readInt()
  43.      # print("numFace ",numFace)
  44.      unk1 = bs.readInt()
  45.      vertOff = bs.readInt()
  46.      normOff = bs.readInt()
  47.      unk2 = bs.readInt()
  48.      UVOff = bs.readInt()
  49.      unk3 = bs.readInt()
  50.      unk4 = bs.readInt()
  51.      faceoff = bs.readInt()
  52.      unk5 = bs.readInt()
  53.      boneOff = bs.readInt()
  54.      # print("boneOff ",boneOff)
  55.      
  56.      bs.seek(vertOff,0)
  57.      for i in range(numVerts):
  58.         vx = bs.readFloat()
  59.         vy = bs.readFloat()
  60.         vz = bs.readFloat()
  61.         VertData.append(FileData())
  62.         # we need to invert the x coordinator to avoid facecull (inverted normal)
  63.         Positions.append(NoeVec3([-vx,vy,vz]))
  64.        
  65.      bs.seek(normOff,0)
  66.      for i in range(numVerts):
  67.         nx = bs.readFloat()
  68.         ny = bs.readFloat()
  69.         nz = bs.readFloat()
  70.         #print(str(nx ) + str(ny ) + str(nz))
  71.         Normals.append(NoeVec3([nx, ny, nz]))
  72.  
  73.      bs.seek(UVOff,0)
  74.      for i in range(numVerts):    
  75.         tu = bs.readFloat()
  76.         tv = bs.readFloat()
  77.         UV.append(NoeVec3([tu,tv,0.0]))
  78.            
  79.      bs.seek(faceoff,0)
  80.      for j in range(numFace*3):
  81.         PolygonIndex.append(bs.readInt())
  82.      # list of NoeBone objects      
  83.      boneList = []
  84.      parBoneMat = []
  85.      bs.seek(boneOff,0)
  86.      boneCount = bs.readUInt()
  87.      
  88.     # read bones loop
  89.      for i in range(boneCount):
  90.         # read bone name
  91.         boneName = bs.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  92.         # read parent name of the current bone
  93.         parentBoneName = bs.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  94.         # read the number of child bone
  95.         numChildren = bs.readUInt()
  96.         # read child bone name
  97.         for j in range(numChildren):
  98.             childBoneName = bs.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  99.         # read 4x4 bone matrix
  100.         boneMat = NoeMat44.fromBytes( bs.readBytes(0x40), NOE_LITTLEENDIAN )
  101.         # convert and inverse to 4x3 matrix
  102.         boneMat43 = boneMat.toMat43().inverse()
  103.         # read 4x4 parent bone matrix
  104.         parentMat = NoeMat44.fromBytes( bs.readBytes(0x40), NOE_LITTLEENDIAN )
  105.         # convert and inverse to 4x3 matrix
  106.         parentMat43 = parentMat.toMat43().inverse()
  107.         parBoneMat.append(parentMat43)
  108.  
  109.         # read the number of vertices deformed by this bone
  110.         numVertices = bs.readUInt()
  111.         if numVertices > 0:
  112.             weightList = []
  113.             VertexList = []
  114.             weights = []
  115.             # read vertex IDs
  116.             for j in range(numVertices):
  117.                 vertexID = bs.readUInt()
  118.                 VertData[vertexID].BoneID.append(i)
  119.                 VertexList.append(vertexID)
  120.             # read weights correspondent to vertex IDs
  121.             for j in range(numVertices):
  122.                 weight = bs.readFloat()
  123.                 VertData[VertexList[j]].Weights.append(weight)
  124.                 weights.append(weight)
  125.  
  126.         # set bones for Noesis to display      
  127.         boneList.append(NoeBone(i, boneName, boneMat43, parentBoneName, parentIndex = -1))
  128.      for k in range(numVerts):
  129.         weightList.append(NoeVertWeight(VertData[k].BoneID, VertData[k].Weights))
  130.  
  131.  
  132. # load animation -------------------------------------------------------------------
  133.      anims = rapi.loadPairedFile("JX2 ani", ".min")
  134.      aniF = NoeBitStream(anims)
  135.      signature = aniF.readBytes(8)
  136.      numAnims = aniF.readUInt()
  137.      # print('numAnims: ', numAnims)
  138.      frameRate = aniF.readUInt()
  139.      # print('frameRate: ', frameRate)
  140.      #animName may not be read correctly since it is Chinese string when using ascii or utf-8,
  141.      animName = aniF.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  142.      # print('animName: ', animName)
  143.      numBones = aniF.readUInt()
  144.      # print('numBones: ', numBones)
  145.      numFrames = aniF.readUInt()
  146.      # print('numFrames: ', numFrames)
  147.      unk6 = aniF.readBytes(4)
  148.      
  149.      animBones = []
  150.      animFrameMats = []
  151.      
  152.      MatIndex = []  
  153.      MatIndex2 = []
  154.      
  155.      for i in range(numBones):    
  156.         animBoneName = aniF.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  157.         #print(animBoneName)
  158.         for j in range(len(boneList)):
  159.             if animBoneName == boneList[j].name:
  160.                 MatIndex.append(j)
  161.                 MatIndex2.append(i)
  162.                 #print(boneList[j].name)
  163.                 #print("boneList[j].index",boneList[j].index)
  164.                 break
  165.  
  166.      for i in range(numFrames):
  167.      
  168.         TempMat = []
  169.        
  170.         for j in range(len(boneList)):
  171.             TempMat.append(boneList[j].getMatrix())
  172.            
  173.         for k in range(len(MatIndex)):
  174.             frameMat44 = NoeMat44.fromBytes(aniF.readBytes(64))
  175.             frameMat43 = frameMat44.toMat43()
  176.             frameMat43 = frameMat43.inverse()
  177.             if k in MatIndex2:
  178.                 # print(k)
  179.                 TempMat[MatIndex[k]] =  TempMat[MatIndex[k]] * frameMat43
  180.                 if boneList[MatIndex[k]].parentName != 'Scene Root':
  181.                     x = [boneList[y].name for y in range(boneCount)].index(boneList[MatIndex[k]].parentName)
  182.                     TempMat[MatIndex[k]] = TempMat[MatIndex[k]] * TempMat[x].inverse()
  183.            
  184.         animFrameMats += TempMat
  185.  
  186.      anim = NoeAnim(animName, boneList, numFrames, animFrameMats, frameRate)
  187.      animList = []
  188.      animList.append(anim)
  189.      # --------------- set elements of the model
  190.      mesh = NoeMesh(PolygonIndex, Positions, meshName)
  191.      mesh.setWeights(weightList)
  192.      mesh.setNormals(Normals)
  193.      mesh.setUVs(UV)
  194.      meshes.append(mesh)
  195.      mdl = NoeModel(meshes)  
  196.      mdl.setBones(boneList)
  197.      #animation testing
  198.      # panims = [NoeProceduralAnim("Bip01 L Clavicle", -30.0, 1, 0.1), NoeProceduralAnim("Bip01 R Clavicle", 30.0, 1, 0.1)]
  199.      # anims = rapi.createProceduralAnim(boneList, panims, 100)
  200.      mdl.setAnims(animList)
  201.      #end of animation testing
  202.      mdlList.append(mdl)
  203.      
  204.      return 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement