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 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 Mesh_Info_Struct
(
VertexCount, FaceCount, VertexId,UsedBones, VertexEnd,Vsize
)		
clearlistener()

fseek f (0x0) #seek_set
Magic=readlong f 
if Magic!=-1547335172 then fclose f
if Magic==-1547335172 then (
fseek f (0x27) #seek_set
MeshNameSize=readbyte f#unsigned 
MeshName=readFixedString f MeshNameSize

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

fseek f (0xF) #seek_cur

Print ("!!!!! @ 0x"+((bit.intAsHex(ftell f))as string))	
	
MeshVersionSize=readbyte f#unsigned 
MeshVersion=readFixedString f MeshVersionSize
CountLong=readBElong f
	
if CountLong==3 then 	fseek f (0xC) #seek_cur
if CountLong==6 then 	fseek f (0x10) #seek_cur	
fseek f (0x58) #seek_cur
UnkCount=readBElong f
MeshCount=readBElong f	
fseek f (0x8) #seek_cur

Start=ftell f
	
for i=1 to MeshCount do (	
	
fseek f (0x204) #seek_cur		
	
vertArray = #()
faceArray = #()
UV_array = #()
Weight_array=#()	
	
VIDSec=readBElong f
VertCount=readBElong f
UnkCount=readBElong f
	
print VIDSec	

if VIDSec==88 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
FFFF=readlong f	
	
fseek f 0x1C #seek_cur		
	
tu00=Readfloat f	
tv00=Readfloat f	*-1	

tu01=Readfloat f	
tv01=Readfloat f	*-1		
	
tu02=Readfloat f	
tv02=Readfloat f	*-1	
	
fseek f getPos #seek_set		
)	
	
if VIDSec==84 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
fseek f 0x1C #seek_cur		
	
tu00=Readfloat f	
tv00=Readfloat f	*-1	

tu01=Readfloat f	
tv01=Readfloat f	*-1		
	
tu02=Readfloat f	
tv02=Readfloat f	*-1	
	
fseek f getPos #seek_set		
)	

if VIDSec==76 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
		
fseek f 0x1C #seek_cur		
	
tu00=Readfloat f	
tv00=Readfloat f	*-1	

tu01=Readfloat f	
tv01=Readfloat f	*-1		
	
fseek f getPos #seek_set		
)

if VIDSec==72 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	

FFFF=readlong f
	
fseek f 0x1C #seek_cur		
	
tu=Readfloat f	
tv=Readfloat f	*-1	
	
fseek f getPos #seek_set		
)	

if VIDSec==68 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
fseek f 0x1C #seek_cur		
	
tu=Readfloat f	
tv=Readfloat f	*-1	

fseek f getPos #seek_set		
)	

if VIDSec==64 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
fseek f getPos #seek_set		
)	

if VIDSec==48 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
fseek f getPos #seek_set		
)	

Print ("End @ 0x"+((bit.intAsHex(ftell f))as string))	
	
FacePos=ftell f
FaceCheck=readBElong f
if FaceCheck>1 then (	
fseek f FacePos #seek_set	
FaceSize=readBElong f
FaceCount=readBElong f
)

if FaceCheck<=1 then  (	
FaceSize=readBElong f
FaceCount=readBElong f
)

Print ("FaceStart @ 0x"+((bit.intAsHex(ftell f))as string))		
	
for x=1 to FaceCount/3 do (	
fa=readshort f #unsigned +1
fb=readshort f #unsigned +1
fc=readshort f #unsigned +1
)	

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

if VIDSec>48 then (	

SecId=readBElong f	
	
if SecId==4 then fseek f (0x8) #seek_cur
if SecId!=4 then fseek f (0xA) #seek_cur
	
for x = 1 to VertCount do (
getPos = ftell f + 36
fseek f getPos #seek_set		
)		
	
Print ("SecEnd @ 0x"+((bit.intAsHex(ftell f))as string))	

Pos=ftell f
CheckShort=readshort f
if CheckShort!=0 then fseek f Pos #seek_set 	
if CheckShort==0 then fseek f 0x0 #seek_cur	
	
BoneMapCount=readbyte f	

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

for x = 1 to BoneMapCount do (
BoneMap=readbyte f	
)			

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

if BoneMapCount==0 then (
fseek f (0x7) #seek_cur
	
Pos0=ftell f
Null0=readbyte f
if Null0!=0 then fseek f (Pos0-3) #seek_set 	
if Null0==0 then (
fseek f 0x0 #seek_cur	
Pos1=ftell f
Null1=readbyte f
if Null1!=0 then fseek f (Pos1-3) #seek_set 	
if Null1==0 then (
fseek f 0x0 #seek_cur	
Pos2=ftell f
Null2=readbyte f
if Null2!=0 then fseek f (Pos2-3) #seek_set 	
if Null2==0 then (
fseek f 0x0 #seek_cur	
Pos3=ftell f
Null3=readbyte f
if Null3!=0 then fseek f (Pos3-3) #seek_set 	
if Null3==0 then fseek f 0x0 #seek_cur			
)		
)	
)		
	
)	
if BoneMapCount!=0 then (
fseek f (0xB) #seek_cur

Pos0=ftell f
Null0=readbyte f
if Null0!=0 then fseek f (Pos0-3) #seek_set 	
if Null0==0 then (
fseek f 0x0 #seek_cur	
Pos1=ftell f
Null1=readbyte f
if Null1!=0 then fseek f (Pos1-3) #seek_set 	
if Null1==0 then (
fseek f 0x0 #seek_cur	
Pos2=ftell f
Null2=readbyte f
if Null2!=0 then fseek f (Pos2-3) #seek_set 	
if Null2==0 then (
fseek f 0x0 #seek_cur	
Pos3=ftell f
Null3=readbyte f
if Null3!=0 then fseek f (Pos3-3) #seek_set 	
if Null3==0 then fseek f 0x0 #seek_cur			
)		
)	
)		
	
)
)	
)

BNArr=#()

Null=readbyte f
Pos00=ftell f
Null00=readbyte f
if Null00!=0 then fseek f Pos00 #seek_set 	
if Null00==0 then (
fseek f 0x0 #seek_cur	
Pos01=ftell f
Null01=readbyte f
if Null01!=0 then fseek f Pos01 #seek_set 	
if Null01==0 then (
fseek f 0x0 #seek_cur	
Pos02=ftell f
Null02=readbyte f
if Null02!=0 then fseek f Pos02 #seek_set 	
if Null02==0 then (
fseek f 0x0 #seek_cur	
Pos03=ftell f
Null03=readbyte f
if Null03!=0 then fseek f Pos03 #seek_set 	
if Null03==0 then fseek f 0x0 #seek_cur			
)		
)	
)	
BoneCount=readbyte f#unsigned
for i=1 to BoneCount do (
FloatB=readfloat f	
fseek f (0xB) #seek_cur	
BoneNameSize=readbyte f#unsigned
BoneName=readFixedString f BoneNameSize
Null0=readbyte f		
Null1=readbyte f	
if Null1==0 then (
Check=readbyte f
if Check==0 then fseek f (0xF) #seek_cur
if Check!=0 then fseek f (0xE) #seek_cur	
)	
if Null1!=0 then (
if Null0!=0 then fseek f (0xD) #seek_cur	
if Null0==0 then fseek f (0xE) #seek_cur	
)	
BoneNameSize2=readbyte f#unsigned
BoneName2=readFixedString f BoneNameSize2
FloatB=readBEfloat f
BoneId=readBElong f+1
Unk2=readBElong f
Unk3=readBElong f	
getPos = ftell f + 72
c11=readBEfloat f;c12=readBEfloat f;c13=readBEfloat f
BoneParent=readBElong f+1
c21=readBEfloat f;c22=readBEfloat f;c23=readBEfloat f;c24=readBEfloat f
c31=readBEfloat f;c32=readBEfloat f;c33=readBEfloat f;
vx=readBEfloat f;vy=readBEfloat f;vz=readBEfloat f
rx=readBEfloat f;ry=readBEfloat f;rz=readBEfloat f;rw=readBEfloat f*-1
fseek f getPos #seek_set		

tfm = (quat rx ry rz rw) as matrix3
tfm.row4 = [vx,vy,vz]
if (BoneParent != 0) do (
tfm = tfm * BNArr[(BoneParent)].objecttransform	
)	

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  
)	

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])	

