3
$\begingroup$

I'm hoping this question builds on a previous question, Batch import Wavefront OBJ.

I'd like to batch import a bunch of .3ds files and export each of them to their own .js file (using the Three.js file importer/exporter addon). Is there a way to do this in Python? If so, how would I do this? I have 77 files to convert, so it wouldn't kill me to do it by hand, but I'd rather not.

Or should I find a non-Blender solution? There is an online tool called Online 3D Model Converter. Unfortunately, the exports from this tool are not nearly as clean and small as those from Blender.

$\endgroup$
2
  • $\begingroup$ It it feasible with Python, but what is actual question? $\endgroup$
    – CodeManX
    Commented Oct 15, 2014 at 17:01
  • $\begingroup$ @CoDEmanX Sorry. I updated it with the actual question. $\endgroup$
    – Justin
    Commented Oct 15, 2014 at 17:02

2 Answers 2

3
$\begingroup$

Here is an outline:

  • Make sure 3DS importer and Three.js exporter are installed and enabled

    # test
    hasattr(bpy.types, "EXPORT_OT_threejs")
    
    # enable
    try:
        bpy.ops.wm.addon_enable(module="threejs")
    except ImportError:
        print("Threejs importer addon not available")
    
  • Loop over all the files and perform below actions for every single one,
    you may use glob.glob() or os.walk() unless you want to select the files via UI
    (see the Operator Import tempate, you will need to provide
    files = bpy.props.CollectionProperty(type=bpy.types.PropertyGroup), docs)

  • Clear scene if desired, e.g.

     bpy.ops.object.mode_set(mode='OBJECT')
     bpy.ops.object.select_all(action='SELECT')
     bpy.ops.object.delete()
    
  • Import 3DS, basically call the operator bpy.ops.import_scene.autodesk_3ds(filepath="the_path_to_file.3ds")

  • The importer should select the imported files, but there will usually not be an active object. To run operations on the imported data or for some exporters you might need to set one object active (or process each individually and make one active after another).

  • Export to JS, basically call the operator bpy.ops.export.threejs(filepath="target_file.js", **kwargs),
    kwargs stands for all additional arguments you may wanna give to the export operator.

$\endgroup$
2
  • $\begingroup$ I am importing many .obj files sequentially. Very quickly, the import time increases significantly to the point that it takes minutes for each object (even the ones with low-complexity) to get imported. Could you please take a look at my question here and see if you can offer a solution? $\endgroup$
    – Amir
    Commented Mar 4, 2018 at 20:59
  • $\begingroup$ It's probably caused by scene updates: blender.stackexchange.com/a/7360/1363 If possible, try to load the import and export addons as modules and call their functions or methods directly to avoid the operator interface. $\endgroup$
    – CodeManX
    Commented Mar 6, 2018 at 11:21
1
$\begingroup$

Here is an example I typed up from an OBJ batch import script. Add this into your Text Editor and hit Run Script.

It works perfect with 2.69.

import os
import bpy


# path_to_3ds should be the folder where all your sub directories are for your 3ds files
# main_dir is the folder where the sub directory folders that actually contain your 3ds files are.
# sub_dir is the actual folder that contains all the 3ds files.

# Example: path_to_3ds = os.path.join('C:\\users\joe\desktop\Main Folder', 'Folder 1')
# Make sure your Main_dir has "C:\\" this needs two "\" or it wont work.
# if you have divided all your files into seperate sub directories, you will need to import each folder seperately. 
# Ex: C:\\users\joe\desktop\Main Folder', 'Folder 1') - Import then,
# C:\\users\joe\desktop\Main Folder', 'Folder 2') - Import then, 
# C:\\users\joe\desktop\Main Folder', 'Folder 3') and so on...

path_to_3ds = os.path.join('main_dir', 'sub_dir')


file_list = sorted(os.listdir(path_to_3ds))


autodesk_list = [item for item in file_list if item.endswith('.3ds')]

for item in autodesk_list:
   path_to_files = os.path.join(path_to_3ds, item)
   bpy.ops.import_scene.autodesk_3ds(filepath = path_to_files)

Hope this helps :^)

$\endgroup$

You must log in to answer this question.

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