Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from os.path import *
- from math import *
- from inc_noesis import *
- # created by jayn23 - Xentax forum/2D-3D Model
- class FileData:
- def __init__(self):
- self.BoneID = []
- self.Weights = []
- def registerNoesisTypes():
- handle = noesis.register("unk game", ".ksw")
- noesis.setHandlerTypeCheck(handle, noepyCheckType)
- noesis.setHandlerLoadModel(handle, noepyLoadModel)
- #opens debug console
- noesis.logPopup()
- return 1
- def noepyCheckType(data):
- '''Verify that the format is supported by this plugin. Default yes'''
- #I preform my check while reading mesh data
- return 1
- def noepyLoadModel(data, mdlList):
- meshName = "test"
- Positions = []
- Normals = []
- UV = []
- PolygonIndex = []
- meshes = []
- VertData = []
- bs = NoeBitStream(data, NOE_LITTLEENDIAN)
- bs.seek(140,0)
- numMesh = bs.readInt()
- # print("numMesh ",numMesh)
- numVerts = bs.readInt()
- # print("numVerts ",numVerts)
- numFace = bs.readInt()
- # print("numFace ",numFace)
- unk1 = bs.readInt()
- vertOff = bs.readInt()
- normOff = bs.readInt()
- unk2 = bs.readInt()
- UVOff = bs.readInt()
- unk3 = bs.readInt()
- unk4 = bs.readInt()
- faceoff = bs.readInt()
- unk5 = bs.readInt()
- boneOff = bs.readInt()
- # print("boneOff ",boneOff)
- bs.seek(vertOff,0)
- for i in range(numVerts):
- vx = bs.readFloat()
- vy = bs.readFloat()
- vz = bs.readFloat()
- VertData.append(FileData())
- # we need to invert the x coordinator to avoid facecull (inverted normal)
- Positions.append(NoeVec3([-vx,vy,vz]))
- bs.seek(normOff,0)
- for i in range(numVerts):
- nx = bs.readFloat()
- ny = bs.readFloat()
- nz = bs.readFloat()
- #print(str(nx ) + str(ny ) + str(nz))
- Normals.append(NoeVec3([nx, ny, nz]))
- bs.seek(UVOff,0)
- for i in range(numVerts):
- tu = bs.readFloat()
- tv = bs.readFloat()
- UV.append(NoeVec3([tu,tv,0.0]))
- bs.seek(faceoff,0)
- for j in range(numFace*3):
- PolygonIndex.append(bs.readInt())
- # list of NoeBone objects
- boneList = []
- parBoneMat = []
- bs.seek(boneOff,0)
- boneCount = bs.readUInt()
- # read bones loop
- for i in range(boneCount):
- # read bone name
- boneName = bs.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
- # read parent name of the current bone
- parentBoneName = bs.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
- # read the number of child bone
- numChildren = bs.readUInt()
- # read child bone name
- for j in range(numChildren):
- childBoneName = bs.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
- # read 4x4 bone matrix
- boneMat = NoeMat44.fromBytes( bs.readBytes(0x40), NOE_LITTLEENDIAN )
- # convert and inverse to 4x3 matrix
- boneMat43 = boneMat.toMat43().inverse()
- # read 4x4 parent bone matrix
- parentMat = NoeMat44.fromBytes( bs.readBytes(0x40), NOE_LITTLEENDIAN )
- # convert and inverse to 4x3 matrix
- parentMat43 = parentMat.toMat43().inverse()
- parBoneMat.append(parentMat43)
- # read the number of vertices deformed by this bone
- numVertices = bs.readUInt()
- if numVertices > 0:
- weightList = []
- VertexList = []
- weights = []
- # read vertex IDs
- for j in range(numVertices):
- vertexID = bs.readUInt()
- VertData[vertexID].BoneID.append(i)
- VertexList.append(vertexID)
- # read weights correspondent to vertex IDs
- for j in range(numVertices):
- weight = bs.readFloat()
- VertData[VertexList[j]].Weights.append(weight)
- weights.append(weight)
- # set bones for Noesis to display
- boneList.append(NoeBone(i, boneName, boneMat43, parentBoneName, parentIndex = -1))
- for k in range(numVerts):
- weightList.append(NoeVertWeight(VertData[k].BoneID, VertData[k].Weights))
- # load animation -------------------------------------------------------------------
- anims = rapi.loadPairedFile("JX2 ani", ".min")
- aniF = NoeBitStream(anims)
- signature = aniF.readBytes(8)
- numAnims = aniF.readUInt()
- # print('numAnims: ', numAnims)
- frameRate = aniF.readUInt()
- # print('frameRate: ', frameRate)
- #animName may not be read correctly since it is Chinese string when using ascii or utf-8,
- animName = aniF.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
- # print('animName: ', animName)
- numBones = aniF.readUInt()
- # print('numBones: ', numBones)
- numFrames = aniF.readUInt()
- # print('numFrames: ', numFrames)
- unk6 = aniF.readBytes(4)
- animBones = []
- animFrameMats = []
- MatIndex = []
- MatIndex2 = []
- for i in range(numBones):
- animBoneName = aniF.readBytes(30).decode('ascii', 'ignore').replace('\x00','').replace('\xCD','')
- #print(animBoneName)
- for j in range(len(boneList)):
- if animBoneName == boneList[j].name:
- MatIndex.append(j)
- MatIndex2.append(i)
- #print(boneList[j].name)
- #print("boneList[j].index",boneList[j].index)
- break
- for i in range(numFrames):
- TempMat = []
- for j in range(len(boneList)):
- TempMat.append(boneList[j].getMatrix())
- for k in range(len(MatIndex)):
- frameMat44 = NoeMat44.fromBytes(aniF.readBytes(64))
- frameMat43 = frameMat44.toMat43()
- frameMat43 = frameMat43.inverse()
- if k in MatIndex2:
- # print(k)
- TempMat[MatIndex[k]] = TempMat[MatIndex[k]] * frameMat43
- if boneList[MatIndex[k]].parentName != 'Scene Root':
- x = [boneList[y].name for y in range(boneCount)].index(boneList[MatIndex[k]].parentName)
- TempMat[MatIndex[k]] = TempMat[MatIndex[k]] * TempMat[x].inverse()
- animFrameMats += TempMat
- anim = NoeAnim(animName, boneList, numFrames, animFrameMats, frameRate)
- animList = []
- animList.append(anim)
- # --------------- set elements of the model
- mesh = NoeMesh(PolygonIndex, Positions, meshName)
- mesh.setWeights(weightList)
- mesh.setNormals(Normals)
- mesh.setUVs(UV)
- meshes.append(mesh)
- mdl = NoeModel(meshes)
- mdl.setBones(boneList)
- #animation testing
- # panims = [NoeProceduralAnim("Bip01 L Clavicle", -30.0, 1, 0.1), NoeProceduralAnim("Bip01 R Clavicle", 30.0, 1, 0.1)]
- # anims = rapi.createProceduralAnim(boneList, panims, 100)
- mdl.setAnims(animList)
- #end of animation testing
- mdlList.append(mdl)
- return 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement