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.

My .blend file can be found here:

  • $\begingroup$ but inside blender - is too vauge, where exactly? $\endgroup$
    – ideasman42
    Apr 23 '17 at 9:10
  • $\begingroup$ @ideasman42 question updated as suggested. $\endgroup$
    – Tak
    Apr 23 '17 at 10:24

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:

    context = bpy.context.copy()
    for line in text.split("\n"):
        bpy.ops.console.scrollback_append(context, text=line, type='OUTPUT')

console_write("Hello World")

Step 1: Redirect the output to the python console.

You can use the following codes written by @batFINGER (https://blender.stackexchange.com/a/93746/101378):

import bpy
from bpy import context

import builtins as __builtin__

def console_print(*args, **kwargs):
    for a in context.screen.areas:
        if a.type == 'CONSOLE':
            c = {}
            c['area'] = a
            c['space_data'] = a.spaces.active
            c['region'] = a.regions[-1]
            c['window'] = context.window
            c['screen'] = context.screen
            s = " ".join([str(arg) for arg in args])
            for line in s.split("\n"):
                bpy.ops.console.scrollback_append(c, text=line)

def print(*args, **kwargs):
    console_print(*args, **kwargs)       # to Python Console
    __builtin__.print(*args, **kwargs)   # to System Console

The codes above will redirect the outputs in System Console to the Python Console.

Step 2: Add the script to the system path which your Blender can find it.

You can run sys.path in the python console and then it will output a list of system paths which python in Blender can find the imported scripts.

For example, %APPDATA%\Blender Foundation\Blender\2.92\scripts\addons.

enter image description here

Write the script in step 1 to a seperated file, such as pycl.py.

Then use from pycl import print to use the redirected print in your scripts:

enter image description here


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.