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

clearlistener()
fn readHalfFloat fstream = (
    hf=readshort fstream #unsigned
    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_map_data
(
	MapId
)
clearlistener()

vert_info_array = #()
bone_info_array = #()
parent_info_array = #()
unk_info_array = #()
weight_info_array = #()
BNArr = #()
BoneMapArray	= #()
map_info_array = #()
ChildCountArr = #()
model_name_array = #()
model_name_array2 = #()
parentless_array = #()

clearlistener()

fseek g 0x8 #seek_set
DataCount = readlong g
DataSize = readlong g

for x = 1 to DataCount do (
getPos = ftell g + DataSize
Type = readbyte g
Type2 = readbyte g	
SecId = readshort g
if Type==0 then
(
Id=readlong g	
SecSize= readlong g
BoneOffset= readlong g
)		
if Type==1 then
(
Id=readlong g	
SecSize= readlong g
ParentOffset= readlong g
append parent_info_array ParentOffset		
)	
if Type==2 then
(
Unk= readlong g
UnkOffset= readlong g
Id=readlong g
Unk2=readlong g
)		
if Type==5 then
(
Unk= readlong g
WeightOffset= readlong g
Id=readlong g
Unk2=readlong g
)
if Type==11 then
(
Id=readlong g	
SecSize= readlong g
ModelNameOffset= readlong g
)	
if Type==21 then
(
Id=readlong g	
SecSize= readlong g
ParentlessOffset= readlong g
)	
if Type==22 then
(
Unk= readlong g
Offset= readlong g
Id=readlong g
Unk2=readlong g
)	
if Type==23 then
(
Unk= readlong g
MapOffset= readlong g
Id=readlong g
Unk2=readlong g
)	
fseek g getPos #seek_set	
if Type==0 then
(	
append bone_info_array BoneOffset	
)	
if Type==11 then
(	
append model_name_array ModelNameOffset
)	
if Type==21 then
(	
append parentless_array ParentlessOffset
)	
)

Print ("End @ 0x"+((bit.intAsHex(ftell g))as string))	
	
print bone_info_array	

fseek f 0x8 #seek_set
DataCount = readlong f
DataSize = readlong f

for x = 1 to DataCount do (
getPos = ftell f + DataSize
Type = readbyte f
Type2 = readbyte f
SecId = readshort f
if Type==0 then
(
Unk= readlong f
BoneOffset= readlong f
Id=readlong f
Unk2=readlong f
)		
if Type==1 then
(
Id=readlong f
SecSize= readlong f
ParentOffset= readlong f
append parent_info_array ParentOffset		
)	
if Type==2 then
(
Id=readlong f	
SecSize= readlong f
UnkOffset= readlong f
)		
if Type==5 then
(
Id=readlong f	
SecSize= readlong f
WeightOffset= readlong f
)
if Type==11 then
(
Id=readlong f	
SecSize= readlong f
ModelNameOffset2= readlong f
)	
if Type==21 then
(
Unk= readlong f
Offset= readlong f
Id=readlong f
Unk2=readlong f
)	
if Type==22 then
(
Id=readlong f	
SecSize= readlong f
Offset= readlong f
)	
if Type==23 then
(
Id=readlong f	
SecSize= readlong f
MapOffset= readlong f
)	
fseek f getPos #seek_set	
if Type==2 then
(	
append unk_info_array UnkOffset	
)
if Type==5 then
(	
append weight_info_array WeightOffset	
)	
if Type==11 then
(	
append model_name_array2 ModelNameOffset2
)	
if Type==22 then
(	
append vert_info_array Offset
)
if Type==23 then
(	
append map_info_array MapOffset
)	
)

print (bone_info_array as string)


Print ("Model Info Start @ 0x"+((bit.intAsHex(ftell g))as string))	

ModelInfo=model_name_array[1]

fseek g ModelInfo #seek_set	
		
ModelName=readString g 
	
Print ("Model Info End @ 0x"+((bit.intAsHex(ftell g))as string))	

BoneCount = #()		

if parent_info_array[1] != undefined then
(	

BoneOff=bone_info_array[1]
fseek g BoneOff #seek_set

Print ("Fake Start @ 0x"+((bit.intAsHex(ftell g))as string))

fseek g 0x20 #seek_cur
	
while ftell g < parent_info_array[1] do (	
Hash = (readlong g#unsigned) as string
fseek g 0x244 #seek_cur	
append BoneCount Hash	
)

print ("parent_info_array @"+((parent_info_array[1]) as string))

BoneParentArray = #()	
BoneOrderArray = #()	
	
ParentOff=parent_info_array[1]
fseek g ParentOff #seek_set

fseek g 0x20 #seek_cur
check=readlong g

Print ("Bone Parent Start @ 0x"+((bit.intAsHex(ftell g))as string))
	
if check==0 then (	
for i = 1 to BoneCount.count do (
BoneParent = readlong g
CCount = readlong g
HashId = (readlong g#unsigned) as string
fseek g 0x8C #seek_cur	
append BoneParentArray BoneParent	
append BoneOrderArray HashId	
)	
)
if check!=0 then fseek g 0x0 #seek_cur

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

BoneOff=bone_info_array[1]
fseek g BoneOff #seek_set

Print ("Bone Start @ 0x"+((bit.intAsHex(ftell g))as string))

fseek g 0x20 #seek_cur

HT = dotNetObject "System.Collections.Hashtable"	
	
for i = 1 to BoneCount.count do (
	
BoneHash = (readlong g#unsigned) as string
		
getPos = ftell g + 308
	
fseek g 0x104 #seek_cur	
	
m11=readfloat g ; m21=readfloat g ; m31=readfloat g; m41=readfloat g;
m12=readfloat g ; m22=readfloat g ; m32=readfloat g; m42=readfloat g;
m13=readfloat g ; m23=readfloat g ; m33=readfloat g; m43=readfloat g;
	
fseek g getPos #seek_set			
	
BoneName=readFixedString g 256
	
append BoneMapArray BoneName

FFFF=readlong g
BoneChild = readlong g	
childCount = readlong g
append ChildCountArr childCount	
NextChild = readlong g	

HT.Add (BoneHash) BoneName 
					
tfm = (matrix3 [m11,m12,m13] [m21,m22,m23] [m31,m32,m33] ([m41,m42,m43]*100))

if isvalidnode (getNodeByName BoneName) != true then (	

 	        newBone = bonesys.createbone	\
				  tfm.row4	\
				  (tfm.row4 + 0.01 * (normalize tfm.row1)) \
				  (normalize tfm.row3)
			newBone.name = BoneName
			newBone.width  = 0.01
			newBone.height = 0.01
			newBone.transform = tfm
			newBone.setBoneEnable false 0
			newBone.wirecolor = white
			newbone.showlinks = true
			newBone.pos.controller      = TCB_position ()
			newBone.rotation.controller = TCB_rotation ()	
			)					  
if isvalidnode (getNodeByName BoneName) == true then (
newBone = getNodeByName BoneName
)	
append BNArr newBone	
)
	
ParentBone = #()	

try (
if check==0 then (	
for i = 1 to BoneCount.count do (
MyBone = getnodebyname HT.item[BoneOrderArray[i]]
append ParentBone MyBone	
BNArr[i]=ParentBone[i]	
if (BoneParentArray[i] != -1) then (
ParentBone[i].parent = ParentBone[(BoneParentArray[i] + 1)]	
)	
)	
)
)catch(format "Failed!!!\n")

if check!=0 then fseek g 0x0 #seek_cur


Print ("Bone End @ 0x"+((bit.intAsHex(ftell g))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,-1,0] [0,0,1] [0,0,0])	
)

if parent_info_array[1]== undefined then 
(	
BoneOff=bone_info_array[1]
fseek g BoneOff #seek_set

Print ("Bone Start @ 0x"+((bit.intAsHex(ftell g))as string))

fseek g 0x20 #seek_cur
	
while ftell g < parentless_array[1] do (	
	
BoneHash = (readlong g#unsigned) as string
		
getPos = ftell g + 308
	
fseek g 0x104 #seek_cur	
	
m11=readfloat g ; m21=readfloat g ; m31=readfloat g; m41=readfloat g;
m12=readfloat g ; m22=readfloat g ; m32=readfloat g; m42=readfloat g;
m13=readfloat g ; m23=readfloat g ; m33=readfloat g; m43=readfloat g;
	
fseek g getPos #seek_set			
	
BoneName=readFixedString g 256
	
append BoneMapArray BoneName

FFFF=readlong g
BoneChild = readlong g	
childCount = readlong g
append ChildCountArr childCount	
NextChild = readlong g		
					
tfm = (matrix3 [m11,m12,m13] [m21,m22,m23] [m31,m32,m33] ([m41,m42,m43]*100))

if isvalidnode (getNodeByName BoneName) != true then (	

 	        newBone = bonesys.createbone	\
				  tfm.row4	\
				  (tfm.row4 + 0.01 * (normalize tfm.row1)) \
				  (normalize tfm.row3)
			newBone.name = BoneName
			newBone.width  = 0.01
			newBone.height = 0.01
			newBone.transform = tfm
			newBone.setBoneEnable false 0
			newBone.wirecolor = white
			newbone.showlinks = true
			newBone.pos.controller      = TCB_position ()
			newBone.rotation.controller = TCB_rotation ()	
			)					  
if isvalidnode (getNodeByName BoneName) == true then (
newBone = getNodeByName BoneName
)	
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,-1,0] [0,0,1] [0,0,0])	
)

fclose g

MapOff=map_info_array[1]
fseek f MapOff #seek_set

FaceCountArray = #()
FacePosArray = #()
VertexCountArray = #()
VertexEnd = #()
ModelOrderArray = #()

Unk=readlong f
DataCount = readlong f
fseek f 8 #seek_cur 	

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

for x = 1 to DataCount do 
(
FacePos=readlong f
FaceCount = readlong f
VPos = readlong f	
VertexCount = readlong f	
ModelNumber =  readlong f	
fseek f 0x10 #seek_cur 
Print ("Vertex Count: "+((VertexCount)as string))	
Print ("Face Count: "+((FaceCount)as string))
append ModelOrderArray ModelNumber	
append VertexEnd VPos
append FacePosArray FacePos	
append FaceCountArray FaceCount	
append VertexCountArray VertexCount		
)		

print VertexEnd
print FacePosArray

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

BoneMapStart = ftell f
UBCountExtended	=#()
	
for i = 1 to DataCount do (	
getPos = ftell f + 260	
UsedBCount = (readlong f+1)
append UBCountExtended UsedBCount
fseek f getPos #seek_set	
)	
	
UBoneCount =#()
ChunkOneType =#()
SecSize	=#()
	
for i = 1 to vert_info_array.count do (	
COff=vert_info_array[i]
fseek f COff #seek_set
		
Unk=readlong f
ChunkType = readlong f
Count = readlong f	
Size = readlong f
fseek f 0x8 #seek_cur	
append ChunkOneType ChunkType
append SecSize Count
)		
	
Print ("ChunkOneType:"+(ChunkOneType as string))

if DataCount>1 then (
Stride=(VertexEnd[2]-VertexCountArray[1])	
)	
print Stride	

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

ModelInfo2=model_name_array2[1]

fseek f ModelInfo2 #seek_set	

for i=1 to 1 do (	
getPos = ftell f + 132			
ModelName2=readString f 
fseek f getPos #seek_set		
)
	
StrideX=readfloat f
StrideY=readfloat f
StrideZ=readfloat f
	
Print ("Model Info2 End @ 0x"+((bit.intAsHex(ftell f))as string))	

for i = 1 to DataCount do (	
	
vertArray = #()
Facearray = #()	
UV_array = #()	
Weight_array =#()
BoneMapId = #()	
	
fseek f BoneMapStart #seek_set
	
Print ("Bone Map Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for i = 1 to 1 do (	
getPos = ftell f + 260	
UsedBoneCount = (readlong f)
append UBoneCount	UsedBoneCount	
for i = 1 to UsedBoneCount do (
MapId=(ReadShort f #unsigned + 1)
if MapId>BNArr.count then 
(
MapId=0
)	
if MapId!=0 then (
append BoneMapId MapId
)	
)	
fseek f getPos #seek_set	
)

BoneMapStart+=260

fseek f (vert_info_array[1]-24) #seek_set

BBMinX=readfloat f
BBMinY=readfloat f
BBMinZ=readfloat f

BBMaxX=readfloat f
BBMaxY=readfloat f
BBMaxZ=readfloat f

fseek f (vert_info_array[1]+12) #seek_set

VertDataSize=readlong f

fseek f (vert_info_array[1]+24) #seek_set
		
if Stride==1 then  VertexCountArray[i]=(VertexCountArray[i]+1)	

if Stride!=1 then  VertexCountArray[i]=(VertexCountArray[i]+0)	

if DataCount==1 then VertexCountArray[i]=SecSize[1]	

print VertexCountArray[i]

if ChunkOneType[1]==0 then (
	
Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))		
	
VertOff=ftell f
if VertDataSize==8 then (	
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set

for x = 1 to (VertexCountArray[i]) do (
vx = readHalfFloat f
vy = readHalfFloat f
vz = readHalfFloat f
Null = readHalfFloat f		
append vertArray ([-vx,-vy,vz]*100)
)
Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))	
)	

if VertDataSize==12 then (	
fseek f (VertOff+(VertexEnd[i]*12)) #seek_set

for x = 1 to (VertexCountArray[i]) do (
vx = readFloat f
vy = readFloat f
vz = readFloat f	
append vertArray ([-vx,-vy,vz]*100)
)
Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))	
)	
)

