I wrote such a script
def execute(self, context): for x in bpy.context.object.material_slots: bpy.context.object.active_material_index = 0 bpy.ops.object.material_slot_remove() D = bpy.data if len(D.objects['Cube'].material_slots) < 1: D.objects['Cube'].data.materials.append(D.materials['Cube material']) else: D.objects['Cube'].material_slots.material = D.materials['Cube material'] if len(D.objects['Cone'].material_slots) < 1: D.objects['Cone'].data.materials.append(D.materials['Cone material']) else: D.objects['Cone'].material_slots.material = D.materials['Cone material']
My simple scrypt first deletes existing material, then assigns for Cube, “Cube material”, Script does the same for Cone Everything works fine as long as the Cube and Cone are on scene. When the Cube is missing, I get this Error: KeyError: ‘bpy.prop_collection[key]: key “Cube” not found’ What to do to make scrypt work even though the Cube is not on scene, but there is one of the objects[for example Cone] included in the script?
I would like the script to work, for example one object is on the scene [of two], for example a Cube. Now, the script doesn’t work, because if there is only a Cube on the scene, it writes an error that there is no Cone.