Applying MultiTexture through Maxscript

Hello, i'm kind of new on Maxscript and i get really stuck when material comes to the issue.

I'm scripting a format importer to study 3D formats and i get really stuck when the mesh has multi materials.

I get from the file the number of textures and which textures does it use and how many faces does each map use

For example I get from the script: First Texture uses 128 Faces, and Second texture uses 253 Faces.

Although i know how to apply a single texture to a whole mesh, i have no clue how to apply it to the number of faces i'm getting

This is my Script:

fTEST = getOpenFileName caption:"Choose Model File:" \
types:"Test Models (*.test)|*.test|"
 
fopen = fTEST "rb"
 
                readlong f
		readfloat f
		ExtraUVs = readlong f -- Flag, if it's not 0 there are multitextures.
		--print "Extra UV's:"
		--print ExtraUvs
 
		NumTex = readlong f --Number of Textures (should be 1 if ExtraUVs == 0)
		--print "Number of Textures:"
		--print NumTex
 
		Texture_array=#()
		Bump_array=#()
		FaceStart_array=#()
		FaceCount_array=#()
 
                -- The following Loop is to get the textures in an array, The stringsize of the texture is 1024, each texture block has 2048, The first string is the bitmap, and the second is the bumpmap, but i'm not using bumpmap right know so although i read it i don't do anything with it. after the 2048 bytes there are two long bytes that tells me where does the face start and how many faces does it use from that point, for example, it starts in the number of face 12 and it uses other 64 faces from that point.               
 
		for i = 1 to NumTex do
		(
			tex = readstring f
			print tex
			fseek f -1 #seek_cur
			for i = 1 to (1024 - tex.count) do readbyte f
			append Texture_Array tex
 
			tex = readstring f
			print tex
			fseek f -1 #seek_cur
			for i = 1 to (1024 - tex.count) do readbyte f
			append Bump_Array tex
 
			Face_Start = readlong f
			print Face_Start
			append FaceStart_array Face_Start
			Face_Count = readlong f
			print Face_Count
			append FaceCount_array Face_Count
		)
 
		Vert_array=#()
		Face_array=#()
		Normal_array=#()
		UV_array=#()
		Tg_array=#()
		EUV_array=#()
 
                -- From now on i'm just reading the vertex, faces and such
 
		Num_Vertex = readlong f
		--print "Number of Vertex:"
		--print Num_Vertex
 
		for i = 1 to Num_Vertex do
		(
			vx = readfloat f
			vy = readfloat f
			vz = readfloat f
			append Vert_array[vx,vy,vz]
			--print Vert_array
		)
 
		Num_Faces = readlong f
		--print "Faces:"
		--print Num_Faces
 
		for i = 1 to  Num_Faces do
		(
			fa = readshort f #unsigned
			fb = readshort f #unsigned
			fc = readshort f #unsigned
			append Face_array[fa+1,fb+1,fc+1]
			--print Face_array
		)
 
		Num_Normals = readlong f #unsigned
		--print "Normals:"
		--print Num_Normals
 
		for i = 1 to Num_Normals do
		(
			nx = readfloat f
			ny = readfloat f
			nz = readfloat f
			append Normal_array[nx,ny,nz]
			--print Normal_array
		)
 
 
 
		Num_UV = readlong f #unsigned
		--Print "Number of UV's:"
		--print Num_UV
 
		for i = 1 to Num_UV do
		(
			tu = readfloat f
			tv = readfloat f
			append UV_array[tu,1-tv,0]
			--print UV_array
		)
 
		--Uv's of each face
		for i = 1 to ExtraUVs do
		(	
			for i = 1 to Num_UV do
			(
				tu = readfloat f
				tv = readfloat f
				append EUV_array[tu,1-tv,0]
				--print UV_array
			)
		)
 
		Num_Tangent = readlong f
		--print "Number of Tangents:"
		--print Num_Tangent
 
		for i = 1 to Num_Tangent do
		(
			tax = readfloat f
			tay = readfloat f
			taz = readfloat f
			append Tg_array[tax,tay,taz]
			--print Tg_array
		)
 
                --Here i create the mesh
 
		msh = mesh vertices:Vert_array faces:Face_array name:(ModelName)
 
		msh.numTVerts = UV_array.count
		buildTVFaces msh
 
                -- This is for only one texture, and i'm not very clear how to do it for 2 or more.
 
		for j = 1 to UV_array.count do setTVert msh j UV_array[j]
		for j = 1 to Face_array.count do setTVFace msh j Face_array[j]
 
--Since fTEST has the full path of the model i remove the path string until i encounter a \, and then i apply the texture string i got from the array, this is in order to apply a texture from the same folder.
 
		mappath = (substring fSCN 1 (fTEST.count - 4))
 
		while mappath[mappath.count] != "\\" do 
		(
			mappath = substring mappath 1 (mappath.count - 1)
		)
 
 
		if NumTex > 1 then
		(
			map = MultiMaterial name:(ModelName+"_Map") numsubs:NumTex
			for v=1 to NumTex do
			(
				local MatDiffuse = substring Texture_Array[v] 1 (Texture_Array[v].count - 4)
				local MatBump = substring Bump_Array[v] 1 (Bump_Array[v].count - 4)
				map[v].diffuseMap = bitmaptexture name: MatDiffuse
				map[v].diffuseMap.filename = mappath + MatDiffuse + ".dds"
 
				if((findstring ModelName "nocull") != undefined) then
						map[v].twoSided = true
 
				if((findstring ModelName "alphablend") != undefined) then
					(
						map[v].opacityMap = bitmaptexture name:("Opacity_"+MatDiffuse)
						map[v].opacityMap.filename = map[v].diffuseMap.filename
						if((findstring ModelName "alphablend1") != undefined) then
							map[v].opacityMap.monoOutput = 1
					)
 
					showtexturemap map[v] map[v].diffusemap true
					map[v].showInViewport = on
			)
			msh.material = map
		)
		else
		(
			MatDiffuse = substring Texture_Array[1] 1 (Texture_Array[1].count - 4)
			MatBump = substring Bump_Array[1] 1 (Bump_Array[1].count - 4)
			map = StandardMaterial name: (ModelName+"_Map")
 
			map.diffuseMap = bitmaptexture name: (ModelName + "Diffuse")
			map.diffuseMap.filename = mappath + MatDiffuse + ".dds"
 
			map.bumpMap = bitmaptexture name: (ModelName + "Bump")
			map.bumpMap.filename = mappath + MatBump + ".dds"
 
			if((findstring ModelName "nocull") != undefined) then map.twoSided = true
 
			if((findstring ModelName "alphablend") != undefined) then
					(
						map.opacityMap = bitmaptexture name:("Opacity_"+ModelName)
						map.opacityMap.filename = map.diffuseMap.filename
						if((findstring ModelName "alphablend1") != undefined) then
						map.opacityMap.monoOutput = 1
					)
 
			showtexturemap map map.diffusemap true
			map.showInViewport = on
			msh.material = map

I know the code is such a mess but i'm a bit bad at it, and although i see the 3ds documentation i'm having a hard time to get it right >_<

If someone can get me some tips for improving it and trying to apply multimaterial it would be great to know

Thanks in advance

-Shayden