Advertisement
Guest User

JX3 python

a guest
Feb 13th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 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.         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.      
  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 )
  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.         # read the number of vertices deformed by this bone
  107.         numVertices = bs.readUInt()
  108.         if numVertices > 0:
  109.             weightList = []
  110.             VertexList = []
  111.             weights = []
  112.             # read vertex IDs
  113.             for j in range(numVertices):
  114.                 vertexID = bs.readUInt()
  115.                 VertData[vertexID].BoneID.append(i)
  116.                 VertexList.append(vertexID)
  117.             # read weights correspondent to vertex IDs
  118.             for j in range(numVertices):
  119.                 weight = bs.readFloat()
  120.                 VertData[VertexList[j]].Weights.append(weight)
  121.                 weights.append(weight)
  122.  
  123.         # set bones for Noesis to display      
  124.         boneList.append(NoeBone(i, boneName, boneMat43, parentBoneName, parentIndex = -1))
  125.        
  126.      for k in range(numVerts):
  127.         weightList.append(NoeVertWeight(VertData[k].BoneID, VertData[k].Weights))
  128.        
  129.      # load animation -------------------------------------------------------------------
  130.      anims = rapi.loadPairedFile("JX2 ani", ".min")
  131.      aniF = NoeBitStream(anims)
  132.      signature = aniF.readBytes(8)
  133.      numAnims = aniF.readUInt()
  134.      print('numAnims: ', numAnims)
  135.      frameRate = aniF.readUInt()
  136.      print('frameRate: ', frameRate)
  137.      #animName may not be read correctly since it is Chinese string when using ascii or utf-8,
  138.      animName = aniF.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  139.      print('animName: ', animName)
  140.      numBones = aniF.readUInt()
  141.      print('numBones: ', numBones)
  142.      numFrames = aniF.readUInt()
  143.      print('numFrames: ', numFrames)
  144.      unk6 = aniF.readBytes(4)
  145.      animBones = []
  146.      for i in range(numBones):
  147.         animBoneName = aniF.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
  148.         for j in range(len(boneList)):
  149.             if animBoneName == boneList[j].name:
  150.                 animBones.append(boneList[j])
  151.      animFrameMats = []
  152.      numMats = numBones * numFrames
  153.      print('numMats: ', numMats)
  154.      for i in range(numMats):
  155.         frameMat44 = NoeMat44.fromBytes(aniF.readBytes(64))
  156.         frameMat43 = frameMat44.toMat43()
  157.         animFrameMats.append(frameMat43)
  158.      anim = NoeAnim(animName, animBones, numFrames, animFrameMats, frameRate)
  159.      #print(anim.__repr__())
  160.      animList = []
  161.      animList.append(anim)
  162.      # --------------- set elements of the model
  163.      mesh = NoeMesh(PolygonIndex, Positions, meshName)
  164.      mesh.setWeights(weightList)
  165.      mesh.setNormals(Normals)
  166.      mesh.setUVs(UV)
  167.      meshes.append(mesh)
  168.      mdl = NoeModel(meshes)  
  169.      mdl.setBones(boneList)
  170.      #animation testing
  171.      # panims = [NoeProceduralAnim("Bip01 L Clavicle", -30.0, 1, 0.1), NoeProceduralAnim("Bip01 R Clavicle", 30.0, 1, 0.1)]
  172.      # anims = rapi.createProceduralAnim(boneList, panims, 100)
  173.      mdl.setAnims(animList)
  174.      #end of animation testing
  175.      mdlList.append(mdl)
  176.      
  177.      return 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement