Can I automate Blender importing and rendering large numbers of files?

I’ve been searching for information about using Blender to render imported scenes from the command line with no manual GUI interaction. I’ve found perhaps half a dozen examples of commands similar to “for f in *.blend ; do blender -b $f -F PNG -o //$f -a ; done”. I would like to do something rather similar, but with scenes imported in some portable stable format, perhaps X3D, rather than scenes described by .blend files created by Blender. (I’m thinking of creating sizable numbers of such scenes using external software which is not written in Python; as far as I can tell, the .blend format is not particularly suitable for software which is not tightly coupled with Blender.)

I have successfully done a similar import-then-render process manually in the Blender GUI, importing an X3D file (not created by Blender) and rendering it. But I have made no progress in finding out how to tell Blender to do this automatically a few dozen or hundred times in a row.

(I have found how to do this kind of automated standalone rendering with various other rendering systems that are designed for standalone use, but I’d prefer to do it with Blender so that I can easily mix hand-created Blender scene information with my procedurally-generated imported scene information.)

(Ideally, I’d prefer a solution that works both on current Blender and on the Blender 2.49b that I have on my old Debian installation. But a solution for only one would be much better than nothing.)

Just run the blender executable with the parameter “-h” and it will show you all all the params you can pass it. Usually you pass it the name of a blend file, which frame to render, and to what format, etc.

But you don’t want to load a blend. Instead look for “-P” where you can send it a python script name instead. Also notice the “–” option which tells it to pass the rest of the params unchanged (so you can read them from python). So you just have to write a python script that looks at the your custom params specifying your X3D (or whatever), imports it, then renders an image. Once you have it going with a single one it will be pretty trivial to create a batch file that does it with an unlimited number.

Harley wrote “you just have to write a python script that looks at the your custom params specifying your X3D (or whatever), imports it […]” Thank you; I have gone back to work on the “imports it” step.

Both before and after posting here I got nowhere on the “imports it” step using the Blender Python API docs. The Blender Python API seems to support implementing import operations in terms of low level abstractions (e.g. operating on a mesh) as in http://vrm.ao2.it/ and http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Advanced_Tutorials/Python_Scripting/Import_scripts but it does not seem to support executing Blender’s high level commands without GUI input. I don’t really want to implement X3D import in terms of low level abstractions: the implementation has already been done inside Blender, and I’d prefer to reuse that.

After posting here, I did some more searching and found that a question similar to mine was asked and answered at http://blenderartists.org/forum/archive/index.php/t-159500.html . I have now adapted that answer to something that seems to work for me. I’ll summarize my adaptation here in hopes that it might help someone else with a similar question someday, as the summary answer from “harwen” has helped me.

The method “harwen” used to invoke Blender’s own import-it functionality (which is the method I use too) does not seem to be documented anywhere. It’s probably not supported either, so beware.

Also beware that 2.49b import_web3d imports only a rather smaller subset of VRML than the documentation at http://wiki.blender.org/index.php/Extensions:2.4/Py/Scripts/Import/X3D_VRML97 might make you think. (Smaller than it made me think…) You should probably discount text like “this importer should be able to load scenes generated from other 3D software” and skip to the listed subset of things that it actually does support. E.g., it’s not going import Extrusion for you, or anything with 2D in the name. Then perhaps discount that list further because IndexedFaceSet, its way of importing mesh-y data, seems not to work reliably. (I was unable to get it to import triangle faces except by duplicating a vertex of each triangle face so that it thinks it is a rectangle face, which I had already discovered it can import.)

Anyway. If that doesn’t discourage you too much, then here’s how I was able to get Blender 2.49b on the Linux command line to render a recognizable subset of a car.wrl VRML demo which I found on the net, apparently using the reader which is built into Blender and certainly without having to implement a reader myself:

  1. Write four lines of Python into a script autoimport-x3d.py:
    import sys
    sys.path.append(r’/usr/share/blender/scripts/blender/’)
    import import_web3d
    import_web3d.load_web3d(‘car.wrl’)
    (For all I know, your internal Blender scripts might not be installed in the same place mine are installed, in which case my sys.path.append command won’t work for you. On a Unix system you can probably find the scripts quickly using the command ‘locate import_web3d’; then adjust your sys.path.append command accordingly.)

  2. Interactively run Blender to set up camera angles and lighting and so forth which will be suitable for rendering your imported scenes. Save the Blender session as stage.blend.

  3. Run blender from the command line as
    blender -b stage.blend -P autoimport-x3d.py -o //car -F JPEG -x 1 -f 1
    et voila (?): for me, at least, this renders the car.wrl scene into
    car0001.jpg with no GUI mousing and clicking required.