4
$\begingroup$

So is importing .obj in 2.78 via script broken, or am I not doing it right? It's crashing on import in my script when I try and reference the object at all.

import bpy
file_loc = 'C:\\Users\\MyComp\\Documents\\3Dobjects\\obj\\humans\\human_figure_JOINED.obj'
imported_object = bpy.ops.import_scene.obj(filepath=file_loc)
obj_object = bpy.context.object
#print('Imported name', imported_object.name)
print('Imported name: ', obj_object.name)

Running this with the first print statement yields this:

AttributeError: 'set' object has no attribute 'name'

If I let it run as shown, the second print gives me an object "Point.001" which is I think the last created object, so the active one?

I have tried to link the imported object to the scene by adding the following code, but this doesn't seem to work:

scn = bpy.context.scene
scn.objects.link(imported_object)

This gives me an error:

TypeError: SceneObjects.link(): error with argument 1, "object" -  Function.object expected a Object type, not set

Appears to me to be something wrong with the import. If I import via the UI (File..Import...Wavefront(.obj) everything seems ok, as in it imports and can be manipulated.

Any ideas?

I would select it referencing the name but I can't even get the name because the reference getting it from "imported object" fails.

$\endgroup$
2
  • 2
    $\begingroup$ Operators return a result set, eg {'FINISHED'} or {'CANCELLED'} not an object. Usually an operator, if run successfully, will change the appropriate context property. Scene imports often import multiple objects, and can be ref'd after using for o in context.selected_objects: or similar. $\endgroup$
    – batFINGER
    Commented Feb 5, 2017 at 10:21
  • $\begingroup$ also related: blender.stackexchange.com/questions/5064/… $\endgroup$
    – p2or
    Commented Feb 5, 2017 at 14:47

1 Answer 1

10
$\begingroup$

You need to get the context selected object as shown below:

import bpy
file_loc = 'C:\\Users\\MyComp\\Documents\\3Dobjects\\obj\\humans\\human_figure_JOINED.obj'
imported_object = bpy.ops.import_scene.obj(filepath=file_loc)
obj_object = bpy.context.selected_objects[0] ####<--Fix
print('Imported name: ', obj_object.name)
$\endgroup$
2
  • $\begingroup$ Thanks to Tak and also Batfinger above. It was a combination of these two. $\endgroup$
    – J Rowoldt
    Commented Feb 5, 2017 at 20:41
  • $\begingroup$ In case there is already an object selected, it seems to be appended to the list of selected object. In my case, I changed the 4th line to obj_object = bpy.context.selected_objects[-1] to be on the safe side. $\endgroup$
    – azt
    Commented Feb 6, 2021 at 15:00

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