Now Ihave manage to do this,
But the save command is not working, anyone that can tell me how it should be,
(The script imports obj from folder, then save a new blend file (not working) then deletes all imported parts)
import os
import bpy
# put the location to the folder where the objs are located here in this fashion
# path_to_obj_dir = os.path.join('C:\\', 'Users', 'YOUR_NAME', 'Desktop', 'OBJS')
path_to_obj_dir = os.path.join('C:\Temp\obj')
## get list of all files in directory
file_list = sorted(os.listdir(path_to_obj_dir))
# get a list of files ending in 'obj'
obj_list = [item for item in file_list if item.endswith('.obj')]
# loop through the strings in obj_list and add the files to the scene
for item in obj_list:
path_to_file = os.path.join(path_to_obj_dir, item)
bpy.ops.import_scene.obj(filepath = path_to_file)
bpy.ops.wm.save_as_mainfile(filepath="C:\Temp\obj est") # This is not working what should it bee instead?
bpy.ops.object.delete(use_global=False)
The problem is the filepath is using backslash characters, which are interpreted as escape characters in python. To fix this, replace all of your ‘’ with ‘\’ in your filepaths. For example:
import os
import bpy
# put the location to the folder where the objs are located here in this fashion
# path_to_obj_dir = os.path.join('C:\\', 'Users', 'YOUR_NAME', 'Desktop', 'OBJS')
path_to_obj_dir = os.path.join('C:\Temp\obj')
## get list of all files in directory
file_list = sorted(os.listdir(path_to_obj_dir))
# get a list of files ending in 'obj'
obj_list = [item for item in file_list if item.endswith('.obj')]
# loop through the strings in obj_list and add the files to the scene
for item in obj_list:
path_to_file = os.path.join(path_to_obj_dir, item)
bpy.ops.import_scene.obj(filepath = path_to_file)
scene_file_path = os.path.join(path_to_obj_dir, item) # Added this
bpy.ops.wm.save_as_mainfile(filepath=scene_file_path)
bpy.ops.object.delete(use_global=False)
But the problem with this is that since the .obj file has it filename “152187.asm.obj” the blend file also get the same name, and I want it to be 152187.blend instead of 152187.asm.obj. So I need to change the “scene_file_path” and exlude “.asm.obj” and replace it with .blend. The best would be to only take the first 6 numbers and remove the rest since it sometimes the .obj do not have got .asm in the fielname.