Trying to select object through C or C++ code, can't find any function or variable to set current selected objects

Can someone tell me where it is, or point me to the relevent documentation? Thanks.

Okay, I found selected_objects as a member of the bContext struct. Only question is how do I set it?
It’s a ListBase struct that has two void pointers, first and last. Is it a linked list?

I found ED_object_base_select function, unfortunately the xr_raycast function gives me an Object not a Base so I can’t use it.

You can use BKE_view_layer_base_find to get the Base corresponding to the Object in the current view layer. They are separate because selection can be different in every view layer.

I tried
printf("Found: %s", ob->id.name); auto b = BKE_view_layer_base_find(CTX_data_view_layer(C), ob); ED_object_base_select(b, BA_SELECT);
But I am always getting an empty base from BKE_view_layer_base_find.

Two potential reasons for that I can think of:

  • The object is not used in the view layer.
  • The object is not an original datablock, but rather a copy evaluated by the dependency graph. In which case you can get the original with DEG_get_original_object.
1 Like

It works! I used DEG_get_original_object, and then copied some code from the ed_object_select_pick function to make it update! Thanks!