Home Coding, Scripting, Shaders
The BRAWL² Tournament Challenge has been announced!

It starts May 12, and ends Sept 12. Let's see what you got!

https://polycount.com/discussion/237047/the-brawl²-tournament

3ds Max - Remove Duplicate materials from Multi-sub object

mawilbolou
polycounter lvl 11
Offline / Send Message
mawilbolou polycounter lvl 11
Hi guys...

I am working on Max 2012 and am working on quite a big scene.

I have combined a number of models together and sampled the multi-sub material in the Material Editor.

Unfortunately, it seems that Max has generated about 300 different materials, even though I am only using about 20 textures. After looking at some of the materials, I have found that there are alot of duplicate materials that are using one texture.

Is there a script that detects by texture, then deletes the duplicates?

Any help would be massively appreciated..

PS I have tried using the "Clean MultiMaterial Utility"

Cheers

Replies

  • monster
    Offline / Send Message
    monster polycounter
    Hey bro, I had this script laying around. If you are only using diffuse maps it might help you.
    1. (
    2. prevMatCount = scenematerials.count
    3. diffuseMapList = #()
    4. materialList = #()
    5.  
    6. scenemats = scenematerials
    7.  
    8.  
    9. for m = 1 to scenemats.count do
    10. (
    11. dmapFile = scenemats[m].diffuseMap.filename
    12. getMat = findItem diffuseMapList dmapFile
    13. if getMat == 0 then
    14. (
    15. --MATERIAL NOT FOUND, ADD IT TO THE MATERIAL LIST
    16. append diffuseMapList dmapFile
    17. append materialList m
    18. )
    19. else
    20. (
    21. --MATERIAL FOUND, APPLY TO ALL IT'S OBJECTS
    22. for obj in refs.dependents scenemats[m] do
    23. (
    24. try
    25. (
    26. obj.material = scenemats[getMat]
    27. )
    28. catch()
    29. )
    30. )
    31. )
    32. gc lite:true
    33. messageBox ("Materials condensed from " + prevMatCount as string + " material(s) to " + materialList.count as string + " material(s).") title:"Materials"
    34. )
  • TorQue[MoD]
    Offline / Send Message
    TorQue[MoD] polycounter lvl 20
    I tried to get Chat GPT to modify the script to not only remove duplicate sub-object ids, but also remove them from the mesh, but it wasn't able to sort it out. Maybe someone who knows MaxScript could fix this?
    1. (
    2. -- Get the selected object
    3. obj = selection[1]
    4. -- Ensure the object is valid and has a Multi/Sub-Object material
    5. if obj != undefined and isKindOf obj.material MultiMaterial then
    6. (
    7. -- Get the Multi/Sub-Object material
    8. multiMat = obj.material
    9. matCount = multiMat.numsubs
    10. -- Arrays to store material names and the first instance mappings
    11. matNameList = #() -- Stores unique material names
    12. replacementMap = #() -- Maps duplicate slots to their first occurrence
    13. -- Step 1: Loop through all sub-materials and find duplicates
    14. for i = 1 to matCount do
    15. (
    16. mat = multiMat.materialList[i]
    17. if mat != undefined then
    18. (
    19. matName = mat.name
    20. getMat = findItem matNameList matName
    21. -- If it's the first occurrence of this material name, store it
    22. if getMat == 0 then
    23. (
    24. append matNameList matName
    25. replacementMap[i] = mat -- Keep the first occurrence
    26. )
    27. else
    28. (
    29. -- If duplicate, replace with the first occurrence
    30. replacementMap[i] = replacementMap[getMat]
    31. )
    32. )
    33. )
    34.  
    35. -- Step 2: Reassign cleaned materials to the Multi/Sub-Object material
    36. for i = 1 to matCount do
    37. (
    38. if replacementMap[i] != undefined then
    39. multiMat.materialList[i] = replacementMap[i]
    40. )
    41.  
    42. -- Step 3: Remove empty slots in the material
    43. newMaterialList = #()
    44. for mat in replacementMap do
    45. (
    46. if findItem newMaterialList mat == 0 then append newMaterialList mat
    47. )
    48. multiMat.materialList = newMaterialList
    49. multiMat.numsubs = newMaterialList.count
    50. -- Step 4: Reassign Material IDs on the mesh to the first instance
    51. for face = 1 to obj.numfaces do
    52. (
    53. -- Use the correct function to get the material ID for each face
    54. matID = getFaceMatID obj face
    55. -- Check if the material ID is valid (not undefined)
    56. if matID != undefined and matID > 0 and matID <= matCount then
    57. (
    58. -- Find the original material that corresponds to the duplicate
    59. originalMat = replacementMap[matID]
    60. -- Ensure the original material is valid
    61. if originalMat != undefined do
    62. (
    63. -- Find the new material ID in the cleaned list
    64. newMatID = findItem newMaterialList originalMat
    65. if newMatID != 0 do -- Only reassign if newMatID is valid
    66. (
    67. -- Set the new Material ID for the face
    68. setFaceMaterialID obj face newMatID
    69. )
    70. )
    71. )
    72. )
    73. -- Garbage collection and message
    74. gc light:true
    75. messageBox ("Sub-materials reduced from " + matCount as string + " to " + newMaterialList.count as string) title:"Multi/Sub-Object Cleanup"
    76. )
    77. else
    78. (
    79. messageBox "Please select an object with a Multi/Sub-Object material." title:"Error"
    80. )
    81. )

Sign In or Register to comment.