1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
24 ./blender.bin --background --python tests/python/batch_import.py -- \
25 --operator="bpy.ops.import_scene.obj" \
31 ./blender.bin --background --python tests/python/batch_import.py -- \
32 --operator="bpy.ops.import_scene.autodesk_3ds" \
35 --start=0 --end=1000 \
38 ./blender.bin --background --addons io_curve_svg --python tests/python/batch_import.py -- \
39 --operator="bpy.ops.import_curve.svg" \
42 --start=0 --end=1000 \
60 _reset_all = addon_utils.reset_all # XXX, hack
61 _disable_all = addon_utils.disable_all # XXX, hack
65 path = os.path.normpath(path)
66 path = os.path.abspath(path)
68 match_upper = match.upper()
71 return fnmatch.fnmatchcase(a.upper(), match_upper)
73 def file_generator(path):
74 for dirpath, dirnames, filenames in os.walk(path):
76 dirnames[:] = [d for d in dirnames if not d.startswith(".")]
78 for filename in filenames:
79 if pattern_match(filename):
80 yield os.path.join(dirpath, filename)
82 print("Collecting %r files in %s" % (match, path), end="")
84 files = list(file_generator(path))
85 files_len = len(files)
86 end = min(end, len(files))
87 print(" found %d" % files_len, end="")
90 files = files[start:end]
91 if len(files) != files_len:
92 print(" using a subset in (%d, %d), total %d" % (start, end, len(files)), end="")
100 for i, f in enumerate(files):
101 print(" %s(filepath=%r) # %d of %d" % (operator, f, i + start, len(files)))
103 # hack so loading the new file doesn't undo our loaded addons
104 addon_utils.reset_all = lambda: None # XXX, hack
105 addon_utils.disable_all = lambda: None # XXX, hack
107 bpy.ops.wm.read_factory_settings(use_empty=True)
109 addon_utils.reset_all = _reset_all # XXX, hack
110 addon_utils.disable_all = _disable_all # XXX, hack
112 result = op(filepath=f)
114 if 'FINISHED' in result:
120 fout = os.path.join(save_path, os.path.relpath(f, path))
121 fout_blend = os.path.splitext(fout)[0] + ".blend"
123 print("\tSaving: %r" % fout_blend)
125 fout_dir = os.path.dirname(fout_blend)
126 os.makedirs(fout_dir, exist_ok=True)
128 bpy.ops.wm.save_as_mainfile(filepath=fout_blend)
130 print("finished, done:%d, fail:%d" % (tot_done, tot_fail))
136 # get the args passed to blender after "--", all of which are ignored by blender specifically
137 # so python may receive its own arguments
141 argv = [] # as if no args are passed
143 argv = argv[argv.index("--") + 1:] # get all args after "--"
145 # When --help or no args are given, print this help
146 usage_text = "Run blender in background mode with this script:"
147 usage_text += " blender --background --python " + __file__ + " -- [options]"
149 parser = optparse.OptionParser(usage=usage_text)
151 # Example background utility, add some text and renders or saves it (with options)
152 # Possible types are: string, int, long, choice, float and complex.
153 parser.add_option("-o", "--operator", dest="operator", help="This text will be used to render an image", type="string")
154 parser.add_option("-p", "--path", dest="path", help="Path to use for searching for files", type='string')
155 parser.add_option("-m", "--match", dest="match", help="Wildcard to match filename", type="string")
156 parser.add_option("-s", "--save_path", dest="save_path", help="Save the input file to a blend file in a new location", metavar='string')
157 parser.add_option("-S", "--start", dest="start", help="From collected files, start with this index", metavar='int')
158 parser.add_option("-E", "--end", dest="end", help="From collected files, end with this index", metavar='int')
160 options, args = parser.parse_args(argv) # In this example we wont use the args
166 if not options.operator:
167 print("Error: --operator=\"some string\" argument not given, aborting.")
171 if options.start is None:
174 if options.end is None:
175 options.end = sys.maxsize
177 # Run the example function
178 batch_import(operator=options.operator,
180 save_path=options.save_path,
182 start=int(options.start),
183 end=int(options.end),
186 print("batch job finished, exiting")
189 if __name__ == "__main__":