Making a New Material and Naming it?

I’m using

bpy.ops.material.new()

to make a new material and I read somewhere (probably old versions) that I can add a string “name” parameter for the name of the new material but I can’t quite get it to work. Any advice is appreciated.

not ops. its the data you want. ops don’t return a value.


mat = bpy.data.materials.new('mat')

On a related note, how would I go about changing the names of existing materials? I’ve got this loop


for i, mat in enumerate(bpy.data.materials):
    mat.name = '{0}.{1}'.format(name, i)

and then I export the file to collada, but in the resulting file the material names are the same as they were initially. What can I do to make my changes stick?

Direct naming in Blender only works if the name does not already exist. So you have to check if the name exists before you rename. This sounds excessive, but it is the only way to make it really stick.

Your loop worked for me.

I managed to get it to work by putting the loop before the geometry manipulation at the beginning of the script. It seems that just setting the name property doesn’t flag the object as out of date, so it wasn’t being reflected in the output, but putting it before an operation that does set the flag got it working.