Is replacing material by slot number the only option? [Pics attached]

Hi guys, I have an object named ‘obj_chess’ which I would like to replace an material called “PaperTopYellow” with “PaperTopRed”.

I used a code as bellow['obj_chess'].data.materials['PaperTopYellow'] =['PaperTopRed']

but it did not work so I had to use['obj_chess'].data.materials[3] =['PaperTopRed']

to make it work.

However, the problem is that PaperTopYellow may not always be in the third position of the slot. How do I specifically target it by name and replace it?


Try something like this:

slotnr = list(i for i, m in enumerate(["Cube"].data.materials) if == "papertopyellow")[0]

Thanks for the reply, but it does not seem to work. I run the script of

list = enumerate(['Obj_chess'].data.materials)

And it prints <enumerate object at 0x116e404c8>

Not sure if I can manipulate the above string to target the one I am aiming for.

for i, m in enumerate(['Obj_chess'].material_slots):
    if == 'PaperTopYellow':
       slotnr = i

I have tested this out. This should be the one

Do it the way I wrote it.