if ChunkOneType[1]==15 then (
	
Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))		
	
VertOff=ftell f
if VertDataSize==8 then (	
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set

for x = 1 to (VertexCountArray[i]) do (
vx = readHalfFloat f
vy = readHalfFloat f
vz = readHalfFloat f
Null = readHalfFloat f		
append vertArray ([-vx,-vy,vz]*100)
)
Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))	
)	

if VertDataSize==12 then (	
fseek f (VertOff+(VertexEnd[i]*12)) #seek_set

for x = 1 to (VertexCountArray[i]) do (
vx = readFloat f
vy = readFloat f
vz = readFloat f	
append vertArray ([-vx,-vy,vz]*100)
)
Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))	
)

if VertDataSize==16 then (	
fseek f (VertOff+(VertexEnd[i]*16)) #seek_set

for x = 1 to (VertexCountArray[i]) do (
vx = readHalfFloat f
vy = readHalfFloat f
vz = readHalfFloat f
Null = readHalfFloat f		
FFFF=readlong f	
tu = readHalfFloat f
tv = readHalfFloat f*-1	
append vertArray ([-vx,-vy,vz]*100)
append UV_array [tu,tv,0]	 		
)
Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))		
)
)

if ChunkOneType[1]==1 do (	
for x = 1 to VertexCountArray[i] do 
(	
nx = readFloat f
ny = readFloat f
nz = readFloat f
)	
vert_info_array[1]+=(VertexCountArray[i]*12)
)