Print ("Bone End @ 0x"+((bit.intAsHex(ftell f))as string))	
	
fseek f (Start) #seek_set	
	
for i=1 to MeshCount do (	
	
fseek f (0x204) #seek_cur		
	
vertArray = #()
faceArray = #()
UV_array = #()
Weight_array = #()	
BoneMapArray = #()
	
VIDSec=readBElong f
VertCount=readBElong f
UnkCount=readBElong f

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

if VIDSec==88 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
FFFF=readlong f	
	
fseek f 0x1C #seek_cur		
	
tu00=Readfloat f	
tv00=Readfloat f	*-1	

tu01=Readfloat f	
tv01=Readfloat f	*-1		
	
tu02=Readfloat f	
tv02=Readfloat f	*-1	
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned
	
weight1 = ReadFloat f
weight2 = ReadFloat f
weight3 = ReadFloat f
weight4 = ReadFloat f
	
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)
      )      
   )	
fseek f getPos #seek_set	
append Weight_array w	  
append vertArray ([vx,-vz,vy])
append UV_array [tu00,tv00,0]	
)	
	
if VIDSec==84 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
fseek f 0x1C #seek_cur		
	
tu00=Readfloat f	
tv00=Readfloat f	*-1	

tu01=Readfloat f	
tv01=Readfloat f	*-1		
	
tu02=Readfloat f	
tv02=Readfloat f	*-1	
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned
	
weight1 = ReadFloat f
weight2 = ReadFloat f
weight3 = ReadFloat f
weight4 = ReadFloat f
	
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)
      )      
   )	
fseek f getPos #seek_set	
append Weight_array w	  	
append vertArray ([vx,-vz,vy])
append UV_array [tu00,tv00,0]	
)	

if VIDSec==76 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
		
fseek f 0x1C #seek_cur		
	
tu00=Readfloat f	
tv00=Readfloat f	*-1	

tu01=Readfloat f	
tv01=Readfloat f	*-1		
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned
	
weight1 = ReadFloat f
weight2 = ReadFloat f
weight3 = ReadFloat f
weight4 = ReadFloat f
	
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)
      )      
   )	
fseek f getPos #seek_set	
append Weight_array w	  	
append vertArray ([vx,-vz,vy])
append UV_array [tu00,tv00,0]		
)

if VIDSec==72 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	

FFFF=readlong f
	
fseek f 0x1C #seek_cur		
	
tu=Readfloat f	
tv=Readfloat f	*-1	
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned
	
weight1 = ReadFloat f
weight2 = ReadFloat f
weight3 = ReadFloat f
weight4 = ReadFloat f
	
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)
      )      
   )	
fseek f getPos #seek_set	
append Weight_array w	  	
append vertArray ([vx,-vz,vy])
append UV_array [tu,tv,0]		
)	

if VIDSec==68 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
fseek f 0x1C #seek_cur		
	
tu=Readfloat f	
tv=Readfloat f	*-1	

bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned
	
weight1 = ReadFloat f
weight2 = ReadFloat f
weight3 = ReadFloat f
weight4 = ReadFloat f
	
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)
      )      
   )	
fseek f getPos #seek_set	
append Weight_array w	   
append vertArray ([vx,-vz,vy])
append UV_array [tu,tv,0]		
)	

