4
$\begingroup$

I am trying to create a list from a directory and import each item with a for loop:

file_list = sorted(os.listdir(path_to_3ds))
autodesk_list = [item for item in file_list if item.endswith('.fbx')]

for item in autodesk_list:
    path_to_files = os.path.join(path_to_3ds, item)
    bpy.ops.import_scene.fbx(filepath = path_to_files)

How can I get the object name of the imported item and export it as object name + ' export'?

$\endgroup$
4
$\begingroup$

You can use Python set operations.

  • Make a set from all objects in the scene before the import
  • Perform the import
  • Make another set from all objects and subtract the set you made earlier

The only objects left in the set are the ones that were imported.

old_objs = set(context.scene.objects)
bpy.ops.import_scene.fbx(filepath="/home/simon/untitled.fbx")
imported_objs = set(context.scene.objects) - old_objs

print("Imported:", imported_objs)

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ @batFINGER agreed, I changed it. $\endgroup$ – B.Y.O.B. May 9 '18 at 16:47
2
$\begingroup$

When calling any import operator (fbx, obj or something), all imported objects are going to be selected (as you may already noticed). To get that selection, you can use bpy.context.selected_objects:

>>> bpy.ops.import_scene.fbx(filepath="/path/to/file/scene-01.fbx")
>>> [o.name for o in bpy.context.selected_objects]
['imported_obj.001', 'imported_obj.002', 'imported_obj.003' ...]
...

Another way would be an iteration through all objects to check their actual select state: imported_objects = [o for o in bpy.context.scene.objects if o.select].


In case of importing multiple files, you can just add all the new names to a list (initialized before the loop) since all object names in blender are unique by design:

import bpy

files = (
    "/path/to/file/scene-01.fbx", 
    "/path/to/file/scene-02.fbx",
    "/path/to/file/scene-03.fbx"
    )

imported_objects = []
for f in files:
    bpy.ops.import_scene.fbx(filepath=f)

    # Object names
    imported_objects += [o.name for o in bpy.context.selected_objects]

    # Object data
    #imported_objects += bpy.context.selected_objects[:]

print (imported_objects)
| improve this answer | |
$\endgroup$

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.