C++ API: not printing to output window

C++ API: not printing to output window

Anonymous
Not applicable
12,962 Views
37 Replies
Message 1 of 38

C++ API: not printing to output window

Anonymous
Not applicable
Hello,

I'm having some trouble with printing to the Output Window using cout (or printf, for that matter). It seems to work some of the time, and not others. I haven't been able to find a pattern yet. All other features of the api seem to be working.

I'm wondering if anyone might know what conditions need to be met to ensure that the standard output is properly sent to the Output Window.

Thanks in advance.
0 Likes
12,963 Views
37 Replies
Replies (37)
Message 2 of 38

Anonymous
Not applicable
So it turns out there is some kind of pattern: the "cout" stream will be sent to the Output Window if I'm debugging (starting Maya using the Visual Studio debugger, and stepping through the code), but does not work when the plugin is run without debugging. I'm trying to determine what would be different, but haven't found anything.

If anyone has any insight, it'd be appreciated.

Thanks.
0 Likes
Message 3 of 38

Anonymous
Not applicable
If anyone is interested:

It seems that Maya has to be run as an administrator for the output window to display the standard output (on windows, anyway). It was working in a debug environment because I'm starting Visual Studio as an administrator, which in turns starts Maya with elevated privileges. However, when I'd start Maya by itself, I wasn't doing it as the admin. When I started doing this, the output window displayed the standard output of the plugin just fine.

The weird thing was that I set the Maya taskbar shortcut to always run as admin, and I was getting the Windows UAC warning that it was being started with elevated privileges, but this didn't work. I have to explicitly click on "Run as Administrator" each time I open the application.

So does anyone have any ideas as to why Maya would require admin privileges to display the standard output of a plugin? It seems unlikely that in normal circumstances, a regular user would start the application this way. I know I've seen data in the output window from other sources without the application running with elevated privileges.

I'd be interested in any insight.

Thanks.
Message 4 of 38

Anonymous
Not applicable
I'm also having trouble printing to the output window. I haven't had any success at all. I'm running on a mac, so I don't know of an equivalent to 'run as administrator'. I'm still interested in an answer to this.

XCode 4.5.2
GCC 4.2

Maya 2013
OS X 10.8.2
0 Likes
Message 5 of 38

admin1
Participant
Participant
@Anonymous Do you have a solution for this?
0 Likes
Message 6 of 38

admin1
Participant
Participant

I found out that printing to cerr does show up in the output window.

 

So as a temp workaround, I now just redirect all values written to cout to cerr

 

std::cout.rdbuf(std::cerr.rdbuf())
Message 7 of 38

Anonymous
Not applicable

I also have this issue since I updated to Maya 2016 Extension 2 SP1..

 

The workaround presented by admin1 works for couts,

 

cout.rdbuf(cerr.rdbuf())

just put it at the beginning of the initializePlugin function to redirect the outputs.

 

However, all viewport compilation error messages are not thrown into the command output window either.

This needs a fix!

 

 

P.S. Administrator rights don't fix anything in my case

 

WIN10 / Maya 2016 Ext 2 SP1

0 Likes
Message 8 of 38

Anonymous
Not applicable

I correct myself, viewport 2.0 FX compilation error messages are thrown into the command output window, but make Maya crash now...

The issue with printing to the command window is specifically with cout

0 Likes
Message 9 of 38

cheng_xi_li
Autodesk Support
Autodesk Support

Hi thasanty,

 

Do you have a working sample for this issue? I am not sure about how to reproduce this problem. Can you try with fflush(stdout) and fflush(stderr) first?

 

Yours,

Li

0 Likes
Message 10 of 38

Anonymous
Not applicable

Hi Li,

 

unfortunately fflush(stdout) and fflush(stderr) did not change anything.
I made a troubleshoot plugin for you to see and replicate the error (code at the bottom)

You can also download two compiled plugins (Win x64) of the source code. One without and one with the fix.

 

Here is a short screencast highlighting the issue:

 

 

Something curious is that after 

cout.rdbuf(cerr.rdbuf())

has been run once from the plugin fix, the command output starts behaving normally again on the normal plugin. (see video)

 

I hope you can reproduce this issue now.

 

best regards,

 

Santiago

 

///////////////////////////////////////////////////////////////////////////////////
//
//	Main Plugin File
//
//	Summary:	This file initializes and uninitializes the plugin
// 
//	Functions:
//
//	initializePlugin:		Plugin initializer 
//
//	uninitializePlugin:		Plugin uninitializer
//
///////////////////////////////////////////////////////////////////////////////////

