Here are some more options, and better options for that matter too - especially logging
if you’re logging lots of information.
Using logging
(recommended)
The logging module also prints out to the statusbar because the default handler uses sys.stdout.write
, for example:
# not recommended since it logs using the 'root' logger
import logging
logging.info("test")
And better is using a set logger for your code so logging level can be defined individually.
import logging
logger = logging.getLogger(__name__)
logger.info("test")
More info on logging
see the Python documentation.
This works because the default handler for logging uses sys.stdout.write
. And even better, it also maps the logging for warnings/errors correctly with the color highlight of the status bar. Bob White mentioned this on Tech Artists Slack:
Yeah, each logging level gets mapped to a thing. Its also what cmds.warning
and cmds.error
use.
So you get the yellow/red background when doing a logging.warning
or logging.error
Using maya.api.OpenMaya.displayInfo
Likely better is using the proper method for these:
import maya.api.OpenMaya as om
om.MGlobal.displayInfo("test")
Credits to Bob White for mentioning this on Slack.
Using sys.stdout.write
Chris Cunningham mentioned on Slack to try sys.stdout.write
and that does write nicely to the script editor in Maya 2022 plus gets shown as expected in the statusbar.
import sys
sys.stdout.write("test")
Plus it even shows in the statusbar if you include a new line.
import sys
sys.stdout.write("test\n")
So you could have for example a printstatus function:
import sys
def printstatus(*args, sep=", ", end="\n", flush=True):
sys.stdout.write(sep.join(args) + end)
if flush:
sys.stdout.flush()
# Example usage
printstatus("test")
printstatus("test", "multiple", "args")
Note that the above will raise a SyntaxError in Python 2, so that wouldn’t compile in older versions of Maya. This however would work in both:
import sys
def printstatus(*args, **kwargs):
sep = kwargs.pop("sep", ", ")
end = kwargs.pop("end", "\n")
flush = kwargs.pop("flush", True)
if kwargs:
raise TypeError(
"printstatus() got unexpected keyword "
"arguments: {}".format(", ".join(kwargs.keys()))
)
sys.stdout.write(sep.join(args) + end)
if flush:
sys.stdout.flush()
printstatus("test", "multiple", "args")