Script to rename materials based on geo name

Im looking for a script to take a bunch of objects and rename their assigned materials to match the name of the object it’s applied to.

Is there something like this already out there? (ive not been able to find it)

How would I script this? (bit of a newbie with scripting)

Any help super appreciated

Closest I’ve come to this is using ‘Detach by ID’ script. I’d combine the entire scene into a single mesh and then run this script to datach by ‘material name’ - I believe it includes the material name in the new object’s name when everything is broken up.

Ah, that could work.
May even be a better way to do it as I can then separate all the objects by material but in larger groups.

What happens if two objects have the same material?

Ultimately they should be collapsed to one object, if they have the same mat

After some trial and error, essentially what Im looking for is something to explode everything by material, and then collapse everything by material, so I end up with one object per material.

Using it for texture baking and export.
Found this script which replaces the shell material with the baked standard material, but I cant work out why it gives it the name of the baked material rather than the shell material?

If go in the listener and do

m=$.material

m.name

Then i get the name of the Shell material, not the baked material, so not sure why this script does something different.


for o in geometry do

(
if classof o.material == Shell_Material then
(

m = o.material

newmat = StandardMaterial name: m.name

newMat = m.bakedMaterial

o.material = newMat
)
)

You shouldn’t need a script to replace the shell material with the baked material. In the Render to Texture window, at the bottom you can select ‘keep baked materials’ and click clear shell materials.

Yeah but it does the same thing, keeps the name of the baked material rather than the shell.

Regardless, I definitely feel like Im going the long way around…

Ah, gotcha. Yeah that would be convenient if the mesh names could match the material’s applied! I think for myself I just used the mesh names to find the attached shaders I’d need to tweak.