# Change material of object by using python

I want select a material, which already is define, of en object by using script.

How can I do that?

Example: This works but make everytime a new material. (Material.001, Material.002, ect..)

#******Material******
bpy.ops.object.select_all(action='DESELECT')
object = bpy.data.objects['Cube']

mat = bpy.data.materials.new("Material")
print (mat)
me = object.data
me.materials.append(mat)

• mat = bpy.data.materials.get("Material") or bpy.data.materials.new("Material") – batFINGER Jan 2 '19 at 14:58

I have solved the problem by:

#***Set color***
D = bpy.data
Color_material = D.materials["Material"]
cube = D.objects['Cube']
cube.active_material = Color_material

• Slight issue here if material named Material doesn't exist, will throw a key error. – batFINGER Jan 2 '19 at 14:59