9

I want to delete all bones in an armature which contain a specific word but I can't figure out how to. This is the script:

import bpy
import fnmatch

ob = bpy.context.object

if ob.type == 'ARMATURE': armature = ob.data

bpy.ops.object.mode_set(mode='EDIT')

for bone in armature.bones: if fnmatch.fnmatchcase(bone.name, "something"): armature.delete(bone)

And returns the error 'Armature' object has no attribute 'delete'. What am I getting wrong?

L0Lock
  • 15,965
  • 1
  • 20
  • 44
leaderlord
  • 93
  • 1
  • 3

1 Answers1

6

Firstly, blender tends to use the term remove instead of delete. Secondly, the armature doesn't directly store the bones, they are stored in a collection called edit_bones (at least, they are when you are in Edit Mode, which is what you script does. bones is for Pose Mode/Object Mode, but bones can't be deleted from there).

So, you firstly need to loop over the edit_bones instead of bones and then use remove instead of delete.

for bone in armature.edit_bones:
    if fnmatch.fnmatchcase(bone.name, "something"): 
        armature.edit_bones.remove(bone)

You can easily explore the properties available on the armature using blender's Python Console, typing in your code there and pressing Ctrl+Space to autocomplete:

>>> bpy.data.objects['Armature'].data.
                                      animation_data
                                      animation_data_clear(
                                      animation_data_create(
                                      as_pointer(
                                      bl_rna
                                      bones
                                      copy(
                                      deform_method
                                      draw_type
                                      driver_add(
                                      driver_remove(
                                      edit_bones
                                      get(
                                      ghost_frame_end
                                      ghost_frame_start
                                      ghost_size
                                      ghost_step
                                      ghost_type
                                      id_data
                                      is_editmode
                                      is_library_indirect
                                      is_property_hidden(
                                      is_property_readonly(
                                      is_property_set(
                                      is_updated
                                      is_updated_data
                                      items(
                                      keyframe_delete(
                                      keyframe_insert(
                                      keys(
                                      etc....
                                      etc...
                                      etc..
                                      etc.

Using this, you would be able to see there is no delete method.

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125