I have a button created by my script. I wonder how I can display what the system console displays but inside blender's console. CoDEmanX did this by running a script inside it as shown here, but I want to display what happens in the system console inside blender's console.
- First redirect output from Python's stdout into your own buffer,
see: How to redirect output from 'bpy.ops.*'? - Then run
bpy.ops.console.scrollback_append
with the console space set using an override, see: https://blender.stackexchange.com/a/31398/55
Hello world example that prints text into the first console found.
import bpy
def console_get():
for area in bpy.context.screen.areas:
if area.type == 'CONSOLE':
for space in area.spaces:
if space.type == 'CONSOLE':
return area, space
return None, None
def console_write(text):
area, space = console_get()
if space is None:
return
context = bpy.context.copy()
context.update(dict(
space=space,
area=area,
))
for line in text.split("\n"):
bpy.ops.console.scrollback_append(context, text=line, type='OUTPUT')
console_write("Hello World")