I have been looking at similar questions yet the following script is not returning any error but the origin of the object is not changed. What did I not set right? The context seems ok since I have selected and activated the object first:

imported_object = bpy.ops.import_scene.obj(filepath = the_file_path)
bpy.data.objects['Blob'].select = True

the_object = [ o for o in bpy.context.scene.objects if o.select ][0]
bpy.context.scene.objects.active = the_object


there is only one active and selected object which is 'Blob'. Here is a picture of the result:

Here is a link with the imported file : https://gist.github.com/compmonks/dd997dc53bb0dcd48de2fe077866266f


This script is loaded with Blender on the command line so there is no direct access to context areas and so on. This causes some operators to not function properly. I found that loading blender with a blend file and the script was avoiding this problem. Not sure if this is the correct way to do so :

import os
  • 2
    $\begingroup$ The active object is generally context.selected_objects[0] always context.active_object In your script above, if an object is imported named other than "Blob" the_object may or may not be "Blob". The set origin operator operates on all context.selected_objects. If you want to only set origin to geom on one object post export, deselect all, select one, make it active, call operator. $\endgroup$ – batFINGER Jul 19 '18 at 21:08
  • $\begingroup$ Thanks for your answer. But even if another object was selected, no object is having its origin set to the geometry. The current context mode is OBJECT but I am wondering if this needs some kind of override $\endgroup$ – compmonks Jul 20 '18 at 13:25
  • $\begingroup$ I edited the question with a picture. There is only the one mesh object returned as active and selected $\endgroup$ – compmonks Jul 20 '18 at 14:06
  • $\begingroup$ If only one object in file, Code could be a two line script, the two operators. Can you provide a link to the obj file too please. $\endgroup$ – batFINGER Jul 20 '18 at 14:27
  • $\begingroup$ just added a link in the question. The blender scene in which it is imported already contains objects. That's why I am making sure it is the only one selected and active first $\endgroup$ – compmonks Jul 20 '18 at 14:37

Your Answer

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

Browse other questions tagged or ask your own question.