3
$\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$
4
$\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$

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.