0

I'm writing Python script in photogrammetry application Metashape. App is written in C/C++ and I would like to suppress all of its console output and make my own progress bar. There are several solutions proposed on how to suppress C++ prints within Python script, but all of those are based on an idea of getting file descriptor of standard output (sys.stdout). Python within Metashape is probably edited and built by Metashape's developers team so some functionalities are missing. There is no method fileno() in sys.stdout so I cannot get this file descriptor. I suppose I could hard code it, but this script if just a part of a bigger architecture and a lot of files are imported, exported and edited, so I suppose this is not a very good idea. This is probably the most promising solution I've find link, but I am stuck at the beginning of it. Any ideas?

Edit:

Python version: 3.5.2 (default, Aug 28 2018, 15:41:10) [MSC v.1600 64 bit (AMD64)]

Note that in the script there are more than twenty C++ function calls that produce massive console output, so it would be ideal to make it possible to block and re-enable C++ output during the process. Also, any simpler solution, even if you ignore problem with non existing method fileno(), would be appreciated. There should be simpler solution to suppress C++ output within Python app

  • How is the C++ code interacting with the Python script? Is it being called as a subprocess, or does this library provide a Python interface? – 0x5453 Dec 26 '19 at 13:44
  • Metashape has Python API. Subprocesses wouldn't be big issue. Output could be piped to log file or `os.devnull` – Nemanja Stojanovic Dec 26 '19 at 13:49
  • You can redirect (OS-level) streams. You can also redirect to which stream C++'s `cout` etc write. – Ulrich Eckhardt Dec 26 '19 at 14:08
  • @UlrichEckhardt Can You please explain this a bit more. Also, note that I cannot edit C++ code – Nemanja Stojanovic Dec 26 '19 at 14:13
  • To be honest, I don't understand. If there's some code you can't change, then it's completely irrelevant that it's written in C++. How are calling that code? Is it a module? A subprocess? What is the relation to Metashape? Is that really relevant? Please also don't add "edit: ..." sections to your question, it should read a whole without picking bits and pieces from various locations. – Ulrich Eckhardt Dec 27 '19 at 09:54
  • To be honest, I don't understand. If there's some code you can't change, then it's completely irrelevant that it's written in C++. How are calling that code? Is it a module? A subprocess? What is the relation to Metashape? Is that really relevant? Please also don't add "edit: ..." sections to your question, it should read a whole without picking bits and pieces from various locations. – Ulrich Eckhardt Dec 27 '19 at 09:54

1 Answers1

1

You can access to console pane through Metashape.app.ConsolePane class or can just allow to create log file with Metashape.app.Setings.log_enable = True and Metashape.app.Seting.log = '...\log.txt' check the doc here: Metashape python API doc.

However you can also try to use solution described here: How do I prevent a C shared library to print on stdout in python? I just tested it on my stand alone Metashape python script and works really well. Metashape python api v. 1.6.5, Python v. 3.7.9