How to sort all materials on object.

Hi everyone. Is there any way to sort all materials on a single object in some predictable order (alphabetical, for example)? Not by using that little “+” sorting button in the bottom of the material list, but for real.
Let me explain. Usually, I import a lot of identical models from DAZ studio. And every time the materials sorting order in the list is completely random, so I can’t just transfer my previously made material setup from one object to another using “Ctrl+L” operation. The “+” sorting button doesn’t help either because “Ctrl+L” operation just ignores that sorting order.
Would be grateful for any help.

1 Like

Well, didn’t find anything useful about this issue on the internet, so I wrote this little addon. It adds two sorting buttons on the material tab. Maybe someone will find it useful.
You can install it via “Install Add-on from file…” button.
MatSorting.zip (767 Bytes)

I just discovered this script here, it’s exactly what I’ve been looking for .

I was wondering if anyone would be kind enough to help us tweak it so it would
work with 2.63-2.66? We use these versions to support older hardware in
our workflow.

Haaaalelujah! Same use case, same problem, your script is what I’ve been fantasizing about!

I wrote a macro for Notepad++ which will alphabetise everything in the MTL files my OBJs use, but because they are imported as separate meshes and need to be joined again, the order of the materials still gets muddled up in Blender, sadly. So that was a waste of time…

I looked into the PY file and narrowed it down to this:-

import bpy
bpy.context.object      
for j in range (len(ob.material_slots)):
    for i in range (len(ob.material_slots)-1):
        ob.active_material_index = i
        tempStr = ob.active_material.name
        ob.active_material_index = i+1
        if ob.active_material.name < tempStr:
            bpy.ops.object.material_slot_move(direction='UP')


Still poking around the API documentation and trying different things to get it to work with 2.63, but still no luck. Trying to save in 2.79 and bring it back to 2.63 gives the “expect loss of data” warning, but I’m yet to figure out what exactly has gone wrong, if anything. It’s still not convenient even if everything is okay. I’ll keep poking at it and post back if I figure it out.

Please, any help to get this piece of Python working with 2.63 would be so awesome.

I figured out that it’s not possible… “object.material_slot_move” is not a thing, it was introduced somewhere between 2.72 and 2.79. This would need a completely different and (I imagine) much more complex solution if it’s going to work with anything lower.