Importing 3DS files with Python

I have found some indications in the internet about how to make it work, but unfortunately I never found a solid copy-paste solution. Very strange that this has been asked over and over again since 2011 and no one tried it to solve it. Since I know that people will need this I will post here the answer so anyone interested could use it as a reference.

Currently I need only to import 3DS files, perhaps if in the future I will need to load any other format I will respond back and type the solution. But the deal is that the methodology will be very similar (having a peek at the io_scene folders and looking for the load or import function, then changing the parameters a bit).

In the Python Console


from io_scene_3ds.import_3ds import load_3ds

How does it work: I will locate this Python file
C:\Programs\Blender\2.74\scripts\addons\io_scene_3ds\import_3ds.py
and from it will import the load_3ds function which does all the work.

Now in order to import the 3DS file this command can be entered

load_3ds("D:/themodel.3ds", bpy.context)

The first argument is a filename for the model and the second is the context which the operator will run, in this case it can be the default context of Blender.

And that’s it.


from io_scene_3ds.import_3ds import load_3ds
load_3ds("D:/themodel.3ds", bpy.context)

Bonus Stage: If you want to import multiple models at once


from io_scene_3ds.import_3ds import load_3ds
models = ["D:/model1.3ds", "D:/model2.3ds"]
for i in models: load_3ds(i, bpy.context)

You could also use the operator, but of course you need to check whether it is actually available. The operator might do selection changes, and it will add to the undo stack.

I wanted also to do it this way but it did not work out of the box


>>> bpy.ops.import_scene.autodesk_3ds("F:\model.3ds");


Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "C:\Programs\Blender\2.74\scripts\modules\bpy\ops.py", line 187, in __call__
    ret = op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
TypeError: Calling operator "bpy.ops.import_scene.autodesk_3ds" error, expected a string enum in ('INVOKE_DEFAULT', 'INVOKE_REGION_WIN', 'INVOKE_REGION_CHANNELS', 'INVOKE_REGION_PREVIEW', 'INVOKE_AREA', 'INVOKE_SCREEN', 'EXEC_DEFAULT', 'EXEC_REGION_WIN', 'EXEC_REGION_CHANNELS', 'EXEC_REGION_PREVIEW', 'EXEC_AREA', 'EXEC_SCREEN')

well, operator parameters are keyword arguments, you can’t pass them as positional args. Furthermore, backslashes need to be escaped (or use raw strings) and ; are pretty unnecessary in python.

Try:

bpy.ops.import_scene.autodesk_3ds(filepath=r"F:\model.3ds")