Merge Materials Based on Contents

So, I’ve got a problem - I have around 800 materials, but across all 800 of them are only 141 unique images. Is there some way I can merge/remove the duplicate materials?
I have a separate folder with the 141 images I want, if that helps anything. Also, all materials have a random name, so Tex_0125.001 and Tex_0125.002 are completely different images. Merging by name wouldn’t help.
Even if I can’t automate this, what’s the quickest way I can get this done?

It should be possible with a python script, that compares the images in the materials. I could write one, if you want. But for this I would need to see some example Materials.

Thanks a lot, I’m kind of going insane over this. Here’s the first listed material, Tex_0010_0_dds.007. (It’s actually a PNG, by the way - despite the appearance)

And here’s the second - this one has transparency.

I can send the whole blend file if you’d like, but it’d likely be huge.

The BlendFile would be perfect (and don’t forget to pack the textures with the blend file). Then I could write a script for you in the evening (in about 12h, if this isn’t too late for you)

I would love that, thanks so much! You’re a life saver.

Blender has the ability to replace datablocks (this includes materials) with others in the outliner but it’s manual. Not sure if there’s an automated way without python.

Appended.blend (4.9 MB)

I let the script run already on this file, because it is quite slow.

It is so slow, because it merges by image-pixels (two materials are the same, if the image is pixel per pixel the same). I designed it this way, because you said the following:

But after a short lokk, I couldn’t find any Materials that are different, but start with the same number. So maybe merging by name would have benn enough


Thanks so much! Although, opening the file, all the images are missing. And merging by name wouldn’t have been enough, because the current file was made from 5 auto generated segments, appended from their own file after being processed - this meant that the name Tex_0125_0 would have been assigned to one image for one file, yet another images for another file. For example, here’s “”
and here’s “”

Oh sorry, I forogt to repack the images. But you can rerun the script on your file. I hope it gives the desired result. And thanks for the counter-example for name merging.

1 Like

Okay, it’s run and I think it worked! Purging removed 488 materials, and I’ve only got 109 material slots now.

1 Like

Comparing pixel to pixel sounds like a lot of comparison operations. Is it better to create and store image hashes and compare hash results?

Of course this method is inefficient as hell.
There are many possibilities to improve the speed. If there is a wide interest in it, i could speed it up and make a proper addon out of it.