【Blender】How to import multiple 3D models at once

wp_tmb_blender-import

This is where the title came from.

Motive

Just like I edit various materials (jpg and png) using layers in Photoshop, I want to edit multiple objects while comparing them in Blender!

So I decided to import a lot of 3D models into Blender…

Worries

Only .blend files can be imported using drag and drop in Blender, and the only option is to import them one by one. In other words, it is inconvenient that you cannot import multiple 3D models. . .

However, I found a way to get rid of this problem.

That’s how you do it with a python script.

This is different from normal 3D model import.

Blender does not allow drag-and-drop import of 3D models, so this is an alternative method. Depending on how you use it, you can import it efficiently and save time.

f:id:koshishirai:20200511074528p:plain:w400 f:id:koshishirai:20200511074542p:plain:w500
Normal import import with python

Now, let’s start by introducing how to import multiple 3D models!

Operating Environment

  • macOS Mojave or Windows 10 Pro 1909
    • It can be run on both OSes, but please be careful in one place.
  • Blender 2.79~2.83 , 2.90~2.91
    • I confirmed that it works properly.
  • programming knowledge
    • If you have programming experience, you may understand this.

Code used and explanation

The method of importing multiple 3D models is achieved by running it in python.
Even if you have zero python knowledge, you can run it by following the steps.

import os
import bpy

#ex. path_to_obj_dir = os.path.join("C:\\Users\\Your Name\\Dropbox\\3d") #Windows
obj_path = bpy.path.abspath('絶対PATH') #Mac

# ディレクトリ内のすべてのファイルのリストを取得します
file_list = sorted(os.listdir(obj_path))

#「obj」で終わるファイルのリストを取得します
obj_list = [item for item in file_list if item[-3:] == 'obj'] # obj, fbx, vrm, etc...

# obj_listの文字列をループし、ファイルをシーンに追加します
for item in obj_list:
    path_to_file = os.path.join(obj_path, item)
    bpy.ops.import_scene.obj(filepath = path_to_file) ## obj, fbx, vrm, etc...

Now, I will explain the code to be executed in order.

1st, 2nd line

import os  # OSに依存した機能を使用します。
import bpy #  bpyモジュールを使用します。

Lines 4 and 5 – For absolute PATH, specify the directory where the 3D file is located.

  • Libraries are different for Windows and Mac. Please comment out one of them and use it.
path_to_obj_dir = os.path.join("C:\\Users\\Your Name\\3dファイルがあるディレクトリ")  #Windows
obj_path = bpy.path.abspath('絶対PATH') #Mac

Lines 7 and 8

# ディレクトリ内のすべてのファイルのリストを取得します。
file_list = sorted(os.listdir(obj_path))

Lines 10 and 11 In addition to obj files, fbx and vrm can also be applied. In that case, please replace the string.

#「obj」で終わるファイルのリストを取得します
obj_list = [item for item in file_list if item[-3:] == 'obj'] # obj, fbx, vrm, etc...

Lines 13-16

# obj_listの文字列をループし、ファイルをシーンに追加します
for item in obj_list:
path_to_file = os.path.join(obj_path, item)
bpy.ops.import_scene.obj(filepath = path_to_file)

Execution steps

Create the same directory obj2blender.py where your 3D model is.

Start Blender, select “Scripting” from the tab above, and open obj2blender.py

f:id:koshishirai:20200511074650p:plain:w500

Run the script obj2blender.py.

f:id:koshishirai:20200511074741p:plain

If the execution is successful, multiple 3D models will be successfully imported. All objects are set to the origin as shown in the image. (Even if the 3D model in the 3D file moves away from the origin position)

f:id:koshishirai:20200511074820p:plain

The number of 3D files in the directory will be imported into the scene.
Since 3D files are often set at the origin, you will find that they often overlap depending on the scale and transformation of the model. If you wish to continue importing, we recommend that you move the model from its origin and create another new scene collection before running the script.

Merit and Demerit

We will show you the advantages and disadvantages of this method.

Merit

  • You can import large numbers of 3D files at once.
  • You can continue importing.

Demerit

  • The .blend file size will increase depending on the file size and number of 3D models.
  • Blender will be overloaded all at once.
  • Loading multiple models will gradually make Blender slower.
  • Errors often occur.

Measures to take if an error occurs

f:id:koshishirai:20200511074844p:plain

Error Message

location: :-1 SyntaxError: (unicode error) \’unicodeescape\’ codec can\’t decode bytes in position 2-3: truncated \\UXXXXXXXX escape File \\obj2blender.py, line 6 Python script failed. Check system console messages

Japanese Translation

Location::-1 Syntax error: (Unicode error) \’unicodeescape\’ codec cannot decode byte in position 2-3: truncated\\UXXXXXXXX escape file \\obj2blender.py, line 6

Meaning: Syntax error. Backslash →\\ is disabled.

Solution: Type backslash twice to make →\\, or add r before C.

path_to_obj_dir = os.path.join("C:\Users\koshi\Dropbox\3d") #Windows
or
path_to_obj_dir = os.path.join(r"C:\Users\koshi\Dropbox\3d") #Windows

Import support file format

Verified in this environment. (Currently 2020/4/15) When I tried it with Blender 2.79 to 2.82, the file was imported with the same behavior in both cases.
We also confirmed that this method can be used by introducing an addon (extension) that allows importing 3D files that are not supported by Blender.

Addon example:.vrm(VRM IMPORTER)

3D file import correspondence table by executing python script

Can be imported fbx obj vrm(addon:VRM IMPORTER) , vpd
Can’t import dae skp pmx(addon: mmd_tools) 3ds stl glTF
Nothing changed blend

Everyone, please try it with no fuss.

Lastly

  • Personally, I modify a lot of models, so this is a great method.
  • On the other hand, 3D modeling software other than Blender, such as Maya and Substance Painter, allows you to import 3D files using drag and drop.
  • Was there a method like this? Just knowing that was good. Thanks to you, I was able to learn python.

As a side note, future challenges include the following:

  • Improve the code to support other 3D file formats that Blender supports by default. (such as .blend.dae)
  • Setting the coordinate axes: Place it slightly offset from the origin.

If I can implement these functions, the range of imports will expand, but I don’t have much knowledge of python, so I’ll update it if I can improve it!

Reference

[1] import – Drag and drop importing of .OBJ files – Blender Stack Exchange

This article is participating in the 4th day of Blender Advent Calendar 2019 – Qiita.

3D model used: ©Project RAIV, Niconi Solid-chan