How can I run some script and have print result in console window? Now I have these results only in System Console Window.

Asked
Active
Viewed 9,791 times
5
piotrek
- 261
- 1
- 6
- 14
1 Answers
13
Override print method.
Been investigating a number of ways to do this. Possibly the simplest is to override the print in your scripts. Below is designed to from somewhere import print and writes all print non keyword arguments to open consoles in same screen.
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() function."""
console_print(*args, **kwargs) # to py consoles
__builtin__.print(*args, **kwargs) # to system console
print("print me to console")
print("and me\n and me", "and me\nI'm:", print)
My attempts at redirecting stdio have failed and often lock up a console. Will get back to this later with better links, but there is a lot of console code to fiddle around with ... console_python.py bl_operators/console.py and the mathviz addon code.
EDIT: See Dubplicate Question Answer on handling stdio redirecting.
batFINGER
- 84,216
- 10
- 108
- 233

pyconsole.pyfile, which is in the path ofsys.path(e.g.%APPDATA%\Blender Foundation\Blender\<version>\scripts\addons) in Blender, and then usefrom pyconsole import printin the script. – Hansimov May 29 '21 at 13:07