user824624
user824624

Reputation: 8066

Use blender change material color with python

I have a model about blender,there is a object (name's car_AudiA8) with multi-material,I want to copy a new object from the object ("car_AudiA8"),then change new object's color and old object's color is not affected,my method is :

obj = bpy.data.objects["car_AudiA8"]
mesh = obj.data
new_obj = bpy.data.objects.new("car_AudiA8", mesh)
bpy.context.scene.objects.link(new_obj)
bpy.ops.object.make_single_user(object = True, obdata = True, material = True,texture = True )
for slot in bpy.data.objects[new_obj.name].material_slots:
    if (slot.name.startswith("carpaint.Black")):
        bpy.data.materials[slot.name].diffuse_color = (1,0,0)

note: material("carpaint.Black") can control car's color .

Upvotes: 2

Views: 8908

Answers (1)

Peter Gibson
Peter Gibson

Reputation: 19544

It looks like you're modifying the existing "carpaint.Black" material, which would affect all objects using that material. Instead, try assigning a new material to that slot

for slot in bpy.data.objects[new_obj.name].material_slots:
    if (slot.name.startswith("carpaint.Black")):
        new_mat = bpy.data.materials.new(name="carpaint.NewRed")
        new_mat.diffuse_color = (1,0,0)
        slot.material = new_mat

I'm not sure if this will work as is, but you get the idea. You might be better off copying the black material instead of just creating a new material from scratch.

Upvotes: 2

Related Questions