3
$\begingroup$

What's the best way of replacing a material in your scene with another one?

At first I thought I could simply bpy.data.materials['old_mat_name'] = new_mat, but that reference is read only so it throws an error.

Do I really have to comb through every object in my scene for material slots named old_mat_name and replace them with the new one?

Is there a better way of getting one material to match another exactly?

$\endgroup$

2 Answers 2

1
$\begingroup$

I don't know if there's a best way to do it since it depends on your file and workflow but :

import bpy

m_source = bpy.data.materials["Material.001"]
m_target = bpy.data.materials["Material.002"]

for obj in (o for o in bpy.data.objects if hasattr(o, "material_slots")):
    for slot in obj.material_slots:
        if slot.material is None:
            continue
        if slot.material.name == m_source.name:
            slot.material = m_target

There actually are material operators to copy/paste material settings but they require a handle on an active object with both materials so it might no be very convenient.

https://docs.blender.org/api/current/bpy.ops.material.html

$\endgroup$
1
  • $\begingroup$ Yeah I was hoping there was a better way but thanks for your response. $\endgroup$ Sep 4, 2021 at 20:38
2
$\begingroup$

To change all objects with old material (brown) to new material (blue).

enter image description here

Go to the Outliner and select Blender File view.

enter image description here

Find the old material under Materials, right click it and select Remap Users.

enter image description here

Select the new material from the popup window.

enter image description here

enter image description here

Note that the new material does not need to be assigned to an object, it just needs to be present in the blend file.

$\endgroup$
1
  • $\begingroup$ You can do this with lots of stuff, including mesh data ;) Very handy when you forget to instance and duplicate instead. $\endgroup$ Jun 30, 2022 at 20:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .