if (heapSize < 200000) then
        heapSize = 2000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
fname = getOpenFileName \
caption:"Open .mesh from Mesh folder" \
types:"LOTF Models (*.*)|*.*" \
historyCategory:"LOTFObjectPresets"
f = fopen fname "rb"

clearlistener()

fn readBElong fstream = (bit.swapBytes (bit.swapBytes (readlong fstream #unsigned) 1 4) 2 3)
	
fn readBEfloat fstream = (bit.intAsFloat (bit.swapBytes (bit.swapBytes (readlong fstream #unsigned) 1 4) 2 3))	
	
fn readBEword fstream = (
short = readshort fstream #unsigned
short = bit.swapBytes short 1 2
return short
)	

fn ReadBEHalfFloat fstream = 
	(
	hf=readBEword fstream
	sign = bit.get hf 16
	exponent = (bit.shift (bit.and hf (bit.hexasint "7C00")) -10) as integer - 16
	fraction = bit.and hf (bit.hexasint "03FF")
	if sign==true then sign = 1 else sign = 0
	exponentF = exponent + 127
	outputAsFloat = bit.or (bit.or (bit.shift fraction 13) \
	(bit.shift exponentF 23)) (bit.shift sign 31)
	return bit.intasfloat outputasfloat*2
)	

fn ReadHalfFloat fstream = 
	(
	hf=readshort fstream
	sign = bit.get hf 16
	exponent = (bit.shift (bit.and hf (bit.hexasint "7C00")) -10) as integer - 16
	fraction = bit.and hf (bit.hexasint "03FF")
	if sign==true then sign = 1 else sign = 0
	exponentF = exponent + 127
	outputAsFloat = bit.or (bit.or (bit.shift fraction 13) \
	(bit.shift exponentF 23)) (bit.shift sign 31)
	return bit.intasfloat outputasfloat*2
)

fn readFixedString bstream fixedLen = (
local str = ""
for i = 1 to fixedLen do (
str += bit.intAsChar (ReadByte bstream #unsigned))
str
)
struct weight_data
(
	boneids,weights
)
struct Bone_Info_Struct
(
	Bone1,Bone2,Bone3,Bone4
)
struct Weight_Info_Struct
(
	Weight1,Weight2,Weight3,Weight4
)
struct Mesh_Info_Struct
(
    DataOff,DataSize,DataCount
)	
struct Mesh_Info_Struct_2 
(
    DataOff2,DataSize2,DataCount2
)	
clearlistener()

BNArr=#()	
BoneNameArray=#()

fseek f (0x0) #seek_end
EndSize=ftell f
fseek f (0x0) #seek_set
Magic=readlong f
if Magic!=1566333786 then fclose f
if Magic==1566333786 then (
fseek f (0x8) #seek_cur
MeshCount=readBELong f
FileSize=readBELong f
DataStart=readBELong f
DataEnd=readBELong f
DataSize=readBELong f

SizeCount=(EndSize-FileSize)
print SizeCount

fseek f (DataEnd+SizeCount) #seek_set
Offset=readlong f

FaceOff=#()
if SizeCount==0 then (
	
fseek f (Offset) #seek_set	
Print ("Start 00@ 0x"+((bit.intAsHex(ftell f))as string))		
Count=readLong f	
fseek f (Count-12) #seek_cur	
BOffset=readlong f	
fseek f (0x78) #seek_cur		
BoneCount=readlong f	
fseek f (BOffset) #seek_set	
fseek f (0x10) #seek_cur	
DOffset=readlong f	
UnkSize=readlong f
	
Print ("Bone Start @ 0x"+((bit.intAsHex(ftell f))as string))	
		
for i=1 to BoneCount do (
getPos = ftell f + 76
BoneId=readlong f
BoneChild=readlong f
Unk01=readlong f
Unk02=readlong f	
BoneParent=readlong f+1	
c11=readfloat f;c12=readfloat f;c13=readfloat f;c14=readfloat f
c21=readfloat f;c22=readfloat f;c23=readfloat f;c24=readfloat f
c31=readfloat f;c32=readfloat f;c33=readfloat f;c34=readfloat f
BoneNameOff=readlong f;Null=readlong f		
fseek f getPos #seek_set	
Position=ftell f
fseek f (BoneNameOff) #seek_set	
BoneName=readstring f	
append BoneNameArray	BoneName
fseek f Position #seek_set	
		
tfm = (quat c24 c23 c22 c21) as matrix3
tfm.row4 = [c32,c33,c34]

newBone = bonesys.createbone    \
                                      tfm.row4      \
                                      (tfm.row4 + 0.01 * (normalize tfm.row1)) \
                                      (normalize tfm.row3)
                            newBone.width  = 0.01
                            newBone.height = 0.01
							newBone.name = BoneName								  
                            newBone.transform = tfm
                            newBone.setBoneEnable false 0
                            newBone.wirecolor = yellow
                            newbone.showlinks = true
                            newBone.pos.controller      = TCB_position ()
                            newBone.rotation.controller = TCB_rotation () 
if BoneParent!=0 then
newBone.parent=BNArr[(BoneParent)]											  
append BNArr newBone  
)	

Print ("Bone End @ 0x"+((bit.intAsHex(ftell f))as string))			
	
	
fseek f (DOffset) #seek_set
DCount=readLong f	
fseek f (DCount-12) #seek_cur
DataOffset=readlong f
fseek f (0x28) #seek_cur	
VertCount=readlong f
FaceCount=readlong f
Unk=readlong f

Mesh_Info=#()

Print ("Data Start @ 0x"+((bit.intAsHex(ftell f))as string))		
for i=1 to 7 do (
getPos = ftell f + 16
DataOff=readlong f
Null=readlong f	
DataSize=readlong f	
DataCount=readlong f	
fseek f getPos #seek_set		
append Mesh_Info (Mesh_Info_Struct DataOff:DataOff DataSize:DataSize DataCount:DataCount)	
)
fseek f (0X4) #seek_cur
append FaceOff (readlong f)
print Mesh_Info
)	


if SizeCount!=0 then (
	
if SizeCount==2 then fseek f (Offset-1) #seek_set
if SizeCount==3 then fseek f (Offset) #seek_set
if SizeCount==4 then fseek f (Offset+1) #seek_set
if SizeCount==5 then fseek f (Offset+2) #seek_set	
Count=readBELong f		

Print ("Start 01@ 0x"+((bit.intAsHex(ftell f))as string))			
fseek f (Count-9) #seek_cur	
BOffset=readlong f
fseek f (0xB4) #seek_cur		
Print ("Start 02@ 0x"+((bit.intAsHex(ftell f))as string))			
BoneCount=readlong f		
	
fseek f (BOffset) #seek_set

if SizeCount==2 then fseek f (0x12) #seek_cur
if SizeCount==3 then fseek f (0x13) #seek_cur	
if SizeCount==4 then fseek f (0x14) #seek_cur
if SizeCount==5 then fseek f (0x15) #seek_cur	
		
DOffset=readlong f
Print ("Start 03@ 0x"+((bit.intAsHex(ftell f))as string))	
	
fseek f (0xC) #seek_cur	

Print ("Bone Start @ 0x"+((bit.intAsHex(ftell f))as string))	
		
for i=1 to BoneCount do (
getPos = ftell f + 88
BoneId=readlong f
BoneChild=readlong f
Unk01=readlong f
Unk02=readlong f	
BoneParent=readlong f+1	
c11=readfloat f;c12=readfloat f;c13=readfloat f;c14=readfloat f
c21=readfloat f;c22=readfloat f;c23=readfloat f;c24=readfloat f
c31=readfloat f;c32=readfloat f;c33=readfloat f;c34=readfloat f
Null=readlong f;BoneNameOff=readlong f	
fseek f (0xC) #seek_cur		
fseek f getPos #seek_set	
Position=ftell f
if SizeCount==2 then fseek f (BoneNameOff+1) #seek_set	
if SizeCount==3 then fseek f (BoneNameOff+2) #seek_set
if SizeCount==4 then fseek f (BoneNameOff+3) #seek_set	
if SizeCount==5 then fseek f (BoneNameOff+4) #seek_set		
BoneName=readstring f	
append BoneNameArray	BoneName
fseek f Position #seek_set	
		
tfm = (quat c24 c23 c22 c21) as matrix3
tfm.row4 = [c32,c33,c34]

newBone = bonesys.createbone    \
                                      tfm.row4      \
                                      (tfm.row4 + 0.01 * (normalize tfm.row1)) \
                                      (normalize tfm.row3)
                            newBone.width  = 0.01
                            newBone.height = 0.01
							newBone.name = BoneName								  
                            newBone.transform = tfm
                            newBone.setBoneEnable false 0
                            newBone.wirecolor = yellow
                            newbone.showlinks = true
                            newBone.pos.controller      = TCB_position ()
                            newBone.rotation.controller = TCB_rotation () 
if BoneParent!=0 then
newBone.parent=BNArr[(BoneParent)]											  
append BNArr newBone  
)	

Print ("Bone End @ 0x"+((bit.intAsHex(ftell f))as string))		

if SizeCount==2 then fseek f (DOffset-1) #seek_set
if SizeCount==3 then fseek f (DOffset) #seek_set	
if SizeCount==4 then fseek f (DOffset+1) #seek_set
if SizeCount==5 then fseek f (DOffset+2) #seek_set	
	
DCount=readBELong f	
	
Print ("Start 04@ 0x"+((bit.intAsHex(ftell f))as string))			
fseek f (DCount-9) #seek_cur	
DataOffset=readlong f
Print ("Start 05@ 0x"+((bit.intAsHex(ftell f))as string))		
fseek f (0x4C) #seek_cur
VertCount=readlong f
FaceCount=readlong f
Unk=readlong f

Mesh_Info=#()

Print ("Data Start @ 0x"+((bit.intAsHex(ftell f))as string))		
for i=1 to 7 do (
getPos = ftell f + 24
Null01=readlong f
DataOff=readlong f
Null02=readlong f	
Null03=readlong f		
DataSize=readlong f	
DataCount=readlong f	
fseek f getPos #seek_set		
append Mesh_Info (Mesh_Info_Struct DataOff:DataOff DataSize:DataSize DataCount:DataCount)	
)

fseek f (0xC) #seek_cur
append FaceOff (readlong f)	
print Mesh_Info
)

if SizeCount==0 then fseek f (Mesh_Info[1].DataOff) #seek_set
if SizeCount==2 then fseek f (Mesh_Info[1].DataOff+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info[1].DataOff+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info[1].DataOff+4) #seek_set
if SizeCount==5 then fseek f (Mesh_Info[1].DataOff+5) #seek_set

Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))	

vertArray = #()
faceArray = #()
UV_array = #()
Weight_array=#()	
B1_array=#()
W1_array=#()
	
for x = 1 to VertCount do (
getPos = ftell f + 12
vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
append vertArray ([vx,vy,vz])	
fseek f getPos #seek_set		
)	

if SizeCount==0 then fseek f (Mesh_Info[2].DataOff) #seek_set
if SizeCount==2 then fseek f (Mesh_Info[2].DataOff+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info[2].DataOff+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info[2].DataOff+4) #seek_set
if SizeCount==5 then fseek f (Mesh_Info[2].DataOff+5) #seek_set

Print ("Normal Start @ 0x"+((bit.intAsHex(ftell f))as string))	

for x = 1 to VertCount do (
getPos = ftell f + 12
fseek f getPos #seek_set			
)	

if SizeCount==0 then fseek f (Mesh_Info[3].DataOff) #seek_set
if SizeCount==2 then fseek f (Mesh_Info[3].DataOff+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info[3].DataOff+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info[3].DataOff+4) #seek_set
if SizeCount==5 then fseek f (Mesh_Info[3].DataOff+5) #seek_set

Print ("Tangent Start @ 0x"+((bit.intAsHex(ftell f))as string))	

for x = 1 to VertCount do (
getPos = ftell f + 12	
fseek f getPos #seek_set		
)	

if SizeCount==0 then fseek f (Mesh_Info[4].DataOff) #seek_set
if SizeCount==2 then fseek f (Mesh_Info[4].DataOff+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info[4].DataOff+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info[4].DataOff+4) #seek_set
if SizeCount==5 then fseek f (Mesh_Info[4].DataOff+5) #seek_set

Print ("UV's Start @ 0x"+((bit.intAsHex(ftell f))as string))	

for x = 1 to VertCount do (
tu=readBEword f 
tv=readBEword f 
fseek f (0x8) #seek_set		
append UV_array ([tu,tv,0]/65535)		
)	

Print ("UV's End @ 0x"+((bit.intAsHex(ftell f))as string))	

if SizeCount==0 then fseek f (Mesh_Info[5].DataOff) #seek_set	
if SizeCount==2 then fseek f (Mesh_Info[5].DataOff+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info[5].DataOff+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info[5].DataOff+4) #seek_set
if SizeCount==5 then fseek f (Mesh_Info[5].DataOff+5) #seek_set	

Print ("BoneID's Start @ 0x"+((bit.intAsHex(ftell f))as string))		
	
for x = 1 to VertCount do (
bone1 = readshort f #unsigned	
bone2 = readshort f #unsigned		
bone3 = readshort f #unsigned	
bone4 = readshort f #unsigned		
append B1_array (Bone_Info_Struct Bone1:Bone1 Bone2:Bone2 Bone3:Bone3 Bone4:Bone4)		
)	

if SizeCount==0 then fseek f (Mesh_Info[6].DataOff) #seek_set
if SizeCount==2 then fseek f (Mesh_Info[6].DataOff+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info[6].DataOff+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info[6].DataOff+4) #seek_set
if SizeCount==5 then fseek f (Mesh_Info[6].DataOff+5) #seek_set

Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	

for x = 1 to VertCount do (
weight1 = ReadFloat f
weight2 = ReadFloat f
weight3 = ReadFloat f
weight4 = ReadFloat f		
append W1_array (Weight_Info_Struct Weight1:Weight1 Weight2:Weight2 Weight3:Weight3 Weight4:Weight4)		
)	

for b = 1 to W1_array.count Do (
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(W1_array[b].Weight1 != 0) then
   maxweight = maxweight + W1_array[b].Weight1
if(W1_array[b].Weight2 != 0) then
   maxweight = maxweight + W1_array[b].Weight2
if(W1_array[b].Weight3 != 0) then
   maxweight = maxweight + W1_array[b].Weight3
if(W1_array[b].Weight4 != 0) then
   maxweight = maxweight + W1_array[b].Weight4

if(maxweight != 0) then
   (
      if(W1_array[b].Weight1 != 0) then
      (
         w1 = W1_array[b].Weight1 as float
         append w.boneids (B1_array[b].Bone1 + 1)
         append w.weights (w1)
      )
      if(W1_array[b].Weight2 != 0) then
      (
         w2 = W1_array[b].Weight2 as float
         append w.boneids (B1_array[b].Bone2 + 1)
         append w.weights (w2)
      )
      if(W1_array[b].Weight3 != 0) then
      (
         w3 = W1_array[b].Weight3 as float
         append w.boneids (B1_array[b].Bone3 + 1)
         append w.weights (w3)
      )
      if(W1_array[b].Weight4 != 0) then
      (
         w4 = W1_array[b].Weight4 as float
         append w.boneids (B1_array[b].Bone4 + 1)
         append w.weights (w4)
      )      
   )
append Weight_array w
)

Print ("!!!!! Start @ 0x"+((bit.intAsHex(ftell f))as string))	

if SizeCount==0 then fseek f (Mesh_Info[7].DataOff) #seek_set	
if SizeCount==2 then fseek f (Mesh_Info[7].DataOff+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info[7].DataOff+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info[7].DataOff+4) #seek_set
if SizeCount==5 then fseek f (Mesh_Info[7].DataOff+5) #seek_set	
	
for x = 1 to Mesh_Info[7].DataCount do (
id=readbyte f
)	

Print ("!!!!! End @ 0x"+((bit.intAsHex(ftell f))as string))	

if SizeCount==0 then fseek f (FaceOff[1]) #seek_set	
if SizeCount==2 then fseek f (FaceOff[1]+2) #seek_set
if SizeCount==3 then fseek f (FaceOff[1]+3) #seek_set	
if SizeCount==4 then fseek f (FaceOff[1]+4) #seek_set	
if SizeCount==5 then fseek f (FaceOff[1]+5) #seek_set		

Print ("Face Start @ 0x"+((bit.intAsHex(ftell f))as string))	

for x=1 to (FaceCount/3) do (	
fa=readlong f +1
fb=readlong f +1
fc=readlong f +1
append faceArray [fa,fb,fc]	
)	

Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))	

msh = mesh vertices:vertArray faces:faceArray
msh.numTVerts = UV_array.count
buildTVFaces msh
for j = 1 to UV_array.count do setTVert msh j UV_array[j]
for j = 1 to faceArray.count do setTVFace msh j faceArray[j]
msh.displayByLayer = false
msh.backfacecull = false
msh.wirecolor = (color 230 200 210)

max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
for i = 1 to BNArr.count do
(
	maxbone = BNArr[i]
	if i != BNArr.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
modPanel.setCurrentObject skinMod
for i = 1 to vertArray.count do (
   w = Weight_array[i]
   bi = #() --bone index array
   wv = #() --weight value array
   
   for j = 1 to w.boneids.count do
   (
      boneid = w.boneids[j]
      weight = w.weights[j]
      append bi boneid
      append wv weight
   )   
   
   skinOps.ReplaceVertexWeights skinMod i bi wv
)	
)

Pos00=ftell f
Null00=readlong f
if Null00!=0 then fseek f Pos00 #seek_set 	
if Null00==0 then (
fseek f 0x0 #seek_cur	
Pos01=ftell f
Null01=readlong f
if Null01!=0 then fseek f Pos01 #seek_set 	
if Null01==0 then (
fseek f 0x0 #seek_cur	
Pos02=ftell f
Null02=readlong f
if Null02!=0 then fseek f Pos02 #seek_set 	
if Null02==0 then (
fseek f 0x0 #seek_cur	
Pos03=ftell f
Null03=readlong f
if Null03!=0 then fseek f Pos03 #seek_set 	
if Null03==0 then fseek f 0x0 #seek_cur			
)		
)	
)	

SimMeshEnd=ftell f
if Magic==1566333786 then (
Print ("SimMeshEnd @ 0x"+((bit.intAsHex(ftell f))as string))	
)

fseek f (0x0) #seek_end
EndSize=ftell f
fseek f (0x0) #seek_set
Magic=readlong f
if Magic!=1566333786 then fclose f
if Magic==1566333786 then (
for i=1 to 1 do (	
fseek f (0x8) #seek_cur
MeshCount=readBELong f
FileSize=readBELong f
DataStart=readBELong f
DataEnd=readBELong f
DataSize=readBELong f
SizeCount=(EndSize-FileSize)
print SizeCount
	
fseek f (SimMeshEnd) #seek_set
fseek f (0xE8) #seek_cur
MeshOffset=readlong f	
print MeshOffset
	
if SizeCount==0 then fseek f (MeshOffset) #seek_set
if SizeCount==2 then fseek f (MeshOffset+2) #seek_set
if SizeCount==3 then fseek f (MeshOffset+3) #seek_set	
if SizeCount==4 then fseek f (MeshOffset+4) #seek_set
if SizeCount==5 then fseek f (MeshOffset+5) #seek_set
	
fseek f (0x18) #seek_cur
	
Pos00=ftell f
Null00=readlong f
if Null00==48 then fseek f Pos00 #seek_set 	
if Null00!=48 then (
fseek f 0x0 #seek_cur	
Pos01=ftell f
Null01=readlong f
if Null01==48 then fseek f Pos01 #seek_set 	
if Null01!=48 then (
fseek f 0x0 #seek_cur	
Pos02=ftell f
Null02=readlong f
if Null02==48 then fseek f Pos02 #seek_set 	
if Null02!=48 then (
fseek f 0x0 #seek_cur	
Pos03=ftell f
Null03=readlong f
if Null03==48 then fseek f Pos03 #seek_set 	
if Null03!=48 then fseek f 0x0 #seek_cur			
)		
)	
)	
Buffer=ftell f
fseek f (0x90) #seek_cur
FaceCount=readlong f
fseek f (Buffer) #seek_set
fseek f (0xE0) #seek_cur
Long048=readlong f
if Long048==48 then (
fseek f (0xDC) #seek_cur
PosLong=ftell f	
Long048_01=readlong f	
if Long048_01!=48 then fseek f (0x94) #seek_cur
if Long048_01==48 then (
fseek f (0xDC) #seek_cur	
PosLong01=ftell f	
Long048_02=readlong f	
if Long048_02!=48 then fseek f (0x94) #seek_cur
if Long048_02==48 then (
fseek f (0xDC) #seek_cur		
PosLong02=ftell f	
Long048_03=readlong f	
if Long048_03!=48 then fseek f (0x94) #seek_cur
if Long048_03==48 then fseek f (0x0) #seek_cur	
)	
)
)	
if Long048!=48 then fseek f (0x94) #seek_cur
Position=ftell f
Print ("Position Start @ 0x"+((bit.intAsHex(ftell f))as string))	
fseek f (0x18) #seek_cur	
FaceStart=ftell f
fseek f (Position) #seek_set
OffsetMesh=readlong f

if SizeCount==0 then fseek f (OffsetMesh) #seek_set
if SizeCount==2 then fseek f (OffsetMesh+2) #seek_set
if SizeCount==3 then fseek f (OffsetMesh+3) #seek_set	
if SizeCount==4 then fseek f (OffsetMesh+4) #seek_set
if SizeCount==5 then fseek f (OffsetMesh+5) #seek_set
OffsetMeshInfo=readlong f
if SizeCount==0 then fseek f (OffsetMeshInfo+104) #seek_set
if SizeCount==2 then fseek f (OffsetMeshInfo+106) #seek_set
if SizeCount==3 then fseek f (OffsetMeshInfo+107) #seek_set	
if SizeCount==4 then fseek f (OffsetMeshInfo+108) #seek_set
if SizeCount==5 then fseek f (OffsetMeshInfo+109) #seek_set

Print ("Data Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
Mesh_Info2=#()

Print ("Data Start @ 0x"+((bit.intAsHex(ftell f))as string))		
for i=1 to 6 do (
getPos = ftell f + 144
BufferName=readlong f
fseek f 0xC #seek_cur
DataOff2=readlong f	
Null02=readlong f	
Null03=readlong f		
DataSize2=readlong f	
DataCount2=readlong f	
fseek f getPos #seek_set	
append Mesh_Info2 (Mesh_Info_Struct_2 DataOff2:DataOff2 DataSize2:DataSize2 DataCount2:DataCount2)		
)
WeightsInfo=ftell f

print Mesh_Info2

if SizeCount==0 then fseek f (Mesh_Info2[1].DataOff2) #seek_set
if SizeCount==2 then fseek f (Mesh_Info2[1].DataOff2+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info2[1].DataOff2+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info2[1].DataOff2+4) #seek_set
if SizeCount==5 then fseek f (Mesh_Info2[1].DataOff2+5) #seek_set

Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))	

vertArray2 = #()
faceArray2 = #()
UV_array2 = #()
Weight_array2=#()	
B1_array2=#()
W1_array2=#()
	
for x = 1 to Mesh_Info2[1].DataCount2 do (
getPos = ftell f + 12
vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f		
fseek f getPos #seek_set
append vertArray2 [vx,vy,vz]
)	

Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))	

fseek f (FaceStart) #seek_set	

Print ("Face Start @ 0x"+((bit.intAsHex(ftell f))as string))	

for x=1 to (FaceCount/3) do (	
fa=readlong f +1
fb=readlong f +1
fc=readlong f +1
append faceArray2 [fa,fb,fc]	
)	

Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))	

if SizeCount==0 then fseek f (Mesh_Info2[4].DataOff2) #seek_set
if SizeCount==2 then fseek f (Mesh_Info2[4].DataOff2+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info2[4].DataOff2+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info2[4].DataOff2+4) #seek_set
if SizeCount==5 then fseek f (Mesh_Info2[4].DataOff2+5) #seek_set
	
Print ("UV's Start @ 0x"+((bit.intAsHex(ftell f))as string))	

for x = 1 to Mesh_Info2[4].DataCount2 do (
tu=readfloat f 
tv=readfloat f*-1 
append UV_array2 [tu,tv,0]			
)	

Print ("UV's End @ 0x"+((bit.intAsHex(ftell f))as string))	

if (Mesh_Info2[6].DataSize2)==8 then (	
if SizeCount==0 then fseek f (Mesh_Info2[6].DataOff2) #seek_set
if SizeCount==2 then fseek f (Mesh_Info2[6].DataOff2+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info2[6].DataOff2+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info2[6].DataOff2+4) #seek_set	
if SizeCount==5 then fseek f (Mesh_Info2[6].DataOff2+5) #seek_set	
	
Print ("BoneID's Start @ 0x"+((bit.intAsHex(ftell f))as string))		
	
for x = 1 to Mesh_Info2[1].DataCount2 do (
bone1 = readshort f #unsigned	
bone2 = readshort f #unsigned		
bone3 = readshort f #unsigned	
bone4 = readshort f #unsigned		
append B1_array2 (Bone_Info_Struct Bone1:Bone1 Bone2:Bone2 Bone3:Bone3 Bone4:Bone4)		
)	
)
	
if (Mesh_Info2[6].DataSize2)==16 then (	
if SizeCount==0 then fseek f (Mesh_Info2[5].DataOff2) #seek_set
if SizeCount==2 then fseek f (Mesh_Info2[5].DataOff2+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info2[5].DataOff2+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info2[5].DataOff2+4) #seek_set	
if SizeCount==5 then fseek f (Mesh_Info2[5].DataOff2+5) #seek_set	
	
Print ("BoneID's Start @ 0x"+((bit.intAsHex(ftell f))as string))		
	
for x = 1 to Mesh_Info2[1].DataCount2 do (
bone1 = readshort f #unsigned	
bone2 = readshort f #unsigned		
bone3 = readshort f #unsigned	
bone4 = readshort f #unsigned		
append B1_array2 (Bone_Info_Struct Bone1:Bone1 Bone2:Bone2 Bone3:Bone3 Bone4:Bone4)		
)	
)

if (Mesh_Info2[6].DataSize2)==8 then (	
fseek f (WeightsInfo) #seek_set
Print ("Weights Info Start @ 0x"+((bit.intAsHex(ftell f))as string))		
WBufferName=readlong f
fseek f 0xC #seek_cur
WDataOff=readlong f	
Null02=readlong f	
Null03=readlong f		
WDataSize=readlong f	
WDataCount=readlong f	

if SizeCount==0 then fseek f (WDataOff) #seek_set
if SizeCount==2 then fseek f (WDataOff+2) #seek_set
if SizeCount==3 then fseek f (WDataOff+3) #seek_set	
if SizeCount==4 then fseek f (WDataOff+4) #seek_set	
if SizeCount==5 then fseek f (WDataOff+5) #seek_set	
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	

for x = 1 to Mesh_Info2[1].DataCount2 do (
weight1 = ReadFloat f
weight2 = ReadFloat f
weight3 = ReadFloat f
weight4 = ReadFloat f		
append W1_array2 (Weight_Info_Struct Weight1:Weight1 Weight2:Weight2 Weight3:Weight3 Weight4:Weight4)		
)	

for b = 1 to W1_array2.count Do (
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(W1_array2[b].Weight1 != 0) then
   maxweight = maxweight + W1_array2[b].Weight1
if(W1_array2[b].Weight2 != 0) then
   maxweight = maxweight + W1_array2[b].Weight2
if(W1_array2[b].Weight3 != 0) then
   maxweight = maxweight + W1_array2[b].Weight3
if(W1_array2[b].Weight4 != 0) then
   maxweight = maxweight + W1_array2[b].Weight4

if(maxweight != 0) then
   (
      if(W1_array2[b].Weight1 != 0) then
      (
         w1 = W1_array2[b].Weight1 as float
         append w.boneids (B1_array2[b].Bone1 + 1)
         append w.weights (w1)
      )
      if(W1_array2[b].Weight2 != 0) then
      (
         w2 = W1_array2[b].Weight2 as float
         append w.boneids (B1_array2[b].Bone2 + 1)
         append w.weights (w2)
      )
      if(W1_array2[b].Weight3 != 0) then
      (
         w3 = W1_array2[b].Weight3 as float
         append w.boneids (B1_array2[b].Bone3 + 1)
         append w.weights (w3)
      )
      if(W1_array2[b].Weight4 != 0) then
      (
         w4 = W1_array2[b].Weight4 as float
         append w.boneids (B1_array2[b].Bone4 + 1)
         append w.weights (w4)
      )      
   )
append Weight_array2 w
)	
)	

if (Mesh_Info2[6].DataSize2)==16 then (
if SizeCount==0 then fseek f (Mesh_Info2[6].DataOff2) #seek_set
if SizeCount==2 then fseek f (Mesh_Info2[6].DataOff2+2) #seek_set
if SizeCount==3 then fseek f (Mesh_Info2[6].DataOff2+3) #seek_set	
if SizeCount==4 then fseek f (Mesh_Info2[6].DataOff2+4) #seek_set	
if SizeCount==5 then fseek f (Mesh_Info2[6].DataOff2+5) #seek_set	
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	

for x = 1 to Mesh_Info2[4].DataCount2 do (
weight1 = ReadFloat f
weight2 = ReadFloat f
weight3 = ReadFloat f
weight4 = ReadFloat f		
append W1_array2 (Weight_Info_Struct Weight1:Weight1 Weight2:Weight2 Weight3:Weight3 Weight4:Weight4)		
)	

for b = 1 to W1_array2.count Do (
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(W1_array2[b].Weight1 != 0) then
   maxweight = maxweight + W1_array2[b].Weight1
if(W1_array2[b].Weight2 != 0) then
   maxweight = maxweight + W1_array2[b].Weight2
if(W1_array2[b].Weight3 != 0) then
   maxweight = maxweight + W1_array2[b].Weight3
if(W1_array2[b].Weight4 != 0) then
   maxweight = maxweight + W1_array2[b].Weight4

if(maxweight != 0) then
   (
      if(W1_array2[b].Weight1 != 0) then
      (
         w1 = W1_array2[b].Weight1 as float
         append w.boneids (B1_array2[b].Bone1 + 1)
         append w.weights (w1)
      )
      if(W1_array2[b].Weight2 != 0) then
      (
         w2 = W1_array2[b].Weight2 as float
         append w.boneids (B1_array2[b].Bone2 + 1)
         append w.weights (w2)
      )
      if(W1_array2[b].Weight3 != 0) then
      (
         w3 = W1_array2[b].Weight3 as float
         append w.boneids (B1_array2[b].Bone3 + 1)
         append w.weights (w3)
      )
      if(W1_array2[b].Weight4 != 0) then
      (
         w4 = W1_array2[b].Weight4 as float
         append w.boneids (B1_array2[b].Bone4 + 1)
         append w.weights (w4)
      )      
   )
append Weight_array2 w
)
)

msh = mesh vertices:vertArray2 faces:faceArray2
msh.name=("Model_Cloth"+i as string)
msh.numTVerts = UV_array2.count
buildTVFaces msh
for j = 1 to UV_array2.count do setTVert msh j UV_array2[j]
for j = 1 to faceArray2.count do setTVFace msh j faceArray2[j]
msh.displayByLayer = false
msh.backfacecull = false
msh.wirecolor = (color 230 200 210)

max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
for i = 1 to BNArr.count do
(
	maxbone = BNArr[i]
	if i != BNArr.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
modPanel.setCurrentObject skinMod
for i = 1 to vertArray2.count do (
   w = Weight_array2[i]
   bi = #() --bone index array
   wv = #() --weight value array
   
   for j = 1 to w.boneids.count do
   (
      boneid = w.boneids[j]
      weight = w.weights[j]
      append bi boneid
      append wv weight
   )   
   
   skinOps.ReplaceVertexWeights skinMod i bi wv
)
)
)

fclose f 