Fixing compiler warning
[blender.git] / source / tests / batch_import.py
index 6a941f3f06cf52ff002366cf19f988563b7b69bd..5c228c014cae29d05ca0c9224ee3f09fc82f56a2 100644 (file)
 
 """
 Example Usage:
-  blender --background --python source/tests/batch_import.py --  --operator="bpy.ops.import_scene.obj" --path="/fe/obj" --match="*.obj" --start=0 --end=10 --save_path=/tmp/test
+
+./blender.bin --background --python source/tests/batch_import.py -- \
+    --operator="bpy.ops.import_scene.obj" \
+    --path="/fe/obj" \
+    --match="*.obj" \
+    --start=0 --end=10 \
+    --save_path=/tmp/test
+
+./blender.bin --background --python source/tests/batch_import.py -- \
+    --operator="bpy.ops.import_scene.autodesk_3ds" \
+    --path="/fe/" \
+    --match="*.3ds" \
+    --start=0 --end=1000 \
+    --save_path=/tmp/test
+
+./blender.bin --background --addons io_curve_svg --python source/tests/batch_import.py -- \
+    --operator="bpy.ops.import_curve.svg" \
+    --path="/usr/" \
+    --match="*.svg" \
+    --start=0 --end=1000 \
+    --save_path=/tmp/test
+
 """
 
 import os
 import sys
 
 
+def clear_scene():
+    import bpy
+    unique_obs = set()
+    for scene in bpy.data.scenes:
+        for obj in scene.objects[:]:
+            scene.objects.unlink(obj)
+            unique_obs.add(obj)
+
+    # remove obdata, for now only worry about the startup scene
+    for bpy_data_iter in (bpy.data.objects, bpy.data.meshes, bpy.data.lamps, bpy.data.cameras):
+        for id_data in bpy_data_iter:
+            bpy_data_iter.remove(id_data)
+
+
 def batch_import(operator="",
                    path="",
                    save_path="",
@@ -34,8 +69,9 @@ def batch_import(operator="",
                    start=0,
                    end=sys.maxsize,
                    ):
+    import addon_utils
+    _reset_all = addon_utils.reset_all  # XXX, hack
 
-    print(list(globals().keys()))
     import fnmatch
 
     path = os.path.normpath(path)
@@ -67,15 +103,29 @@ def batch_import(operator="",
     if len(files) != files_len:
         print(" using a subset in (%d, %d), total %d" % (start, end, len(files)), end="")
 
-    print("")
-
     import bpy
     op = eval(operator)
+
+    tot_done = 0
+    tot_fail = 0
+
     for i, f in enumerate(files):
         print("    %s(filepath=%r) # %d of %d" % (operator, f, i + start, len(files)))
+
+        # hack so loading the new file doesnt undo our loaded addons
+        addon_utils.reset_all = lambda: None  # XXX, hack
+
         bpy.ops.wm.read_factory_settings()
 
-        op(filepath=f)
+        addon_utils.reset_all = _reset_all  # XXX, hack
+        clear_scene()
+
+        result = op(filepath=f)
+
+        if 'FINISHED' in result:
+            tot_done += 1
+        else:
+            tot_fail += 1
 
         if save_path:
             fout = os.path.join(save_path, os.path.relpath(f, path))
@@ -89,6 +139,8 @@ def batch_import(operator="",
 
             bpy.ops.wm.save_as_mainfile(filepath=fout_blend)
 
+    print("finished, done:%d,  fail:%d" % (tot_done, tot_fail))
+
 
 def main():
     import optparse