9
$\begingroup$

I am trying to make loopcuts on an object in a Python script running Blender 2.77a in the background with the "-b" commandline option. If I don't override the context I get this error:

bpy.ops.mesh.loopcut.poll() expected a view3d region & editmesh

There is an answer for this here on StackExchange:

How do I override context for bpy.ops.mesh.loopcut?

...and the loopcut override works when Blender is in the foreground. But when using the technique in the background, this error comes up:

wmSubWindowScissorSet 0: doesn't exist

...and Blender crashes. Online searches don't help on this, I could find no solution. That leads me to the question - can this be done? How can the context be overrriden so that it will work in the background?

My context override is done like so:

def context_override():
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == "VIEW_3D":
                for region in area.regions:
                    if region.type == "WINDOW":
                        ctx = bpy.context.copy()
                        ctx["window"] = window
                        ctx["screen"] = screen
                        ctx["area"] = area
                        ctx["region"] = region
                        ctx["scene"] = bpy.context.scene
                        return ctx
$\endgroup$
  • $\begingroup$ Related Might pay to check on window and screen to see that they aren't None in bg mode. $\endgroup$ – batFINGER Aug 9 '17 at 7:48
  • $\begingroup$ Can you edit your post to add a minimal script which reproduces this issue (with the call to the mesh.loopcut) ? Thanks $\endgroup$ – Dominique Parisot Aug 31 '17 at 15:45
1
$\begingroup$

It's actually quite simple: You can't use operators in background mode.

When Blender is in background mode or you use Blender as a Python module, then the context does not work as usual, because it doesn't exist. There has to be a 3D viewport etc. for the context to work but all of those things don't exist in background mode.

The solution is to use the internal functions of the operator you are trying to use. But if this operator does not use internal functions you are out of luck.

I got this info from a youtube video where the blender devs answered questions (at about 11 minutes in).

$\endgroup$
  • 1
    $\begingroup$ Be careful, the context does exist in background mode, and operators are available. But as Bastien in the video you linked explains it is discouraged to use operators in background mode, as many (but not all) of them require a screen, an area or the like to work properly. You can test this in a command line. Enter blender -b --python-expr "import bpy;print(bpy.context)" - it will return something like <bpy_struct, Context at 0x7fa0408afc48>, which is a context object. $\endgroup$ – aliasguru Dec 10 '17 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.