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 .mod from Mesh folder" \
types:"Monster Hunter 3 WII U  (*.58A15856)|*.58A15856" \
historyCategory:"MH3ObjectPresets"
f = fopen fname "rb"

clearlistener()

fn readBElong fstream = (bit.swapBytes (bit.swapBytes (readlong fstream #unsigned) 1 4) 2 3)


fn ReadBEshort fstream = (
return (bit.swapBytes (readshort fstream #unsigned) 1 2)
)

fn readBEword fstream = (
short = readshort fstream #unsigned
short = bit.swapBytes short 1 2
return short
)

fn readBEfloat fstream = (bit.intAsFloat (bit.swapBytes (bit.swapBytes (readlong fstream #unsigned) 1 4) 2 3))	

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 readFixedString bstream fixedLen = (
local str = ""
for i = 1 to fixedLen do (
str += bit.intAsChar (ReadByte bstream #unsigned))
str
)
struct weight_data
(
	boneids,weights
)
struct Mesh_Info_Struct
(
VertCount, VSize,VId,VId2,VType,VertStart,FacePos, FaceCount,VStart,VEnd,MeshId
)

clearlistener()

fseek f 0x6 #seek_set
BoneCount = ReadBEword f
meshcount = ReadBEword f
fseek f 0xc #seek_set
VertexCount = ReadBElong f
fseek f 0x18 #seek_set
VertSize = ReadBElong f 
Long05 = ReadBElong f 
Long06 = ReadBElong f  
UsedBoneCount = ReadBELong f  
BoneOff = ReadBElong f 
print (meshcount as string)
matcount2 = ReadBEword f
fseek f 0x34 #seek_set
itemoff = readBElong f
print (itemoff as string)
vertOffset = readBElong f
print (vertOffset as string)
faceOffset = readBElong f
fileEnd = readBElong f
print (faceOffset as string)

fseek f BoneOff  #seek_set	
BNArr = #()
BoneParent = #()
for a = 1 to BoneCount Do (
boneid = readbyte f#unsigned
append BoneParent (readbyte f#unsigned)
childid = readbyte f#unsigned
unkid = readbyte f#unsigned
floata = ReadBEfloat f
floatb = ReadBEfloat f
bonex  = ReadBEfloat f
boney  = ReadBEfloat f
bonez  = ReadBEfloat f				  
)

for a = 1 to BoneCount do (
getPos = ftell f + 64
fseek f getPos #seek_set	
)	

for a = 1 to BoneCount do (

c11 = ReadBEfloat f; c12 = ReadBEfloat f; c13 = ReadBEfloat f; c14 = ReadBEfloat f	
c21 = ReadBEfloat f; c22 = ReadBEfloat f; c23 = ReadBEfloat f; c24 = ReadBEfloat f	
c31 = ReadBEfloat f; c32 = ReadBEfloat f; c33 = ReadBEfloat f; c34 = ReadBEfloat f
c41 = ReadBEfloat f; c42 = ReadBEfloat f; c43 = ReadBEfloat f; c44 = ReadBEfloat f
	
tfm = matrix3 [c11,c12,c13] [c21,c22,c23] [c31,c32,c33] ([c41,c42,c43]*100)	
	
newBone = bonesys.createbone	\
				  tfm.row4	\
				  (tfm.row4 + 0.01 * (normalize tfm.row1)) \
				  (normalize tfm.row3)
			newBone.name = (a as string)
			newBone.width  = 0.01
			newBone.height = 0.01
			newBone.transform = inverse tfm
			newBone.setBoneEnable false 0
			newBone.wirecolor = white
			newbone.showlinks = true
			newBone.pos.controller      = TCB_position ()
			newBone.rotation.controller = TCB_rotation ()	
 if (BoneParent[a] != -1) then
 newBone.parent = BNArr[(BoneParent[a])+1]						  
append BNArr newBone					  
)	

Print ("Last Read @ 0x"+((bit.intAsHex(ftell f))as string))
	
rootRT = dummy name:"Main_Root" boxsize:[.1,.1,.1]
for b in BNArr where b.parent==undefined do b.parent = rootRT
rootRT.transform = (matrix3 [1,0,0] [0,0,1] [0,-1,0] [0,0,0])	

fseek f 0x100 #seek_cur

Print ("Bone Map Start @ 0x"+((bit.intAsHex(ftell f))as string))
	
Pos=ftell f	

BoneMapArray00=#()	
BoneMapArray01=#()
BoneMapArray02=#()	
BoneMapArray03=#()	
BoneMapArray04=#()	
BoneMapArray05=#()
BoneMapArray06=#()			
	
fseek f Pos #seek_set
if UsedBoneCount==1 then
(
UBoneCount=1	
for i = 1 to UBoneCount do (
BoneMapCount00=readBElong f	
for i = 1 to BoneMapCount00 do (	
BMap00=(readbyte f#unsigned +1)
append BoneMapArray00 BMap00	
)	
Stride00=(24-BoneMapCount00)
fseek f Stride00 #seek_cur
)	
)
if UsedBoneCount==2 then
(
UBoneCount=1	
for i = 1 to UBoneCount do (
BoneMapCount00=readBElong f		
for i = 1 to BoneMapCount00 do (	
BMap00=(readbyte f#unsigned +1)
append BoneMapArray00 BMap00	
)	
Stride00=(24-BoneMapCount00)
fseek f Stride00 #seek_cur
BoneMapCount01=readBElong f		
for i = 1 to BoneMapCount01 do (	
BMap01=(readbyte f#unsigned +1) 
append BoneMapArray01 BMap01		
)	
Stride01=(24-BoneMapCount01)
fseek f Stride01 #seek_cur		
)	
)
if UsedBoneCount==3 then
(
UBoneCount=1	
for i = 1 to UBoneCount do (
BoneMapCount00=readBElong f		
for i = 1 to BoneMapCount00 do (	
BMap00=(readbyte f#unsigned +1)
append BoneMapArray00 BMap00	
)	
Stride00=(24-BoneMapCount00)
fseek f Stride00 #seek_cur
BoneMapCount01=readBElong f		
for i = 1 to BoneMapCount01 do (	
BMap01=(readbyte f#unsigned +1) 
append BoneMapArray01 BMap01		
)	
Stride01=(24-BoneMapCount01)
fseek f Stride01 #seek_cur	
BoneMapCount02=readBElong f		
for i = 1 to BoneMapCount02 do (	
BMap02=(readbyte f#unsigned +1) 
append BoneMapArray02 BMap02		
)
Stride02=(24-BoneMapCount02)
fseek f Stride02 #seek_cur		
)	
)
if UsedBoneCount==4 then
(
UBoneCount=1	
for i = 1 to UBoneCount do (
BoneMapCount00=readBElong f		
for i = 1 to BoneMapCount00 do (	
BMap00=(readbyte f#unsigned +1)
append BoneMapArray00 BMap00	
)	
Stride00=(24-BoneMapCount00)
fseek f Stride00 #seek_cur
BoneMapCount01=readBElong f		
for i = 1 to BoneMapCount01 do (	
BMap01=(readbyte f#unsigned +1) 
append BoneMapArray01 BMap01		
)	
Stride01=(24-BoneMapCount01)
fseek f Stride01 #seek_cur	
BoneMapCount02=readBElong f		
for i = 1 to BoneMapCount02 do (	
BMap02=(readbyte f#unsigned +1) 
append BoneMapArray02 BMap02		
)
Stride02=(24-BoneMapCount02)
fseek f Stride02 #seek_cur	
BoneMapCount03=readBElong f		
for i = 1 to BoneMapCount03 do (	
BMap03=(readbyte f#unsigned +1) 
append BoneMapArray03 BMap03		
)	
Stride03=(24-BoneMapCount03)
fseek f Stride03 #seek_cur		
)	
)
if UsedBoneCount==5 then
(
UBoneCount=1	
for i = 1 to UBoneCount do (
BoneMapCount00=readBElong f		
for i = 1 to BoneMapCount00 do (	
BMap00=(readbyte f#unsigned +1)
append BoneMapArray00 BMap00	
)	
Stride00=(24-BoneMapCount00)
fseek f Stride00 #seek_cur
BoneMapCount01=readBElong f		
for i = 1 to BoneMapCount01 do (	
BMap01=(readbyte f#unsigned +1) 
append BoneMapArray01 BMap01		
)	
Stride01=(24-BoneMapCount01)
fseek f Stride01 #seek_cur	
BoneMapCount02=readBElong f		
for i = 1 to BoneMapCount02 do (	
BMap02=(readbyte f#unsigned +1) 
append BoneMapArray02 BMap02		
)
Stride02=(24-BoneMapCount02)
fseek f Stride02 #seek_cur	
BoneMapCount03=readBElong f		
for i = 1 to BoneMapCount03 do (	
BMap03=(readbyte f#unsigned +1) 
append BoneMapArray03 BMap03		
)	
Stride03=(24-BoneMapCount03)
fseek f Stride03 #seek_cur	
BoneMapCount04=readBElong f		
for i = 1 to BoneMapCount04 do (	
BMap04=(readbyte f#unsigned +1) 
append BoneMapArray04 BMap04		
)	
Stride04=(24-BoneMapCount04)
fseek f Stride04 #seek_cur	
)	
)
if UsedBoneCount==6 then
(
UBoneCount=1	
for i = 1 to UBoneCount do (
BoneMapCount00=readBElong f		
for i = 1 to BoneMapCount00 do (	
BMap00=(readbyte f#unsigned +1)
append BoneMapArray00 BMap00	
)	
Stride00=(24-BoneMapCount00)
fseek f Stride00 #seek_cur
BoneMapCount01=readBElong f		
for i = 1 to BoneMapCount01 do (	
BMap01=(readbyte f#unsigned +1) 
append BoneMapArray01 BMap01		
)	
Stride01=(24-BoneMapCount01)
fseek f Stride01 #seek_cur	
BoneMapCount02=readBElong f		
for i = 1 to BoneMapCount02 do (	
BMap02=(readbyte f#unsigned +1) 
append BoneMapArray02 BMap02		
)
Stride02=(24-BoneMapCount02)
fseek f Stride02 #seek_cur	
BoneMapCount03=readBElong f		
for i = 1 to BoneMapCount03 do (	
BMap03=(readbyte f#unsigned +1) 
append BoneMapArray03 BMap03		
)	
Stride03=(24-BoneMapCount03)
fseek f Stride03 #seek_cur	
BoneMapCount04=readBElong f		
for i = 1 to BoneMapCount04 do (	
BMap04=(readbyte f#unsigned +1) 
append BoneMapArray04 BMap04		
)	
Stride04=(24-BoneMapCount04)
fseek f Stride04 #seek_cur	
BoneMapCount05=readBElong f		
for i = 1 to BoneMapCount05 do (		
BMap05=(readbyte f#unsigned +1) 
append BoneMapArray05 BMap05		
)	
Stride05=(24-BoneMapCount05)
fseek f Stride05 #seek_cur	
)	
)
if UsedBoneCount==7 then
(
UBoneCount=1	
for i = 1 to UBoneCount do (
BoneMapCount00=readBElong f		
for i = 1 to BoneMapCount00 do (	
BMap00=(readbyte f#unsigned +1)
append BoneMapArray00 BMap00	
)	
Stride00=(24-BoneMapCount00)
fseek f Stride00 #seek_cur
BoneMapCount01=readBElong f		
for i = 1 to BoneMapCount01 do (	
BMap01=(readbyte f#unsigned +1) 
append BoneMapArray01 BMap01		
)	
Stride01=(24-BoneMapCount01)
fseek f Stride01 #seek_cur	
BoneMapCount02=readBElong f		
for i = 1 to BoneMapCount02 do (	
BMap02=(readbyte f#unsigned +1) 
append BoneMapArray02 BMap02		
)
Stride02=(24-BoneMapCount02)
fseek f Stride02 #seek_cur	
BoneMapCount03=readBElong f		
for i = 1 to BoneMapCount03 do (	
BMap03=(readbyte f#unsigned +1) 
append BoneMapArray03 BMap03		
)	
Stride03=(24-BoneMapCount03)
fseek f Stride03 #seek_cur	
BoneMapCount04=readBElong f		
for i = 1 to BoneMapCount04 do (	
BMap04=(readbyte f#unsigned +1) 
append BoneMapArray04 BMap04		
)	
Stride04=(24-BoneMapCount04)
fseek f Stride04 #seek_cur	
BoneMapCount05=readBElong f		
for i = 1 to BoneMapCount05 do (		
BMap05=(readbyte f#unsigned +1) 
append BoneMapArray05 BMap05		
)	
Stride05=(24-BoneMapCount05)
fseek f Stride05 #seek_cur		
BoneMapCount06=readBElong f		
for i = 1 to BoneMapCount06 do (		
BMap06=(readbyte f#unsigned +1) 
append BoneMapArray06 BMap06		
)	
Stride06=(24-BoneMapCount06)
)	
)

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

fseek f itemoff #seek_set
Print ("Item Offset @ 0x"+((bit.intAsHex(ftell f))as string))
for i = 1 to meshcount do (
getPos = ftell f + 48
unk1 = ReadBEword f
VertCount = ReadBEword f	
unk2 = readBElong f	
VId2 = readbyte f #unsigned	
VId = readbyte f	#unsigned
VSize = readbyte f
VType = readbyte f	
VertStart = readBElong f	
unk4 = ReadBEword f	
unk5 = ReadBEword f		
unk6 = readBElong f	
FacePos = readBElong f
FaceCount = readBElong f
Null01 = ReadBElong f
MeshId=ReadBEword f+1		
bone0 = ReadBEword f+1	
VStart = ReadBEword f
VEnd = ReadBEword f
Mshj = ReadBElong f
Null02 = ReadBElong f
Null03 = ReadBElong f	
append Mesh_Info (Mesh_Info_Struct VertCount:VertCount VSize:VSize VId:VId VId2:VId2 VType:VType VertStart:VertStart FacePos:FacePos FaceCount:FaceCount VStart:VStart VEnd:VEnd MeshId:MeshId )		
fseek f getPos #seek_set
)

Print ("Item Off End @ 0x"+((bit.intAsHex(ftell f))as string))
	
print Mesh_Info
		
Print ("Vertex Offset @ 0x"+((bit.intAsHex(ftell f))as string))
	
for i = 1 to meshcount do (	
	
vertArray=#()
faceArray=#()
UV_array=#()	
Weight_array=#()	

fseek f vertOffset #seek_set			

if  (Mesh_Info[i].VSize)==12 do 	
for x = 1 to Mesh_Info[i].VertCount do (
Bone3 = -1
Bone4 = -1
weight3 = 0
weight4 = 0	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
	
Bone1 = readbyte f #unsigned
Bone2 = readbyte f #unsigned

weight1 = 0	
weight2 = 1.00	
	
tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1

	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==16 do 	
for x = 1 to Mesh_Info[i].VertCount do (
Bone3 = -1
Bone4 = -1
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
weight1 = (ReadBEword f /32767.00)	
tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1
unk2	= (readbyte f #unsigned/255.00)
Bone1 = readbyte f #unsigned
unk3	= (readbyte f #unsigned/255.00)
Bone2 = readbyte f #unsigned
	
if weight1!=1.00 then
weight2 = (1.00 - weight1)	
weight4 = 0

if weight1==1.00 then
weight2 = 0
weight3 = 0	
		
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==20 then
if  (Mesh_Info[i].VType)==12 do
for x = 1 to Mesh_Info[i].VertCount do (
Bone3 = -1
Bone4 = -1
weight3 = 0
weight4 = 0	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
	
Bone1 = readbyte f #unsigned
Bone2 = readbyte f #unsigned

weight1 = 0	
weight2 = 1.00	
		
unk1 = (readbyte f #unsigned/255.00)	
unk2 = (readbyte f #unsigned/255.00)
unk3 = (readbyte f #unsigned/255.00)			
unk4 = (readbyte f #unsigned/255.00)	
unk5 = (readbyte f #unsigned/255.00)	
unk6 = (readbyte f #unsigned/255.00)
unk7 = (readbyte f #unsigned/255.00)	
unk8 = (readbyte f #unsigned/255.00)

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==20 then
if  (Mesh_Info[i].VType)==14 do
for x = 1 to Mesh_Info[i].VertCount do (
Bone3 = -1
Bone4 = -1
weight3 = 0
weight4 = 0	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
	
Bone1 = readbyte f #unsigned
Bone2 = readbyte f #unsigned

weight1 = 0	
weight2 = 1.00	
		
unk1 = (readbyte f #unsigned/255.00)	
unk2 = (readbyte f #unsigned/255.00)
unk3 = (readbyte f #unsigned/255.00)			
unk4 = (readbyte f #unsigned/255.00)	
unk5 = (readbyte f #unsigned/255.00)	
unk6 = (readbyte f #unsigned/255.00)
unk7 = (readbyte f #unsigned/255.00)	
unk8 = (readbyte f #unsigned/255.00)

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==20 then
if  (Mesh_Info[i].VType)==15 do
for x = 1 to Mesh_Info[i].VertCount do (
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
p1 = ReadBEword f 

Bone1 = readbyte f #unsigned
Bone2 = readbyte f #unsigned
Bone3 = readbyte f #unsigned
Bone4 = readbyte f #unsigned
	
weight1 = (readbyte f #unsigned/255.00)
weight2 = (readbyte f #unsigned/255.00)
weight3 = (readbyte f #unsigned/255.00)
weight4 = (readbyte f #unsigned/255.00)	
	
tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==20 then
if  (Mesh_Info[i].VType)==16 do
for x = 1 to Mesh_Info[i].VertCount do (
Bone3 = -1
Bone4 = -1
weight3 = 0
weight4 = 0	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
	
Bone1 = readbyte f #unsigned
Bone2 = readbyte f #unsigned
	
weight1 = 0	
weight2 = 1.00	
		
unk1 = (readbyte f #unsigned/255.00)	
unk2 = (readbyte f #unsigned/255.00)
unk3 = (readbyte f #unsigned/255.00)			
unk4 = (readbyte f #unsigned/255.00)	
unk5 = (readbyte f #unsigned/255.00)	
unk6 = (readbyte f #unsigned/255.00)
unk7 = (readbyte f #unsigned/255.00)	
unk8 = (readbyte f #unsigned/255.00)

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==20 then
if  (Mesh_Info[i].VType)==18 do
for x = 1 to Mesh_Info[i].VertCount do (
Bone3 = -1
Bone4 = -1
weight3 = 0
weight4 = 0	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
	
Bone1 = readbyte f #unsigned
Bone2 = readbyte f #unsigned

weight1 = 0	
weight2 = 1.00	
		
unk1 = (readbyte f #unsigned/255.00)	
unk2 = (readbyte f #unsigned/255.00)
unk3 = (readbyte f #unsigned/255.00)			
unk4 = (readbyte f #unsigned/255.00)	
unk5 = (readbyte f #unsigned/255.00)	
unk6 = (readbyte f #unsigned/255.00)
unk7 = (readbyte f #unsigned/255.00)	
unk8 = (readbyte f #unsigned/255.00)

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==20 then
if  (Mesh_Info[i].VType)==19 do
for x = 1 to Mesh_Info[i].VertCount do (
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
p1 = ReadBEword f 

Bone1 = readbyte f #unsigned
Bone2 = readbyte f #unsigned
Bone3 = readbyte f #unsigned
Bone4 = readbyte f #unsigned
	
weight1 = (readbyte f #unsigned/255.00)
weight2 = (readbyte f #unsigned/255.00)
weight3 = (readbyte f #unsigned/255.00)
weight4 = (readbyte f #unsigned/255.00)	
	
tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==24 then
if  (Mesh_Info[i].VId2)==129 do
for x = 1 to Mesh_Info[i].VertCount do (
Bone3 = -1
Bone4 = -1
weight3 = 0
weight4 = 0	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
	
Bone1 = readbyte f #unsigned
Bone2 = readbyte f #unsigned

weight1 = 0	
weight2 = 1.00	
	
unk1 = (readbyte f #unsigned/255.00)	
unk2 = (readbyte f #unsigned/255.00)
unk3 = (readbyte f #unsigned/255.00)			
unk4 = (readbyte f #unsigned/255.00)	
unk5 = (readbyte f #unsigned/255.00)	
unk6 = (readbyte f #unsigned/255.00)
unk7 = (readbyte f #unsigned/255.00)	
unk8 = (readbyte f #unsigned/255.00)

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	

unk1= readBEHalfFloat f	
unk2= readBEHalfFloat f*-1	
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==24 then
if  (Mesh_Info[i].VId2)==130 do
for x = 1 to Mesh_Info[i].VertCount do (	
Bone3 = -1
Bone4 = -1
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
weight1 = (ReadBEword f /32767.00)
p1= ReadBEword f
p2= ReadBEword f
p3= ReadBEword f
p4= ReadBEword f		
tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1
bone1 = (ReadBEHalfFloat f) as integer
bone2 = (ReadBEHalfFloat f) as integer

weight4 = 0

if weight1!=1.00 then
weight2 = (1.00 - weight1)	
weight3 = 0

if weight1==1.00 then
weight2 = 0
weight3 = 0	
		
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1 )
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2 )
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3 )
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4 )
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VType)==16 then	
if  (Mesh_Info[i].VId2)!=132 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)

weight1 =  (ReadBEword f /32767.00)

p1= ReadBEword f
p2= ReadBEword f	
p3= ReadBEword f	
p4= ReadBEword f	

bone1 = readbyte f #unsigned
bone2 = readbyte f #unsigned
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned
		
tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
weight2 = readBEHalfFloat f	
weight3 = readBEHalfFloat f	

weight4 = 1.00-(weight1+weight2+weight3)

if weight1==1.00 then
weight2 = 0
weight3 = 0
weight4 = 0

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2) 
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)  
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4) 
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VId2)==128 do
for x = 1 to Mesh_Info[i].VertCount do (	
Bone1 = -1
Bone2 = -1	
Bone3 = -1
Bone4 = -1
weight1 = 0
weight2 = 0	
weight3 = 0
weight4 = 0	
vx = readBEfloat f
vy = readBEfloat f
vz = readBEfloat f

p1= ReadBEword f
p2= ReadBEword f	
p3= ReadBEword f	
p4= ReadBEword f	

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
weight01 = readbyte f #unsigned	
weight02 = readbyte f #unsigned
weight03 = readbyte f #unsigned
weight04 = readbyte f #unsigned
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1/255.00) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.00) 
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.00)  
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4/255.00) 
      )      
   )
append Weight_array w	
append vertArray [vx,vy,vz]
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VId2)==129 then
if  (Mesh_Info[i].VId)==2 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)

weight1 =  (ReadBEword f /32767.00)

p1= ReadBEword f
p2= ReadBEword f	
p3= ReadBEword f	
p4= ReadBEword f	

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
bone1 = (ReadBEHalfFloat f) as integer
bone2 = (ReadBEHalfFloat f) as integer
	
unk2 = readBEHalfFloat f	
unk3 = readBEHalfFloat f	

weight4 = 0

if weight1!=1.00 then
weight2 = (1.00 - weight1)	
weight3 = 0


if weight1==1.00 then
weight2 = 0
weight3 = 0

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2) 
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)  
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4) 
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)

if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VId2)==129 then
if  (Mesh_Info[i].VId)==0 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = readBEFloat f	
vy = readBEFloat f	
vz = readBEFloat f	

Unk = ReadBElong f

tu= readBEFloat f	
tv= readBEFloat f*-1	
	
bone2 = readbyte f #unsigned	
bone1 = readbyte f #unsigned

weight1 = readbyte f #unsigned
weight2 = readbyte f #unsigned
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1/255) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255) 
      )    
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)

if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VId2)==129 then
if  (Mesh_Info[i].VId)==1 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = readBEFloat f	
vy = readBEFloat f	
vz = readBEFloat f	

Unk = ReadBElong f

tu= readBEFloat f	
tv= readBEFloat f*-1	
	
bone2 = readbyte f #unsigned	
bone1 = readbyte f #unsigned

weight1 = readbyte f #unsigned
weight2 = readbyte f #unsigned
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1/255) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255) 
      )    
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)

if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VId2)==130 then
if  (Mesh_Info[i].VId)==2 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)

weight1 =  (ReadBEword f /32767.00)

p1= ReadBEword f
p2= ReadBEword f	
p3= ReadBEword f	
p4= ReadBEword f	

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
bone1 = (ReadBEHalfFloat f) as integer
bone2 = (ReadBEHalfFloat f) as integer
	
unk2 = readBEHalfFloat f	
unk3 = readBEHalfFloat f	

weight4 = 0

if weight1!=1.00 then
weight2 = (1.00 - weight1)	
weight3 = 0


if weight1==1.00 then
weight2 = 0
weight3 = 0

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2) 
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)  
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4) 
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VId2)==130 then
if  (Mesh_Info[i].VId)==0 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = readBEFloat f	
vy = readBEFloat f	
vz = readBEFloat f	

Unk = ReadBElong f

tu= readBEFloat f	
tv= readBEFloat f*-1	
	
bone2 = readbyte f #unsigned	
bone1 = readbyte f #unsigned

weight1 = readbyte f #unsigned
weight2 = readbyte f #unsigned
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1/255) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255) 
      )    
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VId2)==130 then
if  (Mesh_Info[i].VId)==1 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = readBEFloat f	
vy = readBEFloat f	
vz = readBEFloat f	

Unk = ReadBElong f

tu= readBEFloat f	
tv= readBEFloat f*-1	
	
bone2 = readbyte f #unsigned	
bone1 = readbyte f #unsigned

weight1 = readbyte f #unsigned
weight2 = readbyte f #unsigned
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1/255) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255) 
      )    
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VId2)==131 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)

weight1 =  (ReadBEword f /32767.00)

p1= ReadBEword f
p2= ReadBEword f	
p3= ReadBEword f	
p4= ReadBEword f	

bone1 = readbyte f #unsigned
bone2 = readbyte f #unsigned
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
	
tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
weight2 = readBEHalfFloat f	
weight3 = readBEHalfFloat f	

weight4 = 1.00-(weight1+weight2+weight3)
	
if weight1==1.00 then
weight2 = 0
weight3 = 0
weight4 = 0

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2) 
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)  
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4) 
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==28 then
if  (Mesh_Info[i].VId2)==132 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)

weight1 =  (ReadBEword f /32767.00)

p1= ReadBEword f
p2= ReadBEword f	
p3= ReadBEword f	
p4= ReadBEword f	


bone1 = readbyte f #unsigned
bone2 = readbyte f #unsigned
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
	
tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	
	
weight2 = readBEHalfFloat f	
weight3 = readBEHalfFloat f	

weight4 = 1.00-(weight1+weight2+weight3)
	
if weight1==1.00 then
weight2 = 0
weight3 = 0	
weight4 = 0

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2) 
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3)  
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
         append w.weights (w4) 
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==28 then
if (Mesh_Info[i].VType)==19 do	
for x = 1 to Mesh_Info[i].VertCount do (	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
	
p1= ReadBEword f

bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned
bone5 = readbyte f #unsigned	
bone6 = readbyte f #unsigned	
bone7 = readbyte f #unsigned	
bone8 = readbyte f #unsigned
	
weight1 = readbyte f #unsigned	
weight2 = readbyte f #unsigned	
weight3 = readbyte f #unsigned	
weight4 = readbyte f #unsigned
weight5 = readbyte f #unsigned	
weight6 = readbyte f #unsigned	
weight7 = readbyte f #unsigned	
weight8 = readbyte f #unsigned

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4
if(weight5 != 0) then
   maxweight = maxweight + weight5
if(weight6 != 0) then
   maxweight = maxweight + weight6
if(weight7 != 0) then
   maxweight = maxweight + weight7
if(weight8 != 0) then
   maxweight = maxweight + weight8

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1/255.00)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.00)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.00)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.00)
      )      
	  if(weight5 != 0) then (
         w1 = weight5 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1/255.00)
      )
      if(weight6 != 0) then (
         w2 = weight6 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.00)
      )
      if(weight7 != 0) then (
         w3 = weight7 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.00)
      )
      if(weight8 != 0) then (
         w8 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.00)
      )  
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)
if  (Mesh_Info[i].VSize)==32 then 
if  (Mesh_Info[i].VId2)!=130 do
for x = 1 to Mesh_Info[i].VertCount do (	
vx = (ReadBEword f /32767.00)
vy = (ReadBEword f /32767.00)
vz = (ReadBEword f /32767.00)
weight1 = (ReadBEword f/32767.00)
	
p1= ReadBEword f
p2= ReadBEword f
p3= ReadBEword f
p4= ReadBEword f
	
bone1 = readbyte f #unsigned
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned		
bone4 = readbyte f #unsigned

tu= readBEHalfFloat f	
tv= readBEHalfFloat f*-1	

weight2 = readBEHalfFloat f	
weight3 = readBEHalfFloat f	

p5= ReadBEword f
p6= ReadBEword f	

weight4 = 1.00-(weight1+weight2+weight3)

if weight1==1.00 then
weight2 = 0
weight3 = 0


w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
		 append w.weights (w1) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
		 append w.weights (w2) 
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
		 append w.weights (w3) 
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
		 append w.weights (w4) 
      )      
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)

if  (Mesh_Info[i].VSize)==32 then 
if  (Mesh_Info[i].VId2)==130 then
for x = 1 to Mesh_Info[i].VertCount do (	
vx = readBEFloat f	
vy = readBEFloat f	
vz = readBEFloat f	

Unk = ReadBElong f

tu= readBEFloat f	
tv= readBEFloat f*-1	

bone2 = readbyte f #unsigned	
bone1 = readbyte f #unsigned

weight1 = readbyte f #unsigned
weight2 = readbyte f #unsigned
	
tu0= readBEHalfFloat f	
tv0= readBEHalfFloat f*-1	
	
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
         append w.weights (w1/255) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255) 
      )    
   )
append Weight_array w	
append vertArray ([vx,vy,vz]*100)
append UV_array [tu,tv,0]
)

if  (Mesh_Info[i].VSize)==36 do 
if (Mesh_Info[i].VType)==14 do	
for x = 1 to Mesh_Info[i].VertCount do (	
	
vx = (ReadBEfloat f)
vy = (ReadBEfloat f)
vz = (ReadBEfloat f)

fseek f 0x18 #seek_cur
	
bone1 = -1
bone2 = -1	
bone3 = -1	
bone4 = -1
	
weight1 = 0.00	
weight2 = 0.00
weight3 = 0.00	
weight4 = 0.00
		
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2
if(weight3 != 0) then
   maxweight = maxweight + weight3
if(weight4 != 0) then
   maxweight = maxweight + weight4

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
		 append w.weights (w1) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
		 append w.weights (w2) 
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
		 append w.weights (w3) 
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
		 append w.weights (w4) 
      )      
   )
append Weight_array w	
append vertArray ([vx,-vz,vy]*100)
append UV_array [0,0,0]
)

if  (Mesh_Info[i].VSize)==36 do 
if (Mesh_Info[i].VType)==12 do	
for x = 1 to Mesh_Info[i].VertCount do (	
	
vx = (ReadBEfloat f)
vy = (ReadBEfloat f)
vz = (ReadBEfloat f)
	
fseek f 0x4 #seek_cur
	
tu= (ReadBEfloat f)	
tv= (ReadBEfloat f)*-1		
		
bone1 = readbyte f #unsigned 
bone2 = readbyte f #unsigned 	
	
weight1 = readbyte f #unsigned 	 
weight2 = readbyte f #unsigned 	
	
fseek f 0x8 #seek_cur	

w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
   maxweight = maxweight + weight1
if(weight2 != 0) then
   maxweight = maxweight + weight2

if(maxweight != 0) then (
      if(weight1 != 0) then (
         w1 = weight1 as float
         append w.boneids (bone1 + 1)
		 append w.weights (w1/255.0) 
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
		 append w.weights (w2/255.0) 
      )   
   )
append Weight_array w	
append vertArray ([vx,-vz,vy]*100)
append UV_array [tu,tv,0]
)

vertOffset+=(Mesh_Info[i].VertCount*Mesh_Info[i].VSize)

faceStart=(faceOffset+(Mesh_Info[i].FacePos*2))

fseek f faceStart #seek_set

for x = 1 to (Mesh_Info[i].FaceCount/3) do 
(
f1 = ((ReadBEshort f + 1) - Mesh_Info[i].VertStart)
f2 = ((ReadBEshort f + 1) - Mesh_Info[i].VertStart)
f3 = ((ReadBEshort f + 1) - Mesh_Info[i].VertStart)
append Facearray [f1,f2,f3]
)
		
try(
msh = mesh vertices:vertArray faces:faceArray
msh.numTVerts = UV_array.count
msh.name=("Weapon_"+i as string)	
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 = on
msh.wirecolor = (color 230 200 210)

max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
if Mesh_Info[i].MeshId==1 then
(	
for i = 1 to BoneMapArray00.count do
( 
	
	maxbone =  BNArr[(BoneMapArray00[i])]
	if i != BoneMapArray00.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
)
if Mesh_Info[i].MeshId==2 then
(	
for i = 1 to BoneMapArray01.count do
( 
	
	maxbone =  BNArr[(BoneMapArray01[i])]
	if i != BoneMapArray01.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
)
if Mesh_Info[i].MeshId==3 then
(	
for i = 1 to BoneMapArray02.count do
( 
	
	maxbone =  BNArr[(BoneMapArray02[i])]
	if i != BoneMapArray02.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
)
if Mesh_Info[i].MeshId==4 then
(	
for i = 1 to BoneMapArray03.count do
( 
	
	maxbone =  BNArr[(BoneMapArray03[i])]
	if i != BoneMapArray03.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
)
if Mesh_Info[i].MeshId==5 then
(	
for i = 1 to BoneMapArray04.count do
( 
	
	maxbone =  BNArr[(BoneMapArray04[i])]
	if i != BoneMapArray04.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
)
if Mesh_Info[i].MeshId==6 then
(	
for i = 1 to BoneMapArray05.count do
( 
	
	maxbone =  BNArr[(BoneMapArray05[i])]
	if i != BoneMapArray05.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
)
modPanel.setCurrentObject skinMod
for i = 1 to (Mesh_Info[i].VertCount) do (
   w = Weight_array[i]
   bi = #() 
   wv = #()
   
   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
   
)
if Mesh_Info[i].MeshId==7 then
(	
for i = 1 to BoneMapArray06.count do
( 
	
	maxbone =  BNArr[(BoneMapArray06[i])]
	if i != BoneMapArray05.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
)
modPanel.setCurrentObject skinMod
for i = 1 to (Mesh_Info[i].VertCount) do (
   w = Weight_array[i]
   bi = #() 
   wv = #()
   
   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
   
)
)catch(format "Error!!\n")
format "Mesh %\n\tMesh End: %\n\tFace Start: %\n\tVertex Size: %\n\tVertex Id: %\n\tVertex Type: %\n\n" i ("0x"+((bit.intAsHex(vertOffset))as string)) ("0x"+((bit.intAsHex(faceStart))as string)) (Mesh_Info[i].VSize as string) (Mesh_Info[i].VId as string) (Mesh_Info[i].VType as string)
)
	
fclose f