Export By Material?

I'm using maxscript to dump information in my scene. and just followed the examples given in the maxscript helpme.

But I haven't been able to find any examples on how to export by material...

see my mesh could have multiple materials, and examples just dumps the verts, texturecords, faces etc as one HUGE dump..

I looked through the read me and theres a thing about MatIDs.. but so some reason my MatIDs are weird.. like one model has like matID1, MatID4... but it only has one material...

does anyone know of an example of exporting by material?

edit
I guess the mat ID can be anything, and only really works with multimaterial. otherwise if the model has no material or a standard material. I can export the entire mesh,matIDs don't matter..

here's the script I was working on... I'm trying to make a PMD exporter to make models for MikuMikuDance

clearlistener()
 
obj= $*
 
modelArray=#()
struct model (
vertX,
vertY,
vertZ,
normX,
normY,
normZ,
tvertU,
tvertV,
boneA,
boneB,
weight
)
 
matArray=#()
struct materials (
diffR,
diffG,
diffB,
opac,
gloss,
spec,
specR,
specG,
specB,
ambR,
ambG,
ambB,
outline,
toon,
diffTex,
refTex,
refType,
apexes
)
 
 
num_verts = 0
num_faces = 0
num_tex = 0
 
-- data collection, organize all nodes into seperate arrays
ModelBin=#() 
BoneBin=#()
FaceArray=#()
 
-- general arrays, used for data mangment
vertWeights=#() -- vertex [vertexID,Weight1]
skinBones=#() -- bones used in skin modifier
boneID=#() -- bones used in skin modifier, as ID relating to BoneBin
weightsTwo=#()
 