if VIDSec==64 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
fseek f getPos #seek_set		
append vertArray ([-vx,-vz,vy])
)	

if VIDSec==48 then
for x = 1 to VertCount do (
getPos = ftell f + VIDSec

vx = ReadFloat f
vy = ReadFloat f
vz = ReadFloat f	
	
fseek f getPos #seek_set		
append vertArray ([-vx,-vz,vy])
)	

Print ("End @ 0x"+((bit.intAsHex(ftell f))as string))	
	
FacePos=ftell f
FaceCheck=readBElong f
if FaceCheck>1 then (	
fseek f FacePos #seek_set	
FaceSize=readBElong f
FaceCount=readBElong f
)

if FaceCheck<=1 then  (	
FaceSize=readBElong f
FaceCount=readBElong f
)

Print ("FaceStart @ 0x"+((bit.intAsHex(ftell f))as string))		
	
for x=1 to FaceCount/3 do (	
fa=readshort f #unsigned +1
fb=readshort f #unsigned +1
fc=readshort f #unsigned +1
append faceArray [fa,fb,fc]	
)	

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

if VIDSec>48 then (	

SecId=readBElong f	
	
if SecId==4 then fseek f (0x8) #seek_cur
if SecId!=4 then fseek f (0xA) #seek_cur
	
for x = 1 to VertCount do (
getPos = ftell f + 36
fseek f getPos #seek_set		
)		
	
Print ("SecEnd @ 0x"+((bit.intAsHex(ftell f))as string))	

Pos=ftell f
CheckShort=readshort f
if CheckShort!=0 then fseek f Pos #seek_set 	
if CheckShort==0 then fseek f 0x0 #seek_cur	
	
BoneMapCount=readbyte f	

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

for x = 1 to BoneMapCount do (
BoneMap=readbyte f+1	
append BoneMapArray BoneMap	
)			

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

if BoneMapCount==0 then (
fseek f (0x7) #seek_cur
	
Pos0=ftell f
Null0=readbyte f
if Null0!=0 then fseek f (Pos0-3) #seek_set 	
if Null0==0 then (
fseek f 0x0 #seek_cur	
Pos1=ftell f
Null1=readbyte f
if Null1!=0 then fseek f (Pos1-3) #seek_set 	
if Null1==0 then (
fseek f 0x0 #seek_cur	
Pos2=ftell f
Null2=readbyte f
if Null2!=0 then fseek f (Pos2-3) #seek_set 	
if Null2==0 then (
fseek f 0x0 #seek_cur	
Pos3=ftell f
Null3=readbyte f
if Null3!=0 then fseek f (Pos3-3) #seek_set 	
if Null3==0 then fseek f 0x0 #seek_cur			
)		
)	
)		
	
)	
if BoneMapCount!=0 then (
fseek f (0xB) #seek_cur

Pos0=ftell f
Null0=readbyte f
if Null0!=0 then fseek f (Pos0-3) #seek_set 	
if Null0==0 then (
fseek f 0x0 #seek_cur	
Pos1=ftell f
Null1=readbyte f
if Null1!=0 then fseek f (Pos1-3) #seek_set 	
if Null1==0 then (
fseek f 0x0 #seek_cur	
Pos2=ftell f
Null2=readbyte f
if Null2!=0 then fseek f (Pos2-3) #seek_set 	
if Null2==0 then (
fseek f 0x0 #seek_cur	
Pos3=ftell f
Null3=readbyte f
if Null3!=0 then fseek f (Pos3-3) #seek_set 	
if Null3==0 then fseek f 0x0 #seek_cur			
)		
)	
)		
	
)
)

Print ("Model End @ 0x"+((bit.intAsHex(ftell f))as string))	
	
msh = mesh vertices:vertArray faces:faceArray
msh.name = (MeshName+i as string) 
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 BoneMapArray.count do
(
	maxbone = BNArr[(BoneMapArray[i])]
	if i != BoneMapArray.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
)	
)	
)
fclose f