1
$\begingroup$

I want to check all the subfolder in the selected folder in the blender and import the fbx in it. And I want to apply the file name of the imported model to the obj example: file name: Model.fbx but in fbx object is cube. By default, blender applies this name to the imported object: cube. But I want the name of the imported object must be Model. I think I could explain.

Import fbx from all subfolders. Apply its own file name for each model

my try:

def importer(path, name):
    bpy.ops.import_scene.fbx(filepath=path)
    for obj in bpy.context.selected_objects:
        obj.name = name

def dirScan(dirs, path):
    for c in dirs:
        if os.path.isdir(path+c):
            tempd = os.listdir(path+c+'/')
            for c1 in tempd:
                if os.path.isfile(path+c+'/'+c1):
                    if os.path.splitext(c1)[1] == ".fbx":
                       importer(path+c+'/'+c1, os.path.splitext(c1)[0])
                else:
                    dirScan(os.listdir(path+c+'/'+c1))
        else:
            if os.path.splitext(c)[1] == ".fbx":
                        importer(path+c, os.path.splitext(c)[0])
$\endgroup$
2
  • $\begingroup$ and what did you try so far? $\endgroup$
    – Chris
    Commented Mar 6, 2022 at 9:23
  • $\begingroup$ I wrote the code manually but it is not working as I expected codeshare.io/dwo9BK $\endgroup$ Commented Mar 6, 2022 at 11:51

1 Answer 1

1
$\begingroup$

You can recursively get all the files with a certain extension with Path.glob. Get the file name with Path.stem.

from pathlib import Path
import bpy

folder = Path(r"C:\path\to\your\folder")
fbx_files = [f for f in folder.glob("**/*.fbx") if f.is_file()]
for fbx_file in fbx_files:
    bpy.ops.object.select_all(action='DESELECT')
    bpy.ops.import_scene.fbx(filepath=str(fbx_file))
    for obj in bpy.context.selected_objects:
        obj.name = fbx_file.stem
$\endgroup$
1
  • $\begingroup$ It worked great. Thank you. I didn’t know Path.glob before $\endgroup$ Commented Mar 6, 2022 at 18:04

You must log in to answer this question.

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