2

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

6
  • 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? Commented Dec 26, 2019 at 13:44
  • Metashape has Python API. Subprocesses wouldn't be big issue. Output could be piped to log file or os.devnull Commented Dec 26, 2019 at 13:49
  • You can redirect (OS-level) streams. You can also redirect to which stream C++'s cout etc write. Commented Dec 26, 2019 at 14:08
  • @UlrichEckhardt Can You please explain this a bit more. Also, note that I cannot edit C++ code Commented Dec 26, 2019 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. Commented Dec 27, 2019 at 9:54

1 Answer 1

2

You can access to console pane through Metashape.app.ConsolePane class or can just allow to create log file with Metashape.app.settings.log_enable = True and Metashape.app.settings.log_path = '...\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

Sign up to request clarification or add additional context in comments.

2 Comments

can you provide a sample code? I've tried to use setValue method for Metashape.Settings object and it doesn't seemed to work.
It looks like there is some but in Metashape.Settings object. Instead of using setValue for setting your own setting the Agisoft team is proposing to create a custom .conf file. Check the thread here: agisoft.com/forum/index.php?topic=14234.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.