Checking if selection is empty doesn't work

Hi there!

I’m working on an addon that have a button running a custom operator, which requires to hase at least a bone selected.

So while working on detecting whether bones or objects are detected, I noticed that, even after clicking an empty space or using the Select None operator, the API still returns “something” when asked about what’s selected/active:

In Object mode:

>>> C.object
bpy.data.objects['Armature']

>>> C.active_object
bpy.data.objects['Armature']

In Pose mode:

>>> C.selected_pose_bones
[]

>>> C.active_pose_bone
bpy.data.objects['Armature'].pose.bones["Bone"]
To be clear, I managed to get my things working as I wanted
@classmethod
def poll(cls, context):
    return context.selected_pose_bones

But I have the impression that I can get around this issue this time only because I happen to only need a selected bone ;
But if I had to work with object selection, or with active selection, to me it looks like it wouldn’t work, at least not this way?

I know that selection and active are independent, and you can have either one without the other, but it seems weird that even after making sure to “deselect all”, the API seems to consider there is still something selected. Is that even normal?

Or maybe it would require me to make a custom script that forces Blender to clear out selection/active, but that sounds like a terrible solution for the user if using Blender’s “Select None” doesn’t actually do as it says.

Use bpy.context.selected_objects instead for object mode. Likewise use bpy.context.selected_bones for pose mode. And also bpy.context.selected_editable_bones for edit mode.

I know, but why is it returning stuff when nothing is selected?

Which method:

  • selected_objects
  • selected_bones
  • selected_editable_bones

Are you sure that absolutely nothing is selected. If you’re unsure, do ALT+A to deselect everything. What could also be happening is that you’ve selected some other object as well as the active object in Object mode before switching to either Edit or Pose mode.

Alt A does the same thing as clicking in an empty space, which is the Select None operator .

So far, after selecting “none” in any way, with no possible other object to be selected while not in object mode:

Object mode:

Pose mode:

Object mode:

The issue is, checking for a selected object or active object/element always returns something after deselecting all.

Store active selections in a list or dict, cancel all selections, then reselect bones or objects that you had selected previously after deselecting all.