3
$\begingroup$

I'm starting Blender 2.79 with a --python flag to run a python script.

On a Mac, print() statements show up in the terminal window as expected. On Windows, when executing the same command in git bash, print() statements appear until the main Blender window opens and then output stops.

Where does console output go says on Windows output should go to the "Console Window," but when I select this menu option, the main Blender window loses focus and nothing else happens. (If I start Blender normally the Console Window appears as expected.)

Things that didn't work for me:

Things that kind of worked:

  • Overriding print() as in Blender script run print to console works but only after I quit Blender. -_-
  • The redirect_stdout trick from How to redirect output from 'bpy.ops.*'? prints output if I capture it with stdout.seek(0); print(stdout.read()), but only after I quit Blender. -_-
  • os.system('echo test') works, but if it's possible to get regular python print statements working I'd like to know how.
$\endgroup$
  • $\begingroup$ I found a way to sort of do it by overriding print(), but it's not comprehensive: def print(*args): strings = [] for arg in args: strings.append(str(arg)) output = ' '.join(strings) os.system('echo '+output) This works as expected for everything I've tried except objects, eg: print(datetime) should return <module 'datetime' (built-in)> as it does in a standard python console, but in the context of my script it returns: >The syntax of the command is incorrect. Can anyone improve this? $\endgroup$ – meetar Dec 1 '17 at 20:37
  • $\begingroup$ I think that may be a flaw in the way the windows version works, it doesn't output to stdout, it creates it's own console for output which may be what prevents getting console output this way. $\endgroup$ – sambler Dec 2 '17 at 6:49
  • $\begingroup$ os.system('echo "%s"' % output) will need to make sure any quotes in output are escaped. Suggest never using print(str + str) can use formatting or print(str, str) often suffices. $\endgroup$ – batFINGER Jun 1 '18 at 8:19
  • $\begingroup$ This is a bad idea. 'output' can contain anything, and even simply putting it between quotes won't be enough to have a reliable way to print things. Any string passed to os.system() should be treated very, very carefully in order not to shoot yourself in the foot. Think of this command as "this potentially formats my harddisk/SSD". $\endgroup$ – dr. Sybren Aug 6 '18 at 9:54
1
$\begingroup$

Git Bash is different from regular terminals, as it tries to bridge the gap in behaviour between UNIX terminals and Windows' console (cmd.exe). Run winpty ./blender.exe to get more immediate output of your print() commands.

Note, however, that winpty seems to mangle filenames, so you have to resort to absolute paths in Windows notation to prevent this.

$\endgroup$
0
$\begingroup$

If you only want to see your scripts print output you can run your script in blenders python console. You can find an explanation here which also has a link to an addon I use to add a run script menu item in the console.

If you are using a python script to start blender, the subprocess methods such as run can give you a link to the subprocesses stdout so you can get the output. I don't have access to windows to test but I think that using -con would consume the output and not give your script access to the output.

For your attempt at overriding print(), you could instead use a log file, then instead of watching stdout you can read the contents of the log file. As python is still printing you will get the same info and formatting as you expect.

import builtins

logfile = open('blend_output.txt', 'w')

def print(*args):
    builtins.print(args, file=logfile)
    logfile.flush()

print('this goes to file for me to see')
$\endgroup$

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.