3
$\begingroup$

This question already has an answer here:

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$

marked as duplicate by p2or, Ray Mairlot, Denis, batFINGER, Shady Puck Feb 5 '17 at 23:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 Feb 5 '17 at 10:21
  • $\begingroup$ also related: blender.stackexchange.com/questions/5064/… $\endgroup$ – p2or Feb 5 '17 at 14:47
2
$\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$
  • $\begingroup$ Thanks to Tak and also Batfinger above. It was a combination of these two. $\endgroup$ – J Rowoldt Feb 5 '17 at 20:41

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