if ChunkOneType[1]==2 do (
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set		
	
for x = 1 to VertexCountArray[i] do (	
tu = readFloat f
tv = readFloat f*-1
append UV_array [tu,tv,0]	 	
)
)

if ChunkOneType[1]==3 do 	(
for x = 1 to VertexCountArray[i] do (
tx = readFloat f
ty = readFloat f
tz = readFloat f
tw = readFloat f	
)	
vert_info_array[1]+=(VertexCountArray[i]*16)
)
	
if ChunkOneType[1]==5 then (	
	
FaceOff=ftell f
fseek f (FaceOff+(FacePosArray[i]*2)) #seek_set

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

for x = 1 to (FaceCountArray[i]/3) do 
(	
f1 = (Readshort f #unsigned + 1) - VertexEnd[i]
f2 = (Readshort f #unsigned + 1) - VertexEnd[i]
f3 = (Readshort f #unsigned + 1) - VertexEnd[i]	
append Facearray [f1,f2,f3] 	
)
Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))	
)

if ChunkOneType[1]==6 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 16
fseek f getPos #seek_set
)	
vert_info_array[1]+=(VertexCountArray[i]*16)
)

if ChunkOneType[1]==8 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 28
fseek f getPos #seek_set
)	
vert_info_array[1]+=(VertexCountArray[i]*28)
)

if ChunkOneType[1]==9 do 	(
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set			
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to VertexCountArray[i] do 
(
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
		
weight1 = readbyte f #unsigned	
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 = 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.0)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.0)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.0)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.0)
      )      
   )
append Weight_array w		
)
)

fseek f vert_info_array[2] #seek_set
	
fseek f 0x4 #seek_cur
CType = (readlong f)
Count = readlong f	
Size = readlong f
fseek f 0x8 #seek_cur	


if ChunkOneType[2]==0 then (
for x = 1 to (VertexCountArray[i]) do (
vx = readFloat f
vy = readFloat f
vz = readFloat f	
append vertArray ([-vx,-vy,vz]*100)
)
vert_info_array[2]+=((VertexCountArray[i])*12)
)

if ChunkOneType[2]==1 do (	
for x = 1 to VertexCountArray[i] do 
(	
nx = readFloat f
ny = readFloat f
nz = readFloat f
)	
vert_info_array[2]+=(VertexCountArray[i]*12)
)

if ChunkOneType[2]==2 do (
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set		
	
for x = 1 to VertexCountArray[i] do (	
tu = readFloat f
tv = readFloat f*-1
append UV_array [tu,tv,0]	 	
)
)

if ChunkOneType[2]==3 do 	(
for x = 1 to VertexCountArray[i] do (
tx = readFloat f
ty = readFloat f
tz = readFloat f
tw = readFloat f	
)	
vert_info_array[2]+=(VertexCountArray[i]*16)
)
	
