I am not sure if this is a bug or intended behavior, but using bpy.ops.wm.read_factory_settings(use_empty=True) reduces the context to a state where is cannot be used for many common operators, such as bpy.ops.transform.*.

This snippet illustrates the issue:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pprint import pprint as pp

import bpy

def _get_ctx():
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == "VIEW_3D":
                ctx = bpy.context.copy()
                ctx["window"] = window
                ctx["screen"] = screen
                ctx["area"] = area
                ctx["active_object"] = None
                ctx["selected_objects"] = []

                return ctx

if __name__ == "__main__":

    bpy.ops.mesh.primitive_monkey_add(size=2, enter_editmode=False, location=(0, 0, 0))

    # Current context is missing needed data

        bpy.ops.transform.rotate(value=1.570796, orient_axis="X", orient_type="GLOBAL")
    except RuntimeError as e:

    # Add missing bits to context
    ctx = _get_ctx()
    ctx["selected_objects"] = list(bpy.data.objects)

    bpy.ops.transform.rotate(ctx, value=1.570796, orient_axis="X", orient_type="GLOBAL")

The same snippet produces different output depending on whether this is run via cli with --background where adding selected_objects is unnecessary, whereas running via the script editor it is necessary to add selected_objects.

This issue extends to many operators, which makes it a pain to track down what needs to be patched into the context...

I can see that a similar question has been asked here: read_factory_settings makes all other operators call fail in a python script ran at startup

But the suggested answer doesn't work for my usecase. I have a function where I nuke the scene using bpy.ops.wm.read_factory_settings(use_empty=True), import an fbx, and then export in a specified format. The function may be called multiple times in a row in a loop if multiple export formats are desired. My script works fine when run via cli with --background due to the fact that more of the context remains intact after the call to bpy.ops.wm.read_factory_settings(use_empty=True).

TLDR: Is there a way to get a clean, fully valid context (similar to what one would expect after opening a fresh install of Blender with factory settings after deleting all objects in the scene) after running bpy.ops.wm.read_factory_settings(use_empty=True)?



You must log in to answer this question.

Browse other questions tagged .