If you want to replace several object’s material with another, you usually press shift-L /ctrl-L to assign the selected material
but when these objects have more than one material each and you just want to change the one material they have in common what do you do?
to explain: I have imported a building from sketchup and because it was too big, I imported it in two parts: floor 1 and floor 2. Now the problem is that each floor (which consist of several objects) has a separate material for wall but I want to apply just one. the thing is that some objects have on them more than one materials. I tried using shift-L/ ctrl-L but it doesn’t work
you don’t get me,
that’s not the point. the point is to be able to replace a material for many objects at once if they have more than one materials…
e.g. if you have 10 objects, that each has a porcelain and a chrome material and you want to replace the chrome material for those objects with -lets say- aluminium, what would you do?
currently I have to select them one by one and do the replacement.
you mean to tell me there is no simple way to replace one material throughout the scene with another?
oh, the humanity!!!
It is things like this that make me thing that blender will never be a professional application
import bpy
old_mat = bpy.data.materials.get("The name of material to replace")
new_mat = bpy.data.materials.get("The name of the new material")
old_mat.user_remap(new_mat)
I’m not sure I understand correctly but I think you’re showing copying the shaders from one material to another right?
If that’s correct, then you have the problem that if you make changes to one material, the other one will not be updated with these changes…
come on man…
you went through the trouble of making a gif but not to read the questsion:
I’ve said it three times already, I know this procedure. What I’m asking is if one object has two or more materials and you want to change just one of them, this method does not give you control over which material you can change. It only works for single material objects!
I don’t think the feature you want is implemented yet. In the outliner you can select multiple materials, You would then want to right click and select ‘make single user’ - You could then just choose your custom material to replace all the existing materials.You can ‘right click’ and ‘remap user’ but its still painful to have to change each slot one at a time…
Hi Stephen, i know your post i almost a year old, but your three lines of code might be just what i need
I there a way to exchange all the materials in a scene at once using your approach?
My case is as follows- I use a different Software for nurbs modelling, and import into Blender via FBX format-
In my Nurbs Software i have a Material library that I use all the time- and when exporting as fbx, blender keeps the proper name of each material- just the actual Materials are useless…
My Hope is to rebuild my material library on the blenderside using the exact names, and then save it as a Library(maybe linking a Blend file?)- so when ever I import an fbx, with materials with the same name blender should replace all the shaders with the ones from that Blendfile. Do you think its possible with scripting?
Greetings James
Something like this should work to replace materials by name from library
import bpy
library_path = "path to library file.blend"
# get mat names available in library
with bpy.data.libraries.load(library_path) as (data_from, data_to):
available = set(data_from.materials)
old_mats = {mat.name: mat for mat in bpy.data.materials if mat.name in available}
# assign temp name to old materials
for name, mat in old_mats.items():
mat.name = "{}_tmp".format(name)
# load from library
with bpy.data.libraries.load(library_path, False, False) as (data_from, data_to):
data_to.materials = list(old_mats.keys())
# replace or remove temp name
for name, mat in old_mats.items():
new_mat = bpy.data.materials.get(name)
if new_mat is None:
mat.name = name
else:
mat.user_remap(new_mat)
bpy.data.materials.remove(mat)
HI Stephen, Thanks for you script and great explanation in it
I will try to test it- if I get it to work… (not sure how to tell python the path yet, but i will try to figure it out)
My original idea was to have the materials already linked into the working file by using Link/append, so
I wouldn´t have to define a path to the File… would that also be feasible?