if (heapSize < 20000000) then
	heapSize = 200000000
gname = getOpenFileName \
caption:"Open .fbx from Bones folder" \
types:"Warframe_Bones (*.fbx)|*.fbx" \
historyCategory:"WarframeObjectPresets"
g = fopen gname "rb"
fname = getOpenFileName \
caption:"Open .fbx from Mesh folder" \
types:"Warframe (*.fbx)|*.fbx" \
historyCategory:"WarframeObjectPresets"
f = fopen fname "rb"


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
)	

clearlistener()

struct weight_data
(
   boneids,weights
)
struct mesh_info_struct
(
meshname,FacePos,FaceCount
)
struct Bone_Info_Struct
(
	Bone1,Bone2,Bone3,Bone4
)
struct Weight_Info_Struct
(
	Weight1,Weight2,Weight3,Weight4
)
struct BoneMap_Struct
(
BoneMapId
)
clearlistener()

fseek g 0x10 #seek_set
skelCount = readlong g
modelPathS = readlong g
modelPath = ReadFixedString g modelPathS
materialS = readlong g
materialname = ReadFixedString g materialS
fseek g 0x6f #seek_cur
BoneCount = readlong g
BoneNamesArray = #()
for x = 1 to BoneCount do (
BoneNameSize = readlong g	
BoneName = ReadFixedString g BoneNameSize	
append BoneNamesArray BoneName
)	
TotalFaces = readlong g	
UsedBones = readlong g
VertexCount = readlong g	
NumMorph = readlong g	
fseek g 0x4 #seek_cur
parse = readlong g
if parse!=2 then fseek g 0x3D #seek_cur
if parse==2 then fseek g 0x5D #seek_cur	
BoneCount1 = readlong g
BoneNamesArray1 = #()
boneparent_array = #()
for y = 1 to BoneCount1 do (
BoneNameSize1 = readlong g	
BoneName1 = ReadFixedString g BoneNameSize1
append BoneNamesArray1 BoneName1
boneparent=readshort g
boneId=readshort g
append boneparent_array boneparent
)	
fseek g 0xA2 #seek_cur
MeshCount=readlong g	
MeshInfoArray= #()
for a = 1 to MeshCount do (
fseek g 0x20 #seek_cur	
NameSize=readlong g
meshname = ReadFixedString g NameSize
FacePos=readlong g
Lod_1=readlong g
Lod_2=readlong g	
Lod_3=readlong g
Lod_4=readlong g	
FaceCount=readlong g	
Lod_1_Count=readlong g	
Lod_2_Count=readlong g
Lod_3_Count=readlong g	
Lod_4_Count=readlong g	
fseek g 0x34 #seek_cur
append MeshInfoArray (mesh_info_struct meshname:meshname FacePos:FacePos FaceCount:FaceCount)		
)
UsedBoneCount =#()
if NumMorph!=0 then 
for x = 1 to 1 do (	
MorphCount =readlong g	
for x = 1 to MorphCount do (
MorphNameSize = readlong g
MorphName = ReadFixedString g MorphNameSize		
null = readlong g	
)
)
if NumMorph==0 then fseek g 0x4 #seek_cur	
MeshParts=readlong g	
BoneMapStart = (ftell g)
for a = 1 to MeshParts do (
append UsedBoneCount (readlong g)	
for x = 1 to UsedBoneCount[a] do (
UsedBoneId=readlong g	
)	
)

VCount =#()	

if NumMorph!=0 then 
for i = 1 to 1 do (	
Mcount = readlong g	
fseek g 0x18 #seek_cur
for x = 1 to MeshParts do (
fseek g 0x30 #seek_cur	
longCount = readlong g
append VCount (readlong g)	
fseek g 0x10 #seek_cur		
Count=readlong g	
for x = 1 to Count do (	
NameSize = readlong g	
fseek g NameSize #seek_cur
)
fseek g 0x20 #seek_cur	
)
)

if NumMorph==0 then fseek g 0x0 #seek_cur

print VCount

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

BNArr1 = #()
for a = 1 to BoneCount1 do ( --second bones section
getPos = ftell f + 64		
fseek f getPos #seek_set	
)	
	

for x =1 to BoneCount1 do (
check = readshort f	
)	

for x =1 to BoneCount1 do (
check1 = readshort f	
)	

fseek f 0x8 #seek_cur

if NumMorph==0 then
for i = 1 to MeshCount do (	
	
vertArray=#()
faceArray =#()
UVarray=#()
W1_array=#() 
B1_array=#()
Weight_array=#()
UsedBoneCountArray =#()
	
fseek g BoneMapStart #seek_set	

num = readlong g	
for x = 1 to num do (
BoneMapId=(readlong g+1)	
append UsedBoneCountArray (BoneMap_Struct BoneMapId:BoneMapId)		
)	

BoneMapStart+=(num*4)+4
print UsedBoneCountArray	

vertOffset = (ftell f)

print vertOffset

NumVert=VertexCount	
	
for x =1 to NumVert do (	
vx = readFloat f
vy = readFloat f
vz = readFloat f	
p1 = readHalfFloat f		
p2 = readHalfFloat f
p3 = readHalfFloat f	
p4 = readHalfFloat f
p5 = readHalfFloat f
p6 = readHalfFloat f	
tu = readHalfFloat f	
tv = readHalfFloat f*-1
null = readlong f	
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	

append W1_array (Weight_Info_Struct Weight1:Weight1 Weight2:Weight2 Weight3:Weight3 Weight4:Weight4)	
append B1_array (Bone_Info_Struct Bone1:Bone1 Bone2:Bone2 Bone3:Bone3 Bone4:Bone4)
append vertArray ([vx,vy,vz]*100)	
append UVarray [tu,tv,0]		
)

num_compare=UsedBoneCount[i]

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

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

FaceStart = (ftell f)	
faceOffset = (MeshInfoArray[i].FacePos)*2
	
print num_compare
print NumVert	
print faceOffset
print FaceStart
	
fseek f (FaceStart+faceOffset)  #seek_set	
	
Print ("Last Read @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to (MeshInfoArray[i].FaceCount)/3 do (
fa=readshort f + 1
fb=readshort f + 1
fc=readshort f + 1
append faceArray [fa,fb,fc] 	
)	

Print ("Last Read @ 0x"+((bit.intAsHex(ftell f))as string))	
	
fseek f vertOffset  #seek_set	

try(		
msh = mesh vertices:vertArray faces:faceArray
msh.name=(MeshInfoArray[i].meshname)
msh.numTVerts = UVarray.count
buildTVFaces msh
for j = 1 to UVarray.count do setTVert msh j UVarray[j]
for j = 1 to faceArray.count do setTVFace msh j faceArray[j]


max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
for i = 1 to UsedBoneCount[i] do
(
	maxbone = getnodebyname BoneNamesArray[(UsedBoneCountArray[i].BoneMapId)]
	if i != UsedBoneCount[i] then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
modPanel.setCurrentObject skinMod
for i = 1 to NumVert 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
)
)catch(format "failed!!\n")
)

-----------------------------------------------------------------
vertOffset=(ftell f)
vertStart=(ftell f)
if NumMorph!=0 then	
for i = 1 to MeshCount do (	
	
vertArray=#()
faceArray =#()
UVarray=#()
W1_array=#() 
B1_array=#()
Weight_array=#()
UsedBoneCountArray =#()
	
fseek g BoneMapStart #seek_set	

num = readlong g	
for x = 1 to num do (
BoneMapId=(readlong g+1)	
append UsedBoneCountArray (BoneMap_Struct BoneMapId:BoneMapId)		
)	

BoneMapStart+=(num*4)+4
print UsedBoneCountArray	

print vertOffset

fseek f vertStart #seek_set		

NumVert=VCount[i]	
	
Print ("Last Read @ 0x"+((bit.intAsHex(ftell f))as string))	

for x =1 to NumVert do (	
vx = readFloat f
vy = readFloat f
vz = readFloat f	
p1 = readHalfFloat f		
p2 = readHalfFloat f
p3 = readHalfFloat f	
p4 = readHalfFloat f
p5 = readHalfFloat f
p6 = readHalfFloat f	
tu = readHalfFloat f	
tv = readHalfFloat f*-1
null = readlong f	
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	

append W1_array (Weight_Info_Struct Weight1:Weight1 Weight2:Weight2 Weight3:Weight3 Weight4:Weight4)	
append B1_array (Bone_Info_Struct Bone1:Bone1 Bone2:Bone2 Bone3:Bone3 Bone4:Bone4)
append vertArray ([vx,vy,vz]*100)	
append UVarray [tu,tv,0]		
)

num_compare=UsedBoneCount[i]

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

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


VertexEnd=vertOffset+(VertexCount*40)

fseek f VertexEnd #seek_set	

print VertexEnd
	
for x=1 to NumMorph do (
getPos = ftell f + 16		
fseek f getPos #seek_set		
)	

FaceStart = (ftell f)	
faceOffset = (MeshInfoArray[i].FacePos)*2
	
print num_compare
print NumVert	
print faceOffset
print FaceStart
	
fseek f (FaceStart+faceOffset)  #seek_set	
	
Print ("Last Read @ 0x"+((bit.intAsHex(ftell f))as string))	
	
for x = 1 to (MeshInfoArray[i].FaceCount)/3 do (
fa=readshort f + 1
fb=readshort f + 1
fc=readshort f + 1
append faceArray [fa,fb,fc] 	
)	

vertStart+=NumVert*40

try(		
msh = mesh vertices:vertArray faces:faceArray
msh.name=(MeshInfoArray[i].meshname)
msh.numTVerts = UVarray.count
buildTVFaces msh
for j = 1 to UVarray.count do setTVert msh j UVarray[j]
for j = 1 to faceArray.count do setTVFace msh j faceArray[j]


max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
for i = 1 to UsedBoneCount[i] do
(
	maxbone = getnodebyname BoneNamesArray[(UsedBoneCountArray[i].BoneMapId)]
	if i != UsedBoneCount[i] then 
		skinOps.addBone skinMod maxbone 0
	else
		skinOps.addBone skinMod maxbone 1
)
modPanel.setCurrentObject skinMod
for i = 1 to NumVert 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
)
)catch(format "failed!!\n")
)

fclose g
fclose f