2
$\begingroup$

How do I deselect the active object from a selection of 2 objects with python?

And if necessary, how do I make the second object (the not active one) active once I've deselected the first one?

EDIT1: Ok I found the answer to my first question doing that:

bpy.context.active_object.select_set(False)

But I need to make the other object the active one, without using it's name to direct the selection or anything like that.

EDIT2:

Found the answer :)

bpy.context.active_object.select_set(False)
for obj in bpy.context.selected_objects:
    bpy.context.view_layer.objects.active = obj
$\endgroup$
6
  • $\begingroup$ Is this for Blender 2.8? Or 2.7x $\endgroup$
    – Georges D
    Oct 31, 2019 at 16:04
  • $\begingroup$ 2.8, I've answered my first question but still need help on second one, see edited post. Thanks ! $\endgroup$
    – globglob
    Oct 31, 2019 at 16:14
  • $\begingroup$ If not using it's name, what method will you use to define which object you need to make active? Looks like you have a special case here, so would you elaborate more? $\endgroup$
    – Georges D
    Oct 31, 2019 at 16:16
  • $\begingroup$ I'd use the fact that it would only be ONE selected object, and never more than that. So basically maybe iterate once in bpy.context.selected_objects and make object active? That's what I'm trying out now, might answer my own question fully given enough time lol. $\endgroup$
    – globglob
    Oct 31, 2019 at 16:18
  • $\begingroup$ Well, the way I use in Python to make and object active, is the same way you'll use in the viewport using your mouse, by selecting it last, so apparently you already have your answer :) $\endgroup$
    – Georges D
    Oct 31, 2019 at 16:19

1 Answer 1

3
$\begingroup$
bpy.context.active_object.select_set(False)
for obj in bpy.context.selected_objects:
    bpy.context.view_layer.objects.active = obj
$\endgroup$
2
  • $\begingroup$ Yes I see what you mean, it was much simpler in 2.7X $\endgroup$
    – Georges D
    Oct 31, 2019 at 16:38
  • $\begingroup$ Yeah it's a bit weird now. $\endgroup$
    – globglob
    Oct 31, 2019 at 17:10

You must log in to answer this question.

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