4
$\begingroup$

When calling operators such as bpy.ops.mesh.remove_doubles() it outputs how many vertices were removed into console.

In some cases its better to silence it, but you may also want to catch it and use it within the script.

How can I redirect or otherwise silence the text from printing into the terminal?

$\endgroup$
2
  • $\begingroup$ @ideasman42 dublicate ? blender.stackexchange.com/q/6119/5113 $\endgroup$
    – Chebhou
    Commented Jul 18, 2015 at 20:31
  • 1
    $\begingroup$ Asking to redirect output isn't quite the same as suppressing. $\endgroup$
    – ideasman42
    Commented Jul 18, 2015 at 20:42

1 Answer 1

6
$\begingroup$

This can be done by temporarily overriding the output using redirect_stdout.

eg:

Short example:

from contextlib import redirect_stdout
import io
stdout = io.StringIO()
with redirect_stdout(stdout):
    bpy.ops.mesh.remove_doubles()

Commented answer with redirect:

# Python module to redirect the sys.stdout
from contextlib import redirect_stdout
# So we can create a file-like object
import io

# our new output
stdout = io.StringIO()

with redirect_stdout(stdout):
    # any printing or operator output within this block
    # will go into 'stdout'
    bpy.ops.mesh.remove_doubles()

# if you like to get the output and use it
# you can read it as if its a regular file.
stdout.seek(0)
output = stdout.read()
del stdout

print("We caught the output, it was %d long!" % len(output))

Note, that this can also be used to re-direct operator reports for your own use, and example of this is a benchmarking tool which extracts output from the draw timer. https://gitlab.com/ideasman42/blender-fps-bench/blob/master/blender_fps_bench.py#L196

$\endgroup$

You must log in to answer this question.

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