Keyerror: 'bpy_prop_collection[key]: key not found Blender python [Solved]

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[0].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[0].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.
Greets

check if Cube exists first:

if "Cube" in bpy.data.objects:
    # do stuff
else:
    # Cube doesn't exist, do something else.
1 Like

Thank,
It’s work :slight_smile:
Thank You a lot :slight_smile:
Greets

Maybe You know how solve this problem ? :slight_smile:

class Interactive_mask(bpy.types.Operator):
    def execute(self, context):
        bpy.ops.object.mode_set(mode='SCULPT')
        bpy.ops.sculpt.sculptmode_toggle()        
        bpy.ops.sculpt.mask_expand(invert=True, smooth_iterations=2, use_normals=False, keep_previous_mask=False,)
        
        return {'FINISHED'} 

And I have problem with bpy.ops.sculpt.mask_expand When I press my button i have such a error RuntimeError: Operator bpy.ops.sculpt.mask_expand()failed, context is incorrect What I should change or add to code ?
I will be very grateful for any help :slight_smile:

Greets and one more time thanks for Your help

Without running your code, in guessing it’s the mask operator that is throwing the error- if that’s the case it’s because you are not in sculpt mode when the operator is run. This could be because you are setting the mode to ‘SCULPT’ and then toggling it back off on the next line. Try removing the toggle and see if that helps.

1 Like

Without this toggle I have this same problem. Is not working. This mask_expand is start working after You press LMB, and with second LMB You are finish creating this mask. Maybe I should use Invoke but I don’t know how …

Greets.

to fire off an operator as invoke- just pass in the INVOKE_DEFAULT string. ie:
bpy.ops.view3d.select('INVOKE_DEFAULT')

1 Like

Thank You very much, now is working :slight_smile:
I’m sending lots of greets to You :slight_smile: