Background scripts and context (blender 2.62)

Hi all,

I am not a big blender user, but I want to use it for 3D-visualization purposes.

I am trying to run the following script


import bpy

scenes=['file01.blend','file02.blend']

for i in steps:
    frame_num = i
    for s in scenes:
        bpy.ops.wm.open_mainfile(filepath=s)
        my_obj = bpy.data.objects['MyObj']
        for x in bpy.data.objects:
            x.select=False
        my_obj.select=True
        scn = bpy.data.scenes[0]
        scn.objects.active=my_obj
        bpy.ops.object.shade_smooth()
        scn.frame_current=frame_num
        bpy.ops.render.render(write_still=True)


I need to run it in background mode, but it will always crash when using function like bpy.ops.object.shade_smooth() because of incorrect context.

Is there a way to apply such operators in background mode? Any workaround?

The point is that I absolutely need to re-open the blend file because each frame involves hundred of megabytes of data and this is (as far as I understand) the only way to free memory in blender.

Any suggestion?

Thanks a lot

If the intent is to simply set all the faces of the mesh to smooth then you can write your own auto smoother which will not require bpy.ops.


if my_obj.type == 'MESH':
    my_mesh = my_obj.data
    for face in my_mesh.faces:
        face.use_smooth = True

This may just move the error to the next usage of bpy.ops, however.

bpy.ops.render.render(write_still=True)

Hi Atom,

I did not see your answer.
Thanks a lot, the render operation does not seem to be a problem in background mode. It works perfectly!