code cleanup: unused macros
[blender.git] / source / tests / batch_import.py
index 29b6bb8b9aaf671b655a9b0853a28c06d02aa016..a2c5fb5905566c22e6fbd8bdb2483574dc85827c 100644 (file)
@@ -34,11 +34,20 @@ Example Usage:
     --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()
@@ -54,14 +63,15 @@ def clear_scene():
 
 
 def batch_import(operator="",
-                   path="",
-                   save_path="",
-                   match="",
-                   start=0,
-                   end=sys.maxsize,
-                   ):
-
-    print(list(globals().keys()))
+                 path="",
+                 save_path="",
+                 match="",
+                 start=0,
+                 end=sys.maxsize,
+                 ):
+    import addon_utils
+    _reset_all = addon_utils.reset_all  # XXX, hack
+
     import fnmatch
 
     path = os.path.normpath(path)
@@ -93,16 +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 doesn't undo our loaded addons
+        addon_utils.reset_all = lambda: None  # XXX, hack
+
         bpy.ops.wm.read_factory_settings()
+
+        addon_utils.reset_all = _reset_all  # XXX, hack
         clear_scene()
 
-        op(filepath=f)
+        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))
@@ -111,11 +134,12 @@ def batch_import(operator="",
             print("\tSaving: %r" % fout_blend)
 
             fout_dir = os.path.dirname(fout_blend)
-            if not os.path.exists(fout_dir):
-                os.makedirs(fout_dir)
+            os.makedirs(fout_dir, exist_ok=True)
 
             bpy.ops.wm.save_as_mainfile(filepath=fout_blend)
 
+    print("finished, done:%d,  fail:%d" % (tot_done, tot_fail))
+
 
 def main():
     import optparse