2
$\begingroup$

I'm using the Blender to make all my works university (architecture), but in my job the people use Sketchup for modeling and V-Ray for rendering. I want use Blender, cause liked more then others, the problem is that to rendering with Cycles, I have to need import the Skp file, change all materials (or set), create lamps etc... How to make a Python script that replace the materials by one list in all objects on the scene?

For example:

  • Cube.001 have the material "OldMaterial.01" in one face, and "OldMaterial.02" in others.

  • Cube.002 have the material "OldMaterial.02" in one face, and "OldMaterial.03" in others.

And we have the list:

  • For "OldMaterial.01", replace to "NewMaterial.01"
  • For "OldMaterial.02", replace to "NewMaterial.02"
  • For "OldMaterial.03", replace to "NewMaterial.03"

And the script replace all materials for the match in the list.

That will be increase so much the workflow Sketchup - Blender!

Thanks for now!

$\endgroup$

1 Answer 1

2
$\begingroup$

Every object has a list of materials it uses in it's material_slots property, this is shown in the material properties, each slot contains the material used. By looping through this list you can change the material assigned to each slot.

I would setup a dictionary of old-to-new names to use for the conversion process.

import bpy

matnames = {
    'OldMaterial.01': 'NewMaterial.01',
    'OldMaterial.02': 'NewMaterial.02',
    'OldMaterial.03': 'NewMaterial.03',
    'OldMaterial.04': 'NewMaterial.04',
    'OldMaterial.05': 'NewMaterial.05',
    'OldMaterial.06': 'NewMaterial.06',
}

for obj in bpy.data.objects:
    for slot in obj.material_slots:
        slot.material = bpy.data.materials[matnames[slot.material.name]]
$\endgroup$
4
  • $\begingroup$ Nice! I'm new Python's user, and don't know programing :( But I trying to learn :D Thanks for all. But i have so many questions more hahahahhaa I try run this script that u posted, but don't worked :/ In the line "matnames = { .....}" u are creanting a dictionary, right? $\endgroup$ Commented Sep 16, 2017 at 16:01
  • 1
    $\begingroup$ A good start would be spending an hour or two doing a getting started tutorial like the official python tutorial. Yes matnames is a dictionary, it uses the name of the old mat as the key to retrieve the name of the new mat. Everything between {} is the contents of the dictionary, adjust the old name and new name on each line to match what you have. $\endgroup$
    – sambler
    Commented Sep 17, 2017 at 4:00
  • $\begingroup$ @sambler Great answer! This was very helpful to me, as I had spent several hours struggling to write something similar. One thing I noticed and want to modify, is that while an object with materials (for example) R,G,B will get replaced with R_new, G_new, B_new... if an object has R,G_new, B, then material "B" won't get replaced because "G_new" isn't a match (it's only looking for R,G,B and stops at a non-match). Can you please teach how to skip over non-matches? $\endgroup$
    – Mentalist
    Commented Oct 23, 2020 at 4:12
  • $\begingroup$ @Mentalist My first suggestion is to start with a full list, even if the replacement is the same as the original, go into blenders python console and type bpy.data.materials.keys() to get a complete list of material names. The other option, is after the second for add if slot.material.name in matname.keys(): to only do the last command if a substitute exists. You could also put the last statement into a try block to catch errors and keep going, you only need pass in the except section. $\endgroup$
    – sambler
    Commented Oct 23, 2020 at 9:43

You must log in to answer this question.

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