for a = 1 to obj.count do( -- Filters Out Nodes
 
if (classOf obj[a].baseobject) == BoneGeometry do append BoneBin obj[a]
 
 
if (classOf obj[a].baseobject) == Editable_Poly \
or (classOf obj[a].baseobject) == Editable_Mesh do(
for h = 1 to obj[a].modifiers.count do(
if obj[a].modifiers[h]==obj[a].modifiers[#Skin] \
AND skinOps.GetNumberBones obj[a].modifiers[#Skin]!=0 do append ModelBin obj[a]))
-- "Section 01: Name/Comments @ 0x3"
-- "Section 02: Model Data @ 0x11b"
-- "Section 03: Materials @ 0x530dd"
-- "Section 04: Bones @ 0x53587"
-- "Section 05: IK Chains @ 0x5449e"
-- "Section 06: Morphs @ 0x544d8"
-- "Section 07: Frames @ 0x6bfad"
-- "Section 08: English Names @ 0x6c222"
-- "Section 08b: Bone Names @ 0x6c337"
-- "Section 08c: Morph Names @ 0x6caf3"
-- "Section 08d: IK Chain Names @ 0x6ce3b"
-- "Section 08e: Toon Names @ 0x6cf99"
-- "Section 09: Collision Model @ 0x6d381"
-- "Section 10: Physics @ 0x6d9ae"
 
)
 
 
-- re-order data if need be
 
 
for ms = 1 to ModelBin.count do( --extracts weights, but they need to be formed to fit 2bone/weight
msh = ModelBin[ms]
select msh
max modify mode
-- get bone info
bones_total_count = skinops.getnumberbones msh.modifiers[#skin]
vertex_count = getNumverts msh --getnumvertices
 
-- loop through all mesh vertices in this mesh
for i = 1 to vertex_count do(
-- loop through all bones affecting this vertex
vertex_bone_count = skinOps.GetVertexWeightCount msh.modifiers[#skin] i
for bn = 1 to vertex_bone_count do(
weight = skinops.getvertexweight msh.modifiers[#skin] i bn
selectBone=skinOps.GetBoneName msh.modifiers[#skin] bn 0
append vertWeights[i,weight]
append skinBones selectBone
)
-- print "done"
))
 
for sc = 1 to skinBones.count do( -- converts the skin list to IDs, based on master bone list
for mt = 1 to BoneBin.count do(
if BoneBin[mt].name==skinBones[sc] do append boneID mt
))
 
for mh = 1 to ModelBin.count do( -- stores mesh data to array. ready for mesh export.
tmesh = snapshotAsMesh ModelBin[mh]	
num_verts = tmesh.numverts
num_faces = tmesh.numfaces
num_tex = tmesh.numtverts
 
 
for rd = 1 to num_verts do(
vert = getVert tmesh rd
norm = getNormal tmesh rd
texcoord = getTVert tmesh rd
nx=norm.x
ny=norm.z
nz=norm.y
vx=vert.x
vy=vert.z
vz=vert.y
tu=texcoord.x
tv=texcoord.y*-1
 
 
newWeights=#()
for y = 1 to vertWeights.count do(
if rd==vertWeights[y][1] do append newWeights[(boneID[y]),(vertWeights[y][2])]
)
print "g"
print newWeights.count
!REG3XP1!>
if newWeights.count==1 do(
b1=newWeights[1][1]
b2=0
w1=1.0
)
if newWeights.count==2 do(
b1=(newWeights[1][1])-1
b2=(newWeights[2][1])-1
w1=newWeights[1][2]
	print w1
)
if newWeights.count==3 do(
col=#()
 
if vertWeights[1][2]>=vertWeights[2][2] then(
if vertWeights[3][2]>=vertWeights[2][2] then(
append col[(vertWeights[3][1]),(vertWeights[3][2])])
else(append col[(vertWeights[2][1]),(vertWeights[2][2])])
append col[(vertWeights[1][1]),(vertWeights[1][2])])else(
if vertWeights[3][2]>=vertWeights[1][2] 
then(append col[(vertWeights[3][1]),(vertWeights[3][2])])
else(append col[(vertWeights[1][1]),(vertWeights[1][2])])
append col[(vertWeights[2][1]),(vertWeights[2][2])]
)	
 
b1=(col[1][1])-1
b2=(col[2][1])-1
w1=(((1-(col[1][2]+col[2][2]))/2)+col[1][2])
)
if newWeights.count==4 do(
col=#()
colB=#()
if vertWeights[1][2]>=vertWeights[2][2] then(
if vertWeights[3][2]>=vertWeights[2][2] then(
if vertWeights[4][2]>=vertWeights[3][2] 
then(append col[(vertWeights[4][1]),(vertWeights[4][2])])
else(append col[(vertWeights[3][1]),(vertWeights[3][2])])
append col[(vertWeights[3][1]),(vertWeights[3][2])]
)else(
if vertWeights[4][2]>=vertWeights[2][2] 
then(append col[(vertWeights[4][1]),(vertWeights[4][2])])
else(append col[(vertWeights[2][1]),(vertWeights[2][2])])
append col[(vertWeights[2][1]),(vertWeights[2][2])]
)
append col[(vertWeights[1][1]),(vertWeights[1][2])]
)else(
if vertWeights[3][2]>=vertWeights[1][2] then(
if vertWeights[4][2]>=vertWeights[3][2] 
then(append col[(vertWeights[4][1]),(vertWeights[4][2])])
else(append col[(vertWeights[3][1]),(vertWeights[3][2])])
append col[(vertWeights[3][1]),(vertWeights[3][2])]
)else(
if vertWeights[4][2]>=vertWeights[2][2] 
then(append col[(vertWeights[4][1]),(vertWeights[4][2])])
else(append col[(vertWeights[2][1]),(vertWeights[2][2])])
append col[(vertWeights[1][1]),(vertWeights[1][2])]
)
append col[(vertWeights[2][1]),(vertWeights[2][2])]
)
if col[1][2]>=col[2][2] then(
if col[3][2]>=col[2][2] then(
append colB[(col[3][1]),(col[3][2])])
else(append colB[(col[2][1]),(col[2][2])])
append colB[(col[1][1]),(col[1][2])])else(
if col[3][2]>=col[1][2] 
then(append colB[(col[3][1]),(col[3][2])])
else(append colB[(col[1][1]),(col[1][2])])
append colB[(col[2][1]),(col[2][2])]
)	
b1=(colB[1][1])-1
b2=(colB[2][1])-1
w1=(((1-(colB[1][2]+colB[2][2]))/2)+colB[1][2])
if w1>=1 do w1=1
)
 
w1=((w1)*100) as integer
 
 
append modelArray (model vertX:vx vertY:vy vertZ:vz normX:nx normY:ny normZ:nz tvertU:tu tvertV:tv boneA:b1 boneB:b2 weight:w1)
	)
 
 
 
 
 
msh =  ModelBin[mh]
if msh.material == undefined then (
mat=meditMaterials[1] = Standardmaterial ()
mat.Diffuse=msh.wirecolor
matCount=1
)else(
mat=msh.material	
)
 
matType=0
 
 
 
diffMap=""
reflMap=""
refrMap=""
reflAlpha=0
 
if classof mat == NoMaterial do matType=1
if classof mat == Standard do matCount=1
if classof mat == InkNPaint do matType=4
if classof mat == Blend  do matType=5
if classof mat == MultiMaterial do matCount=mat.count
 
 
for m = 1 to matCount do(
if matCount!=1 then (subM=mat.materialList[m]) else (subM=mat )
matGroup=0	
 
outline=subM.twoSided
if outline==true then outline=1 else outline=0
if subM.diffuseMap!=undefined do diffMap=filenameFromPath subM.diffuseMap.fileName
if subM.reflectionMap!=undefined do (
reflMap=filenameFromPath subM.reflectionMap.fileName
reflAlpha=subM.reflectionMap.alphaSource
)
if subM.refractionMap!=undefined do refrMap=filenameFromPath subM.refractionMap.fileName
 
 
 
print "start face count"
print num_faces
for f = 1 to num_faces do(
 
if (getFaceMatID tmesh f)==m do(
matGroup+=1
face = getFace tmesh f
append FaceArray face
)
 
 
)
 
 
 
append matArray(materials \
diffR:((subM.Diffuse.red)/255) \
diffG:((subM.Diffuse.green)/255) \
diffB:((subM.Diffuse.blue)/255) \
opac:((subM.opacity)/100) \
gloss:subM.glossiness \
spec:subM.specularLevel  \
specR:((subM.Specular.red)/255) \
specG:((subM.Specular.green)/255) \
specB:((subM.Specular.blue)/255) \
ambR:((subM.ambient.red)/255) \
ambG:((subM.ambient.green)/255) \
ambB:((subM.ambient.blue)/255) \
outline:outline \
toon:refrMap \
diffTex:diffMap \
refTex:reflMap \
refType:reflAlpha \
apexes:(matGroup*3) )
 
 
 
 
 
 
 
 
	)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
delete tmesh
 
)
 
 
 
 
s = fopen "C:\\Program Files (x86)\\MikuMikuDanceE_v713\\UserFile\\Model\\test.pmd" "wb"
 
testname="test"
comment="Test File, gn?"
 
writestring s "Pmd";fseek s -1 #seek_cur --IDs File
writefloat s 1 -- File Version?
 
writestring s testname
for x = 1 to (20-(testname.count+1)) do(writebyte s -3)
writestring s comment
for x = 1 to (256-(comment.count+1)) do(writebyte s -3)
 
 
 
writelong s ModelArray.count
for wr = 1 to ModelArray.count do(
writefloat s ModelArray[wr].vertX
writefloat s ModelArray[wr].vertY
writefloat s ModelArray[wr].vertZ
writefloat s ModelArray[wr].normX
writefloat s ModelArray[wr].normY
writefloat s ModelArray[wr].normZ
writefloat s ModelArray[wr].tvertU
writefloat s ModelArray[wr].tvertV
print ModelArray[wr].boneA
print ModelArray[wr].boneB
writeshort s ModelArray[wr].boneA
writeshort s ModelArray[wr].boneB
writeshort s ModelArray[wr].weight
)
 
print "end face count"
print FaceArray.count
writelong s (FaceArray.count*3)
for wr = 1 to FaceArray.count do(
writeshort s (FaceArray[wr][1]-1)
writeshort s (FaceArray[wr][3]-1)
writeshort s (FaceArray[wr][2]-1)
)
 
 
writelong s matArray.count
for wr = 1 to matArray.count do(
writefloat s matArray[wr].diffR
writefloat s matArray[wr].diffG
writefloat s matArray[wr].diffB
writefloat s matArray[wr].opac
writefloat s matArray[wr].gloss
writefloat s matArray[wr].specR
writefloat s matArray[wr].specG
writefloat s matArray[wr].specB
writefloat s matArray[wr].ambR
writefloat s matArray[wr].ambG
writefloat s matArray[wr].ambB
writebyte s 0
writebyte s matArray[wr].outline
writelong s matArray[wr].apexes
writebyte s 0
for x = 1 to 19 do (writebyte s -3)
)
 
 
writeshort s BoneBin.count
for wr = 1 to BoneBin.count do(
writestring s BoneBin[wr].name
for x = 1 to (18-((BoneBin[wr].name).count)+1) do (writebyte s -3)
writeshort s -1
writeshort s 0
writebyte s 1
writeshort s 0
writefloat s BoneBin[wr].pos.x
writefloat s BoneBin[wr].pos.z
writefloat s BoneBin[wr].pos.y
)
 
 
 
 
 
 
writeshort s 0
writeshort s 0
writebyte s 0
writebyte s 0
writelong s 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-- if a>=b then(
-- if c>=b then(
-- if d>=c then(append col d)else(append col c)
-- append col c
-- )else(
-- if d>=b then(append col d)else(append col b)
-- append col b
-- )
-- append col a
-- )else(
-- if c>=a then(
-- if d>=c then(append col d)else(append col c)
-- append col c
-- )else(
-- if d>=b then(append col d)else(append col b)
-- append col a
-- )
-- append col b
-- )
 
gc()
fclose s