if ChunkOneType[2]==5 then (	
	
FaceOff=ftell f
fseek f (FaceOff+(FacePosArray[i]*2)) #seek_set

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

for x = 1 to (FaceCountArray[i]/3) do 
(	
f1 = (Readshort f #unsigned + 1) - VertexEnd[i]
f2 = (Readshort f #unsigned + 1) - VertexEnd[i]
f3 = (Readshort f #unsigned + 1) - VertexEnd[i]	
append Facearray [f1,f2,f3] 	
)
Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))	
)

if ChunkOneType[2]==6 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 16
fseek f getPos #seek_set
)	
vert_info_array[2]+=(VertexCountArray[i]*16)
)

if ChunkOneType[2]==8 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 28
fseek f getPos #seek_set
)	
vert_info_array[2]+=(VertexCountArray[i]*28)
)

if ChunkOneType[2]==9 do 	(
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set			
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to VertexCountArray[i] do 
(
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
		
weight1 = readbyte f #unsigned	
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 = 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.0)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.0)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.0)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.0)
      )      
   )
append Weight_array w		
)
)

fseek f vert_info_array[3] #seek_set
	
fseek f 0x4 #seek_cur
CType = (readlong f)
Count = readlong f	
Size = readlong f
fseek f 0x8 #seek_cur	


if ChunkOneType[3]==0 then (
for x = 1 to (VertexCountArray[i]) do (
vx = readFloat f
vy = readFloat f
vz = readFloat f	
append vertArray ([-vx,-vy,vz]*100)
)
vert_info_array[3]+=((VertexCountArray[i])*12)
)

if ChunkOneType[3]==1 do (	
for x = 1 to VertexCountArray[i] do 
(	
nx = readFloat f
ny = readFloat f
nz = readFloat f
)	
vert_info_array[8]+=(VertexCountArray[i]*12)
)

