How to find the name of a material

By means of bpy.ops.material.new() I create new materials.
The materials are named automatically.

How can I get to know the names of these materials created by means of Python?

Don’t ever use bpy.ops on a python script !

You should use the regular way to create a material and assign it on an object :

mat = bpy.data.materials.new("TheNameYouWant")
bpy.context.object.data.materials.append(mat)               #I'll consider you want to do that on the selected object

Thank you DoubleZ_,

I have removed the bpy.ops.material.new() command and inserted your code.

However I also use the bpy.ops.mesh.primitive_cube_add command.
Should this one also be replaced?

i am confuse about what’s different between ops and data ?
why dont use ops?
Thank you

there’s nothing fundamentally wrong with using ops, it just leads to problematic situations such as the one described in the OP where you’ve created something and have no way of referencing it. If you just need to create a cube or materials and don’t need to do anything with the data, there’s no harm in running a built-in op, often they are significantly faster since they use native execution paths.

1 Like