Home Technical Talk

condense Material IDs

caogaiyu
polycounter lvl 6
Offline / Send Message
caogaiyu polycounter lvl 6
Hi guys, I found this code online and it helps to condense material Ids in a multisub material and recreate a new multisub. It assigns old ID to new one follow the new multisub material that it recreated.

It work perfectly until I have a empty slot/few empty slot in a multisub material (eg. I use ID 1 to 100 but I clear out ID 89, so there is no material at 89) then it start to give problem.

So anyone have better idea to solve this,I have try for days, but as beginner for maxscript it just too hard.

thank you in advance.

global CondenseMaterialIDs

rollout IDs "Condense Material IDs"
(
local collapseBy = "Name"

radioButtons CollapseByRadioButtons "Collapse By:" labels:#("Name", "Diffuse Texture Name") default:1

on CollapseByRadioButtons changed state do
(
collapseBy = case CollapseByRadioButtons.state of
(
1: "Name"
2: "TextureName"
default: "Name"
)
)

fn getTextureName mat =
(
local textureName = mat.name
try
(
diffuseTextureMap = mat.diffusemap
print (classof diffuseTextureMap)
if classof diffuseTextureMap == Bitmaptexture do
(
textureName = getFileNameFile diffuseTextureMap.bitmap.filename
)
)
catch()
textureName
)

button CondenseMaterialIDsButton "Condense Material IDs"

on CondenseMaterialIDsButton pressed do
(
sel = selection as array
--for object in selection which is Editable Poly and object material is MultiMaterial
for obj in sel where classof obj == Editable_Poly and obj.material != undefined and classof obj.material == MultiMaterial do
(

for originalMaterial in obj.material.materialList do
(
print (getTextureName originalMaterial)
)
materialIDList = obj.material.materialIDList
materialMap = #()
append materialMap (findItem materialIDList 1)
materialList = #(obj.material.materialList[materialMap[1]])
for i = 2 to materialIDList.count do
(
index = (findItem materialIDList i)
mat = obj.material.materialList[index]
notFound = true
newIndex = 0
for j = 1 to materialList.count while notFound do
(
otherMat = materialList[j]
if collapseBy == "Name" then
(
if mat.name == otherMat.name do
(
notFound = false
)
)
else
(
if collapseBy == "TextureName" do
(
if (getTextureName mat) == (getTextureName otherMat) do
(
notFound = false
)
)
)
if not notFound do
(
newIndex = j
)
)
if notFound do
(
append materialList mat
newIndex = materialList.count
)
append materialMap newIndex;
)
print "materialMap {"
print materialMap
print "}"
if materialList.count != obj.material.materialList.count do
(
newMat = MultiMaterial numsubs:materialList.count
materialFaces = #()
print materialFace
for i = 1 to newMat.materialList.count do
(

newMat.materialList = materialList
newMat.materialIDList = i
newMat.names = materialList.name
append materialFaces #()
)
obj.material = newMat
for i = 1 to obj.GetNumFaces() do
(
oldMaterialID = obj.GetFaceMaterial i
print oldMaterialID
oldMaterialIndex = findItem materialIDList oldMaterialID
newMaterialID = materialMap[oldMaterialIndex]
print "old ID is" oldMaterialID "and new ID is" newMaterialID
if oldMaterialID != newMaterialID do
(
append materialFaces[newMaterialID] i
)
)
for i = 1 to materialFaces.count where materialFaces.count > 0 do
(
polyOp.setFaceMatID obj materialFaces i
)
)

)
)
)
CondenseMaterialIDs = newRolloutFloater "CondenseMaterialIDs" 150 110
addRollout IDs CondenseMaterialIDs
Sign In or Register to comment.