if ChunkOneType[3]==2 do (
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set		
	
for x = 1 to VertexCountArray[i] do (	
tu = readFloat f
tv = readFloat f*-1
append UV_array [tu,tv,0]	 	
)
)

if ChunkOneType[3]==3 do 	(
for x = 1 to VertexCountArray[i] do (
tx = readFloat f
ty = readFloat f
tz = readFloat f
tw = readFloat f	
)	
vert_info_array[8]+=(VertexCountArray[i]*16)
)
	
if ChunkOneType[3]==5 then (	
	
FaceOff=ftell f
fseek f (FaceOff+(FacePosArray[i]*2)) #seek_set

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

for x = 1 to (FaceCountArray[i]/3) do 
(	
f1 = (Readshort f #unsigned + 1) - VertexEnd[i]
f2 = (Readshort f #unsigned + 1) - VertexEnd[i]
f3 = (Readshort f #unsigned + 1) - VertexEnd[i]	
append Facearray [f1,f2,f3] 	
)
Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))	
)

if ChunkOneType[3]==6 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 16
fseek f getPos #seek_set
)	
vert_info_array[3]+=(VertexCountArray[i]*16)
)

if ChunkOneType[3]==8 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 28
fseek f getPos #seek_set
)	
vert_info_array[3]+=(VertexCountArray[i]*28)
)

if ChunkOneType[3]==9 do 	(
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set			
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to VertexCountArray[i] do 
(
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
		
weight1 = readbyte f #unsigned	
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 = 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.0)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.0)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.0)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.0)
      )      
   )
append Weight_array w		
)
)

fseek f vert_info_array[4] #seek_set
	
fseek f 0x4 #seek_cur
CType = (readlong f)
Count = readlong f	
Size = readlong f
fseek f 0x8 #seek_cur	


if ChunkOneType[4]==0 then (
for x = 1 to (VertexCountArray[i]) do (
vx = readFloat f
vy = readFloat f
vz = readFloat f	
append vertArray ([-vx,-vy,vz]*100)
)
vert_info_array[4]+=((VertexCountArray[i])*12)
)

if ChunkOneType[4]==1 do (	
for x = 1 to VertexCountArray[i] do 
(	
nx = readFloat f
ny = readFloat f
nz = readFloat f
)	
vert_info_array[4]+=(VertexCountArray[i]*12)
)

if ChunkOneType[4]==2 do (
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set		
	
for x = 1 to VertexCountArray[i] do (	
tu = readFloat f
tv = readFloat f*-1
append UV_array [tu,tv,0]	 	
)
)

if ChunkOneType[4]==3 do 	(
for x = 1 to VertexCountArray[i] do (
tx = readFloat f
ty = readFloat f
tz = readFloat f
tw = readFloat f	
)	
vert_info_array[4]+=(VertexCountArray[i]*16)
)
	
if ChunkOneType[4]==5 then (	
	
FaceOff=ftell f
fseek f (FaceOff+(FacePosArray[i]*2)) #seek_set

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

for x = 1 to (FaceCountArray[i]/3) do 
(	
f1 = (Readshort f #unsigned + 1) - VertexEnd[i]
f2 = (Readshort f #unsigned + 1) - VertexEnd[i]
f3 = (Readshort f #unsigned + 1) - VertexEnd[i]	
append Facearray [f1,f2,f3] 	
)
Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))	
)

if ChunkOneType[4]==6 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 16
fseek f getPos #seek_set
)	
vert_info_array[4]+=(VertexCountArray[i]*16)
)

if ChunkOneType[4]==8 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 28
fseek f getPos #seek_set
)	
vert_info_array[4]+=(VertexCountArray[i]*28)
)

if ChunkOneType[4]==9 do 	(
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set			
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to VertexCountArray[i] do 
(
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
		
weight1 = readbyte f #unsigned	
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 = 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.0)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.0)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.0)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.0)
      )      
   )
append Weight_array w		
)
)

-----------------------------------------------------