#include <maya/MFnPlugin.h>


/* Register a render override, npr command and configuration node */
MStatus initializePlugin(MObject obj) {
	MStatus status;

	MFnPlugin fnPlugin(obj, "Troubleshooter", "cout", "Any"); //(obj, "YOUR NAME", "Version/prototype", "Any")

	//cout.rdbuf(cerr.rdbuf()); //hack to get error messages out in Maya 2016.5

	cout << "-> Plugin Initialized" << endl;

	//fflush(stdout);
	//fflush(stderr);

	return status;
}


/* When uninitializing the plugin, make sure to deregister the
overrides, nodes or commands */
MStatus uninitializePlugin(MObject obj) {
	MStatus status;
	MFnPlugin fnPlugin(obj);

	cout << "Plugin Uninitialized" << endl;

	return status;
}

 

0 Likes
Message 11 of 38

Anonymous
Not applicable

Status of this?  I just spent an hour trying to figure out why cout stopped working.

0 Likes
Message 12 of 38

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

Sorry for the late reply. I tried to reproduce the issue with the code provided, but it can't be reproduced on my computer.

 

I had a few times when debugging a debug version of plugin and the output was delayed, but it never happens in release mode on my window 7 and windows 10.

 

Yours,

Li

0 Likes
Message 13 of 38

admin1
Participant
Participant

I'm having this issue on 3 different machines (all with Maya 2016 x64, Windows 10 x64 and Visual Studio 2015).

 

Are you using a different version of Visual Studio?

 

For now I'm using my cout.rdbuf(cerr.rdbuf()) fix, but that really is a hack.

 

Best regards,

Peter verswyvelen

 

0 Likes
Message 14 of 38

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

Yes, I am using VS2012.  

 

Yours,

Li

0 Likes
Message 15 of 38

Anonymous
Not applicable

Using Maya 2016, and just switched to vs2015 ( was using vs2012 ). Now having the same problem: unable to print anything to output window with release builds. Can only print with debug builds. Tried every solution in this thread to no avail. ( administrator, cerr, flushes, redirects .. ) Nothing works.

 

Perhaps something needs to be defined in the project compile settings?

 

Any new information?

 

Thanks.

H

0 Likes
Message 16 of 38

Anonymous
Not applicable

Got a fix that kinda works:

 

Use MStreamUtils to get to the output stream:

 

MStreamUtils::stdErrorStream() << "TEST1 " << "\n";
MStreamUtils::stdOutStream() << "TEST2 " << "\n";

 

Include:

 

#include <maya/MGlobal.h>
#include <maya/MStreamUtils.h>

 

Above works, but if you switch out the "\n" with std::endl like so:

 

MStreamUtils::stdErrorStream() << "TEST1 " << std::endl;
MStreamUtils::stdOutStream() << "TEST2 " << std::endl;

 

Instant crash:

 

Unhandled exception at 0x000002CBDCF50150 in maya.exe: 0xC0000005: Access violation executing location 0x000002CBDCF50150.

 

So Maya hates std::endl for some reason. I use them everywhere so this just ruined my day. Maybe someone knows a trick?

 

 

 

Message 17 of 38

michaelkdaw
Enthusiast
Enthusiast

This is so bizarre. I've been using Python for a long time and haven't worried about writing cpp plugins. I just started one tonight for fun and immediately ran in to this same "cout" problem. I went looking online, and found this thread. It wasn't until I'd read through it a couple times before I realized I had started it in 2012.

 

Anyway, DEVR2702's solution worked for me. If the "run as admin" thing ever did work, it doesn't now.

0 Likes
Message 18 of 38

Anonymous
Not applicable

I have the same problem with Maya 2016 and Visual Studio 2015 and Release Builds.

In a Debug build, i see cerr but not cout, in a Release Configuration I do not get any output on the console.

0 Likes
Message 19 of 38

Anonymous
Not applicable

ED: Moved

0 Likes
Message 20 of 38

kollig
Advocate
Advocate

Hi!

 

I always have cout-Output Window trouble as long as the used compiler doesn't match the build environment of Maya. Currently my solution is to set rdbuf of cout and cerr to MStreamUtils::stdOutStream().rdbuf() resp. stdErrorStream in initializePlugin.

 

So far, I haven't seen a problem with this solution, while testing my plugin for 4 different Maya versions (2016 to 2018) on Windows 7 and 10.

 

-Thomas