1
$\begingroup$

I import an object into the scene via bpy.ops.import_scene.obj. Before adding this object I have added some other objects in some other layers. However, after importing the new object bpy.context.scene.objects.active does not change. I tried bpy.ops.object.select_all(action='DESELECT') before importing the new object but it didn't work and the active object is still another object in the scene. I also tried the following before importing the new object:

for obj in self.scene.objects:
    obj.select = False
bpy.context.scene.update() # Which seems unnecessary, but I do it just in case

But it still doesn't work.

To get around the issue temporarily, I did bpy.context.scene.objects.active = bpy.context.scene.objects[0] after importing the new object. This way I manually change the active object which is not ideal, and I should not be the case.

I am a bit confused why deselecting the objects does not work. I wonder, am I doing something wrong?

$\endgroup$
  • 1
    $\begingroup$ Could try setting context.scene.objects.active = None before the import. If after the import you want one of imported (and for most importers, selected) objects to be the active, set it to a member of: context.selected_objects. $\endgroup$ – batFINGER Sep 11 '18 at 11:53
  • $\begingroup$ @batFINGER After importing the object, bpy.context.selected_objects shows the right mesh being selected, but context.scene.objects.active is None :/ Isn't that weird? $\endgroup$ – Amir Sep 11 '18 at 15:45
  • 1
    $\begingroup$ I suppose. If the obj file has N objects.. which one should be the active one after import? The first, the last the biggest? $\endgroup$ – batFINGER Sep 11 '18 at 16:01
  • $\begingroup$ @batFINGER Yes that makes sense. $\endgroup$ – Amir Sep 11 '18 at 16:04
1
$\begingroup$

Active object is the one on which the actions in 3d view are performed. For instance, if you press tab the mode of only the active object changes. There can, however, be multiple selected objects. Left click an object in outliner to make it active, deselect everything and you will still be able to tab the object into edit mode (as it is active but not selected).

Excuse me if this all is known piece of info but since you said "I tried bpy.ops.object.select_all(action='DESELECT') before importing the new object but it didn't work and the active object is still another object in the scene.", I guess you are thinking that by deselecting an object you would also negate its active state.

So the answer to your question is: The imported object is selected but not active by default. To make it active,

  • deselect all the objects before you do the ops.import_scene.obj and do bpy.ops.object.select_all(action='DESELECT')

  • make the selected object active explicitly. Since the imported object will be the only one selected, you can simply do bpy.context.scene.objects.active = [ob for ob in bpy.data.objects if ob.select == True][0]

  • or better as suggested in the comments do:

    bpy.context.scene.objects.active = bpy.context.selected_objects[0]

$\endgroup$
  • 1
    $\begingroup$ Can you please edit in some code markdown. The last line is IMO not the way to go, as it iterates over all objects in file, whether or not linked to context scene (or any scene). Would suggest using context.selected_objects which is equivalent to [o for o in context.scene.objects if o.select] $\endgroup$ – batFINGER Sep 11 '18 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.