if vert_info_array[5]!=undefined do (

fseek f vert_info_array[5] #seek_set
	
fseek f 0x4 #seek_cur
CType = (readlong f)
Count = readlong f	
Size = readlong f
fseek f 0x8 #seek_cur	


if ChunkOneType[5]==0 then (
for x = 1 to (VertexCountArray[i]) do (
vx = readFloat f
vy = readFloat f
vz = readFloat f	
append vertArray ([-vx,-vy,vz]*100)
)
vert_info_array[5]+=((VertexCountArray[i])*12)
)

if ChunkOneType[5]==1 do (	
for x = 1 to VertexCountArray[i] do 
(	
nx = readFloat f
ny = readFloat f
nz = readFloat f
)	
vert_info_array[5]+=(VertexCountArray[i]*12)
)

if ChunkOneType[5]==2 do (

VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set		
	
for x = 1 to VertexCountArray[i] do (	
tu = readFloat f
tv = readFloat f*-1
append UV_array [tu,tv,0]	 	
)
)

if ChunkOneType[5]==3 do 	(
for x = 1 to VertexCountArray[i] do (
tx = readFloat f
ty = readFloat f
tz = readFloat f
tw = readFloat f	
)	
vert_info_array[5]+=(VertexCountArray[i]*16)
)
	
if ChunkOneType[5]==5 then (	
	
FaceOff=ftell f
fseek f (FaceOff+(FacePosArray[i]*2)) #seek_set

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

for x = 1 to (FaceCountArray[i]/3) do 
(	
f1 = (Readshort f #unsigned + 1) - VertexEnd[i]
f2 = (Readshort f #unsigned + 1) - VertexEnd[i]
f3 = (Readshort f #unsigned + 1) - VertexEnd[i]	
append Facearray [f1,f2,f3] 	
)
Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))	
)

if ChunkOneType[5]==6 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 16
fseek f getPos #seek_set
)	
vert_info_array[5]+=(VertexCountArray[i]*16)
)

if ChunkOneType[5]==8 do (	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 28
fseek f getPos #seek_set
)	
vert_info_array[5]+=(VertexCountArray[i]*28)
)

if ChunkOneType[5]==9 do 	(
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set			
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	
		
for x = 1 to VertexCountArray[i] do 
(
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
		
weight1 = readbyte f #unsigned	
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 = 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.0)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.0)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.0)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.0)
      )      
   )
append Weight_array w		
)
)
)

if vert_info_array[6]!=undefined do (
	
fseek f vert_info_array[6] #seek_set
	
fseek f 0x4 #seek_cur
CType = (readlong f)
Count = readlong f	
Size = readlong f
fseek f 0x8 #seek_cur	

if ChunkOneType[6]==0 then (
	
Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))		
	
VertOff=ftell f
if VertDataSize==8 then (	
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set

for x = 1 to (VertexCountArray[i]) do (
vx = readHalfFloat f
vy = readHalfFloat f
vz = readHalfFloat f	
Null = readHalfFloat f		
append vertArray ([-vx,-vy,vz]*100)
)
Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))	
)	

if VertDataSize==12 then (	
fseek f (VertOff+(VertexEnd[i]*12)) #seek_set

for x = 1 to (VertexCountArray[i]) do (
vx = readFloat f
vy = readFloat f
vz = readFloat f		
append vertArray ([-vx,-vy,vz]*100)
)
Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))	
)	
)

if ChunkOneType[6]==1 do (	
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*12)) #seek_set		
	
for x = 1 to VertexCountArray[i] do 
(	
nx = readFloat f
ny = readFloat f
nz = readFloat f
)	
)

if ChunkOneType[6]==2 do (

VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set		
	
for x = 1 to VertexCountArray[i] do (	
tu = readFloat f
tv = readFloat f*-1
append UV_array [tu,tv,0]	 	
)
)

if ChunkOneType[6]==3 do 	(
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*16)) #seek_set		
	
for x = 1 to VertexCountArray[i] do (
tx = readFloat f
ty = readFloat f
tz = readFloat f
tw = readFloat f	
)	
)
	
