forked from Durik256/Noesis-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt_lma.py
147 lines (126 loc) · 4.95 KB
/
fmt_lma.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#by Durik256
#orig Blender script https://forum.xentax.com/viewtopic.php?f=16&t=4592#p48184
from inc_noesis import *
def registerNoesisTypes():
handle = noesis.register("Granado Espada", ".lma")
noesis.setHandlerTypeCheck(handle, noepyCheckType)
noesis.setHandlerLoadModel(handle, noepyLoadModel)
return 1
def noepyCheckType(data):
if data[:4] != b'LMA ':
return 0
return 1
def noepyLoadModel(bs, mdlList):
bs = NoeBitStream(bs)
ctx = rapi.rpgCreateContext()
bs.seek(127)
back = bs.getOffset()
offset = 0
groupList=[]
matList=[]
texList=[]
skinList=[]
skeleton=[]
while (offset!=bs.getSize()):
base = [bs.readInt() for x in range(3)]
offset = bs.getOffset()
offset+=base[1]
if base[0]==0:
name = noeAsciiFromBytes(bs.readBytes(40))
parentName = noeAsciiFromBytes(bs.readBytes(40))
if len(parentName)==0:
parentName=None
bs.seek(40,1)
matrix = NoeMat44.fromBytes(bs.readBytes(64)).toMat43().inverse()
skeleton.append(NoeBone(len(skeleton),name,matrix,parentName))
if base[0]==6:#material
matName = noeAsciiFromBytes(bs.readBytes(40))
matList.append(NoeMaterial(matName, ''))
if base[0]==7:#texture
imageName = noeAsciiFromBytes(bs.readBytes(40))
texList.append([imageName,[bs.readUShort(), bs.readUShort()]])
if base[0]==3:#mesh
w=[bs.readInt() for x in range(6)]
meshList=[]
for m in range(w[4]):
mesh=Mesh()
mesh.skinID=w[0]
matID,unk=bs.readUShort(), bs.readUShort()
mesh.matList = matList[matID-1].name
indiceCount=bs.readInt()
vertCount = bs.readInt()
mesh.skinItemIDList=[]
for m in range(vertCount):
mesh.vertPosList += bs.readBytes(12)
bs.seek(12,1)
mesh.vertUVList += bs.readBytes(8)
mesh.skinItemIDList.append(bs.readInt())
mesh.indiceList=bs.readBytes(indiceCount*4)
meshList.append(mesh)
groupList.append(meshList)
if base[0]==4:#weighting
skin=Skin()
skin.ID = bs.readInt()
for m in range(w[1]):
indiceList=[]
weightList=[]
count=bs.readByte()
for j in range(count):
A,B=bs.readUShort(), bs.readUShort()
indiceList.append(A)
weightList.append(bs.readFloat())
skin.indiceList.append(indiceList)
skin.weightList.append(weightList)
skinList.append(skin)
bs.seek(offset)
for group in groupList:
for mesh in group:
for skin in skinList:
if skin.ID==mesh.skinID:
for itemID in mesh.skinItemIDList:
mesh.skinIndiceList.append(skin.indiceList[itemID])
mesh.skinWeightList.append(skin.weightList[itemID])
mesh.draw()
mdl = rapi.rpgConstructModel()
mdl.setBones(skeleton)
mdl.setModelMaterials(NoeModelMaterials([], matList))
mdlList.append(mdl)
rapi.setPreviewOption("setAngOfs", "0 90 90")
return 1
def searchString(bs):
bytes = []
byte = None
while byte != 0:
byte = bs.readUByte()
bytes.append(byte)
return noeAsciiFromBytes(bytes)
class Mesh():
def __init__(self):
self.vertPosList = b''
self.vertUVList = b''
self.indiceList = b''
self.skinIndiceList = []
self.skinWeightList = []
def draw(self):
self.createWeight()
rapi.rpgSetMaterial(self.matList)
rapi.rpgBindPositionBuffer(self.vertPosList, noesis.RPGEODATA_FLOAT, 12)
rapi.rpgBindUV1Buffer(self.vertUVList, noesis.RPGEODATA_FLOAT, 8)
if self.skinIndiceList and self.skinWeightList:
rapi.rpgBindBoneIndexBuffer(self.skinIndiceList, noesis.RPGEODATA_USHORT, 8, 4)
rapi.rpgBindBoneWeightBuffer(self.skinWeightList, noesis.RPGEODATA_FLOAT, 16, 4)
rapi.rpgCommitTriangles(self.indiceList, noesis.RPGEODATA_UINT, len(self.indiceList)//4, noesis.RPGEO_TRIANGLE)
def createWeight(self):
IWBUF, WBUF = b'', b''
for x in self.skinIndiceList:
x += [0]*(4-len(x))
IWBUF += struct.pack('4H',*x)
for x in self.skinWeightList:
x += [0]*(4-len(x))
WBUF += struct.pack('4f',*x)
self.skinIndiceList = IWBUF
self.skinWeightList = WBUF
class Skin():
def __init__(self):
self.indiceList = []
self.weightList = []