1
$\begingroup$

repro:

  1. Run Blender 4.2.0
  2. File -> New -> General
  3. Scripting Workspace
  4. Template -> Simple Operator

replace main function with

def main(context):
    bpy.ops.console.scrollback_append(text="hello world", type="OUTPUT")

Blender generates a runtime error because object.

Python: Traceback (most recent call last):
  File "/operator_simple.py", line 41, in <module>
  File "./blender-4.2.0-linux-x64/4.2/scripts/modules/bpy/ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Error: Python: Traceback (most recent call last):
  File "/operator_simple.py", line 18, in execute
  File "/operator_simple.py", line 5, in main
  File "./blender-4.2.0-linux-x64/4.2/scripts/modules/bpy/ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Operator bpy.ops.console.scrollback_append.poll() failed, context is incorrect
Location: ./blender-4.2.0-linux-x64/4.2/scripts/modules/bpy/ops.py:109

What context needs to be setup?

in blender 3.x i would specify an override for window, screen, area

override = {'window': window, 'screen': screen, 'area': area}
bpy.ops.console.scrollback_append(overide, text='hello world', type="OUTPUT")

but in blender4, i cannot seem to specify these, either as args (no accepted), kwargs (ignored), or bpy.context (refused mutation, read only). i suppose there are other, less accessible ways to change the window, screen, and area.

might be looking into a temporary change current window

$\endgroup$

1 Answer 1

1
$\begingroup$

overriding the context of a bpy operator changed, there is now bpy.context.temp_override in blender 4.2+ specifically for changing the window, screen, and area context items.

for example, to bpy.ops.console.scrollback_append to all consoles currently layed out in blender:

import bpy
with bpy.context.temp_override(**override):
    for window in bpy.context.window_manager.windows:
    screen = window.screen
    for area in screen.areas:
        if area.type == 'CONSOLE':
            override = {'window': window, 'screen': screen, 'area': area}
            for line in str(data).splitlines():
                with bpy.context.temp_override(**override):
                    bpy.ops.console.scrollback_append(text='hello world', type="OUTPUT")
$\endgroup$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .