3
$\begingroup$

Is there a way to check if objects are selectable in Blender Python?

The code below checks if an object is visible, but is there also something to check if an object is also selectable in viewport?

Example for checking if visible:

if obj.visible_get():
    # Do something...

The reason is: If you set off the "Object Types Selectibility" for Meshes, then they can still be selected using python or in the outliner. See my shot, which area I mean.

enter image description here

$\endgroup$

3 Answers 3

4
$\begingroup$

You can use the property hide_select to set an object's selectability:

import bpy

obj = bpy.data.objects['Cube']
obj.hide_select = True # makes the cube not selectable

Then you can use it to check if it is selectable:

if obj.hide_select:
   print("object", obj, "is not selectable")
$\endgroup$
2
  • 1
    $\begingroup$ Interesting side note from playing around with your answer and mine: it's possible to disable an object's selectability when show_object_select_mesh = True but it's not possible to enable an object's selectability when show_object_select_mesh = False. Hence a rigid check for selectability would need to be if not obj.hide_select and show_object_select_mesh: ... $\endgroup$
    – taiyo
    Commented Sep 5, 2023 at 11:18
  • 1
    $\begingroup$ @taiyo ah nice, this will all be useful information then for the community! $\endgroup$
    – Harry McKenzie
    Commented Sep 5, 2023 at 11:35
4
$\begingroup$

I've found a solution for my situation:

for obj in bpy.data.objects:
    if obj.visible_get() and bpy.context.space_data.show_object_select_mesh == False:
        # Do something...

I forgot, I can use the Info window in Blender to see the most command codes, that were recently used by the user.

$\endgroup$
1
3
$\begingroup$

You can query the Object Types Visibility dialog, for example for the MESH type:

def is_mesh_type_selectable():
    for area in bpy.context.screen.areas:
        if area.type == 'VIEW_3D':
            return area.spaces[0].show_object_select_mesh
        
    raise Exception('No 3D Viewport found.')

For the other object types, have a look in the docs, they are all starting with show_object_select....

$\endgroup$
1
  • $\begingroup$ In addition one can also use bpy.context.selectable_objects which accounts for object visibility and selectability (the outliner settings) - but not viewport overrides for visibility and selectability $\endgroup$
    – Gorgious
    Commented Sep 5, 2023 at 11:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .