2
$\begingroup$

Sorry, I'm extremely surprised I can't find this question anywhere already. In Python I just want to check a specific object to see if it is currently in the set of objects that are selected.

$\endgroup$

3 Answers 3

3
$\begingroup$

You can also use bpy.types.Object.select_get :

import bpy

obj = bpy.data.objects["Cube"]
is_obj_selected = obj.select_get()
$\endgroup$
3
$\begingroup$

Print a message when the user selected a specific object

import bpy

specific_object_name = "Cube"
is_select = False

def draw_callback():
    global is_select

    if specific_object_name in bpy.data.objects:
        if is_select is True:
            if bpy.data.objects[specific_object_name] not in bpy.context.selected_objects:
                is_select = False
        else:
            if bpy.data.objects[specific_object_name] in bpy.context.selected_objects:
                is_select = True
                print("You selected a specific object")


# Run this on bpy.app.handlers.version_update if you use it on addon
bpy.types.SpaceView3D.draw_handler_add(draw_callback, (), 'WINDOW', 'POST_VIEW')

Or check it when active object changed

import bpy

# it trigger when user change the active object, but not via script
def msgbus_callback(*args):
    print("Check if your object is selected here")

bpy.msgbus.subscribe_rna(
    key=(bpy.types.LayerObjects, "active"),
    owner="me",
    args=(),
    notify=msgbus_callback,
)
$\endgroup$
2
$\begingroup$

You can use bpy.context.selected_objects. Here is some sample code:

import bpy


if bpy.data.objects["So_and_so_object"] in bpy.context.selected_objects:
    print("Yes, the object wanted is selected")
else:
    print("No, the object is not selected.")

But, of course, you need to replace the code inside the if statement with what you want it to do, and the name inside the data.objects list with what the wanted object's name is

For further guidance visit the official Blender Python API Documentation

$\endgroup$
0

You must log in to answer this question.

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