obj import and then save to blend file

Hi,

I need a script that does this,

I am wondering if anyone can change this so it work likes this:

  1. You select batch import .obj
  2. Select a couple of .obj files
  3. The first obj is imported and then saved as it own .blend file with the same name as the imported .obj
  4. The imported obj material is removed
  5. The script takes the next .obj imports it and saves it as it own .blend file

The above is repeated to all .obj files are imported and then saved as separate .blend files.

//Wilnix

1 Like

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)

//Wilnix

1 Like

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:


bpy.ops.wm.save_as_mainfile(filepath="C:\\Temp\\obj\	est")

1 Like

Thanks for your input doakey3,

So now I have Changes it to this:


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.

Any know how to do this?

//Wilnix

2 Likes

you can use several methods:

Method 1: split the string by periods

obj_name = '152187.asm.obj'
blend_name = obj_name.split('.')[0] + '.blend'

Method 2: Use os.path.splitext (this will not get rid of the ‘asm’ though)

import os
obj_name = '152187.asm.obj'
blend_name = os.path.splitext(obj_name)[0] + '.blend'

Method 3: Use the first 6 characters of the obj name

obj_name = '152187.asm.obj'
blend_name = obj_name[0:6] + '.blend'
1 Like

The last code example works great.
Thank you very much for your help.

/Wilnix

1 Like