if ChunkOneType[6]==5 then (	
	
FaceOff=ftell f
fseek f (FaceOff+(FacePosArray[i]*2)) #seek_set

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

for x = 1 to (FaceCountArray[i]/3) do 
(	
f1 = (Readshort f #unsigned + 1) - VertexEnd[i]
f2 = (Readshort f #unsigned + 1) - VertexEnd[i]
f3 = (Readshort f #unsigned + 1) - VertexEnd[i]	
append Facearray [f1,f2,f3] 	
)
Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))	
)

if ChunkOneType[6]==6 do (	
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*16)) #seek_set		
	
for x = 1 to VertexCountArray[i] do 
(
getPos = ftell f + 16
fseek f getPos #seek_set
)	
)

if ChunkOneType[6]==8 do (	
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*28)) #seek_set		
	
for x = 1 to VertexCountArray[i] do (
getPos = ftell f + 28
fseek f getPos #seek_set
)	
)

if ChunkOneType[6]==9 do 	(
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set			
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to VertexCountArray[i] do 
(
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
		
weight1 = readbyte f #unsigned	
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 = 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.0)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.0)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.0)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.0)
      )      
   )
append Weight_array w		
)
)
)

if vert_info_array[7]!=undefined do (
	
fseek f vert_info_array[7] #seek_set
	
fseek f 0x4 #seek_cur
CType = (readlong f)
Count = readlong f	
Size = readlong f
fseek f 0x8 #seek_cur	

if ChunkOneType[7]==8 do (	
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*28)) #seek_set		
	
for x = 1 to VertexCountArray[i] do (
getPos = ftell f + 28
fseek f getPos #seek_set
)	
)

if ChunkOneType[7]==9 do 	(
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set		
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to VertexCountArray[i] do 
(
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
		
weight1 = readbyte f #unsigned	
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 = 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.0)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.0)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.0)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.0)
      )      
   )
append Weight_array w		
)
)
)

if vert_info_array[8]!=undefined do (

fseek f vert_info_array[8] #seek_set
	
fseek f 0x4 #seek_cur
CType = (readlong f)
Count = readlong f	
Size = readlong f
fseek f 0x8 #seek_cur	

if ChunkOneType[8]==8 do (	
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*28)) #seek_set	
	
for x = 1 to VertexCountArray[i] do (
getPos = ftell f + 28
fseek f getPos #seek_set
)	
)

if ChunkOneType[8]==9 do 	(
	
VertOff=ftell f
fseek f (VertOff+(VertexEnd[i]*8)) #seek_set	
	
Print ("Weights Start @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to VertexCountArray[i] do 
(
	
bone1 = readbyte f #unsigned	
bone2 = readbyte f #unsigned	
bone3 = readbyte f #unsigned	
bone4 = readbyte f #unsigned	
		
weight1 = readbyte f #unsigned	
weight2 = readbyte f #unsigned
weight3 = readbyte f #unsigned
weight4 = 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.0)
      )
      if(weight2 != 0) then (
         w2 = weight2 as float
         append w.boneids (bone2 + 1)
         append w.weights (w2/255.0)
      )
      if(weight3 != 0) then (
         w3 = weight3 as float
         append w.boneids (bone3 + 1)
         append w.weights (w3/255.0)
      )
      if(weight4 != 0) then (
         w4 = weight4 as float
         append w.boneids (bone4 + 1)
        append w.weights (w4/255.0)
      )      
   )
append Weight_array w		
)
)
)

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

msh = mesh vertices:vertArray faces:Facearray
msh.name = (ModelName+"_"+ 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]

move msh [-StrideX,-StrideY,StrideZ]

max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
for i = 1 to BoneMapId.count do
(
	maxbone = getnodebyname BoneMapArray[(BoneMapId[i])]
	if i != BoneMapId.count then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
modPanel.setCurrentObject skinMod
for i = 1 to VertexCountArray[i] 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
)	
)

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

fclose f