Hey guys,
I have a problem here that is bugging me for years. When we get CAD data, we often have multiple materials sharing the same name in the scene. These are exactly the same material, but assigned to different objects. Is there a way to condense these materials into one material in a fast (one-click) way? Something like this must be very easy in MaxScript, but I don’t know how to use it.
I can help, likely.
Would you be so kind as to send me a sample scene for this? Small is fine, i just need to test the logic.
Ok, i made the test file myself.
Attached, the test scene and the script.
Running it will get stuff doodled, and eventually done.
It will print minimal info in the listener, and it will print out if anything (hopefully graciously) failed.
It’s built with heavy scenes in mind, so it will set up max in the quickest possible condition before running, and return it to normal as the process has concluded successfully or not (meaning it’ll turn on expert mode, deactivate UI bits, disable VP redraw, and so on.).
It will NOT save your scene.
It will recognise different shaders based on their first level properties only (for speed. traversing and hashing complex shaders would take a long time, potentially).
Run it on the provided scene first to see what the behaviour is, and please report anything which doesn’t behave as you would like it to.
sample result:
“Parsing and hashing 491 materials.”
“Parsing and Hashing 2274 geometry nodes.”
“all done.”
18.868 seconds
EDIT: New version here.
I am personally pouring a virtual drink for LeLe…cheers!
I have come across this kind of shit in the past and it is definitively a massive ball-ache. So whilst I don’t have anything of the sort right now, thanks LeLe, great work! ![]()
!! gotta try that on monday! Thanks!
Jesus, you’re quick! I just tested it on a heavy production scene, and it actually works as expected! It just reduced the amount of materials from 113 to 21.
Great, thank you so much!
I owe you some beer. You still like that, right?
I’m horrible at holding beer, these days.
But i’m half a farmer, so i’ll happily get a few seeds! ![]()
Made a new version (right-mouse click and download as…), which you can grab directly from my gitHub (i *strongly* discourage you from poking around and downloading stuff. it’s not meant for widespread usage, and it’s likely broken to bits anyway. You’ve been warned.
).
The version now creates a macroscript under the VRay category, called Duplicate Material Condenser.
You can then assign it a shortcut key, drag it on bars and whatever else.
It’s got an UI which simply allows to choose the undo state (turn off should the scene be ginormous, and ram very scarce. makes no difference to speed that i could see, if ram is aplenty.), and has three progress bars, one per script phase.
It will also pop a neat dialog box as it completes, showing statistics of what was done to the scene.
There has been no substantial change in the code, if it’s been cleaned up and reorganised a bit.
Great can’t wait for a crappy Revit Scene to try this on. Does this also work with materials that are part of multiple multisubs?
In principle, yes: it operates on the scenematerials library, and those materials are present there as individual ones too.
It does not condense a multimaterial in fewer IDs, that’s not what the goal is, as that’s covered by the tools in the material editor, but it should get rid of more identical materials which aren’t instances.
But please verify it does what it should. ![]()
I second that! Thanks Lele!
Sounds like a cool tool - just tested and it doesn’t seem to work as i expected. If i have 5 cubes and 5 materials all with the same diffuse colour should the script make all 5 cubes just have the one material ?
No. The script is intended to work if the 5 cubes have 5 different materials, but all with exactly the same name. This can never happen in Max, because Max always needs a unique name for materials when assigning them to objects, but stuff like this easily happens by importing objects from unknown sources.
Now i understand. Works perfectly. Will help massively with SK model imports
Imagine the cost: personal (your most precious time!), and of energy (the PC staying on for you to just click stuff, essentially.).
This stuff makes me super, super, super happy of doing the job i do. ![]()
The record, however, is currently, and to my knowledge, belonging to someone else:
I can’t begin to fathom the scene’s complexity…
Oh, wow! Okay, that’s something different…
My thoughts to the dot when i saw it, ahah.
One thing I just noticed: The whole process takes significantly longer if the Material/Map Browser is open. I have to remember myself to close it before starting.
Umh, i had a v003 which i apparently never shared.
Would you be so kind as to be my guinea pig, Olli?
It should just work fine, and take care of putting max in its most performant state.
It’s also turned into a macroscript, so when run it will go into the “VRay” category, as “Duplicate Material Condenser”, and can then be assigned to shortcuts, menus, and so on.
Attached (but TEST it first!)
mergeMaterialsWithSameName_003.zip (2.16 KB)

