I want to rename my Material slots for a series of objects. In another question, someone suggested that I use this code:
gen = (s for s in ["_Spring", "_Summer", "_Winter", "_Autumn"])
bpy.data.objects[0].name = bpy.data.objects[0].name + ".000"
for oj in bpy.data.objects:
s = next(gen)
name = oj.name[: -4]
oj.name = f"{name}{s}"
oj.data.name = oj.name
oj.material_slots[0].material.name = f"{name}_leaf{s}"
oj.material_slots[1].material.name = f"{name}_trunk{s}"
Some objects that I need to process have a third slot called "Leaf_2". How could I add a check which, if the slot exists renames the slot similar to how slot 0 and 1 are renamed? And if it there is no such slot, don't modify nor create one. How could that be achieved?