6
$\begingroup$

Every time the bpy.ops.remove_doubles() operator is used, it outputs to my terminal (where I called Blender from). How can I suppress this output?

The code is:

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.remove_doubles()

and the undesired output is:

Info: Removed 48 vertices

$\endgroup$
  • 1
    $\begingroup$ I don't know how to suppress it or if it's even possible, but I believe that message is generated by the report method (stuff will appear in stdout every time something is displayed in the info panel) See this related post. $\endgroup$ – gandalf3 Jan 9 '14 at 8:06
5
$\begingroup$

Python can temporarily redirect the stdout, this is ideal since you may not want to suppress output for _all_ scripts, just selectively silence some operations.

eg:

import bpy

import io
from contextlib import redirect_stdout

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

If you want you can read the output back out or use it however you like.

stdout.seek(0)
output = stdout.read()
print("Report was %r" % output)

If you want to redirect both stdout and stderr.

import bpy

import io
from contextlib import redirect_stdout, redirect_stderr

output = io.StringIO()
with redirect_stdout(output), redirect_stderr(output):
    bpy.ops.some.operator_that_may_print_text()
| improve this answer | |
$\endgroup$
  • $\begingroup$ This doesn't work for bpy.ops.wm Could you help me out with this? $\endgroup$ – Školstvo Apr 13 '17 at 18:41
2
$\begingroup$

To filter the shell output of Blender you can start it via command line using a pipe with sed (Linux / OSX) or findstr (Windows). We can use sed or findstr to omit every line that starts with Info.

Linux / OSX:

blender 2>&1 | grep --line-buffered -v '^Info'

Windows:

blender.exe 2>&1 | findstr /v /b "Info"

findstr on Windows doesn't seem to have a similar option like --line-buffered. You could install grep for Windows though.

If you want to mute Blender all together preventing it from printing anything you can do this too.

Linux / OSX:

blender > /dev/null 2>&1

Windows:

blender 2>&1 > NUL
| improve this answer | |
$\endgroup$
  • $\begingroup$ Cool! I wouldn't have thought of that. I guess the only disadvantage is the output (which I do care about) is not displayed until I quit Blender. $\endgroup$ – Garrett Jan 10 '14 at 4:32
  • $\begingroup$ I updated my answer to account for realtime output (I hope). $\endgroup$ – Maccesch Jan 10 '14 at 11:21
  • $\begingroup$ I tried the command above (blender 2>&1 | grep --line-buffered -v '^Info'), but again, it displayed no output at all until I closed Blender, at which point, it correctly suppressed all the lines starting with 'Info'. $\endgroup$ – Garrett Jan 11 '14 at 1:22
  • $\begingroup$ You can verify this behavior by opening a new Blender file, then go to Text Editor, then entering: print("Info - don't display"); print("Hello - display this"), then running the script. $\endgroup$ – Garrett Jan 11 '14 at 1:32
  • 1
    $\begingroup$ blender 2>&1 | grep --line-buffered -v '^Info' Works like a charm. Thank you, @Maccesch! $\endgroup$ – mcgeo52 May 25 at 22:37
2
$\begingroup$

This looks to be exactly what you need. This process would work well for redirecting stdout and/or stderr to /dev/null when you're about to display something that you don't want to see.

Essentially, point the current references to /dev/null, which will make output go away:

devnull = open(os.devnull, 'w')

sys.stdout, sys.stderr = devnull, devnull

...then point them back to stdout and stderr when you're done, which will restore normal output:

sys.stdout = self.__stdout__

sys.stderr = self.__stderr__

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Note: sys already contains the original values for stdout and stderr. You do not have to save them. To restore: sys.stdout = sys.__stdout__. The same is true in many other cases. Also /dev/null is not valid python syntax. $\endgroup$ – Bakuriu Jan 10 '14 at 12:23
  • 1
    $\begingroup$ I see, but in the Python docs udner sys.__stdout__, it says "the preferred way to do this is to explicitly save the previous stream before replacing it, and restore the saved object." $\endgroup$ – Garrett Feb 9 '14 at 6:47
  • $\begingroup$ While this will work, it would be better to temporarily override stdout, see: blender.stackexchange.com/a/34218/55 $\endgroup$ – ideasman42 Jul 18 '15 at 20:43

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.