8
$\begingroup$

I have about thousands of files I need to convert into from one format to another. Both formats are handled by existing add-ons..

I assume the script process would go something like this:

  • Select every source file in a directory and it's sub-directories
  • Store the path and filename of the source format.
  • Delete everything in scene and import the file
  • Export the file to the destination format with the same path and the same filename as the original file, with a different extension.
  • move on to the next item

The problem is that I have very little experience with Blender, and Python is not my area. The manual solution of importing and exporting works perfectly fine, as there is no need for anything to be modified.

$\endgroup$
3
  • $\begingroup$ what are using to import .meshdata ? $\endgroup$
    – Chebhou
    Commented Jul 25, 2015 at 21:34
  • $\begingroup$ Was wondering if you could point me in the direction regarding batch exporting since you found good success in the endeavor. I am looking to importing a whole bunch of FBX file then save them individually into blender format. I have looked over the stuff posted here but did not really understand how it all fit together. Is there a add-on plugin that you wrote yourself or packages you could share so that I can edit them to the fix the files I needed to convert Thanks for your time. $\endgroup$
    – hawkenfox
    Commented Dec 26, 2015 at 6:08
  • $\begingroup$ gist.github.com/HsiaTsing/31361ea018637aa1781c#file-ply2obj-py (This code uses the Blender Python API to batch convert 3D model file with ''ply'' format to ''obj'' format. It is only tested on Blender 2.64.) Also works on Blender 3.1.0. Easy to change I/O formates. $\endgroup$ Commented Dec 2, 2022 at 21:48

2 Answers 2

6
$\begingroup$

Set the path variable and change the import line to whatever importer you are using :

import bpy
import os

path = 'C:/path/to/files/'  # set this path

for root, dirs, files in os.walk(path):
    for f in files:
        if f.endswith('.meshdata') :
            mesh_file = os.path.join(path, f)
            obj_file = os.path.splitext(mesh_file)[0] + ".obj"

            bpy.ops.object.select_all(action='SELECT')
            bpy.ops.object.delete()

            bpy.ops.import_scene.obj(filepath=mesh_file) # change this line

            bpy.ops.object.select_all(action='SELECT')

            bpy.ops.export_scene.obj(filepath=obj_file)
$\endgroup$
7
  • $\begingroup$ Although I have one issue. I can't seem to find the correct importer function. Here's the init.py: pastebin.com/G980EA2h and here's the meshdata_import.py: pastebin.com/bKkWiVMD Here's a meshdata file: dump.no/files/78e43d0cb895/… the error occurs on this line: bpy.ops.object.mode_set(mode='OBJECT') With the error: RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect. Disabling everything bellow the import line causes no errors, but also no imported files. Could you have a look? $\endgroup$ Commented Jul 26, 2015 at 14:31
  • $\begingroup$ Okay, it now successfully imports all the objects, but it's still getting the "RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect" error. caused by "bpy.ops.object.mode_set(mode='OBJECT')" $\endgroup$ Commented Jul 26, 2015 at 15:08
  • $\begingroup$ @MatsBakken ( sorry I was away ) can you just comment this line and try again (it's not that crucial ) $\endgroup$
    – Chebhou
    Commented Jul 26, 2015 at 15:41
  • $\begingroup$ Wow, I didn't actually think of that. It works now! Thank you! Although it only does the specified directory and not it's sub directories. I guess I should read up on python. $\endgroup$ Commented Jul 26, 2015 at 15:57
  • 1
    $\begingroup$ this works, but when you import a lot of meshes, blender will crash after a while because it runs out of memory. This mainly happens because textures, materials and the like are not dereferenced when you delete the object. To fix this issue I used the answer by Amir found here: blender.stackexchange.com/questions/48836/… Basically you need to call bpy.data.materials.remove(block), bpy.data.textures.remove(block) and so on, in every iteration of the for-loop EDIT: old thread, but answer is still valid. I'm on Blender 3.6.2 $\endgroup$ Commented Oct 19, 2023 at 6:42
3
$\begingroup$

This script is an example of how you can loop over all files and convert from one format to another.

This example converts all .obj files to .x3d, but changing the formats is trivial.

CONVERT_DIR = "/my/test/directory"

import os

def file_iter(path, ext):
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            ext = os.path.splitext(filename)[1]
            if ext.lower().endswith(ext):
                yield os.path.join(dirpath, filename)

import bpy


def reset_blend():
    bpy.ops.wm.read_factory_settings(use_empty=True)

def convert_recursive(base_path):
    for filepath_src in file_iter(base_path, ".obj"):
        filepath_dst = os.path.splitext(filepath_src)[0] + ".x3d"

        print("Converting %r -> %r" % (filepath_src, filepath_dst))

        reset_blend()

        bpy.ops.import_scene.obj(filepath=filepath_src)
        bpy.ops.export_scene.x3d(filepath=filepath_dst)


if __name__ == "__main__":
    convert_recursive(CONVERT_DIR)
$\endgroup$
1
  • $\begingroup$ I'm getting this error when I use this script: TypeError: Converting py args to operator properties: : keyword "use_empty" unrecognized $\endgroup$ Commented Jul 30, 2017 at 5:07

You must log in to answer this question.

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