98
$\begingroup$

I'm trying to debug a Python script by placing a print("foo=" + foo) statement in my code.

But where does the output appear?

It doesn't appear on the Python Console, and it doesn't appear on the area that appears when you drag the lower edge of the infobar downwards (what is the name for this area? My script errors show up in red here.)

$\endgroup$
3

9 Answers 9

125
$\begingroup$

It's printed to the system console. If it's not opened, go to Window > Toggle System Console.

Note that this option is only available on Windows. On other OS, start Blender from a terminal.

See here: https://docs.blender.org/manual/en/latest/advanced/command_line/introduction.html

You may also use this script:
Is there anyway to make blender print errors in the UI?

$\endgroup$
9
  • 3
    $\begingroup$ ah man, you have even written a script to fix it. One upvote is not enough! $\endgroup$
    – P i
    Jan 11, 2014 at 1:55
  • 3
    $\begingroup$ Window > Toggle System Console doesn't exist in Blender 2.77 $\endgroup$
    – Andy Ray
    Apr 22, 2016 at 21:33
  • 4
    $\begingroup$ @user824624: read again, I clearly stated that this option is exclusively available on Windows. On Linux and OS X, you need to start Blender from a shell. $\endgroup$
    – CodeManX
    Jun 7, 2016 at 10:41
  • 2
    $\begingroup$ On OS X you can run it from terminal like this: /Users/eugenekulabuhov/Downloads/blender-2.77-OSX_10.6-x86_64/blender.app/Contents/MacOS/blender Update the path to blender.app. $\endgroup$ Aug 21, 2016 at 15:26
  • 6
    $\begingroup$ Warning! If you open the Console, don't ever close it! It will kill Blender instantly without saving! $\endgroup$ Oct 21, 2018 at 17:12
22
$\begingroup$

Here is my script which redirects the print() function to all open python console windows.

import bpy
def print(data):
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == 'CONSOLE':
                override = {'window': window, 'screen': screen, 'area': area}
                bpy.ops.console.scrollback_append(override, text=str(data), type="OUTPUT")       
$\endgroup$
3
  • 1
    $\begingroup$ Helpful to make data parameter *data - and text parameter to text=str(" ".join([str(x) for x in data])) (This allows print to take multiple parameters) $\endgroup$
    – Brad
    Nov 22, 2019 at 2:36
  • 1
    $\begingroup$ @Brad or similarly blender.stackexchange.com/questions/93728/… $\endgroup$
    – batFINGER
    Nov 23, 2019 at 12:32
  • 1
    $\begingroup$ FANTASTIC! Not sure why that isn't a default operation or why there isn't a simple bpy command to do that already. But thank you so much! $\endgroup$ Jan 4, 2023 at 13:36
10
$\begingroup$

With my own experience and help from Sambler I made a simple app for the purpose of opening Blender with terminal.

Download the application

How to use it:

  1. Navigate yourself to blender.app with finder.

enter image description here

  1. Right click on Blender and select Show Package contents.

enter image description here

  1. Download the app and unzip it.

enter image description here

  1. Drag the app into Blender's Contents folder.

enter image description here

  1. Drag the app to dock and open for the first time.

enter image description here

Alternatively here is the applescript source: (currently, with plenty of helpful comments)

set myPath to ((path to current application) as string) --find the path to blenderOpen.app
set myPath to ((characters 1 through ((length of myPath) - 1) of myPath) as string) --rip off the last ":"
set charDelete to (last character of myPath) -- rip off the "blenderOpen.app"
repeat until charDelete = ":" -- rip off the "blenderOpen.app"
    set myPath to ((characters 1 through ((length of myPath) - 1) of myPath) as string) -- rip off the "blenderOpen.app"
    set charDelete to (last character of myPath) -- rip off the "blenderOpen.app"
end repeat
set myPath to myPath & "MacOS" --find the blender runtime by appending this path

set myPath to quoted form of the POSIX path of myPath -- convert path so terminal understands
(*
why this little if statement down below?
This if statement is here because if a user
opens terminal and runs some command,
then afterwards runs our script,
we want to use a new window so as not
to interfere with the user.
However, if WE open terminal,
than we want to use the window 
that terminal just made for us.
*)

if testterminal() then
    tell application "Terminal" to do script "cd " & myPath & " && ./blender" -- tell terminal to open new window, and open blender, Voila!!!
else
    tell application "Terminal" to tell front window to do script "cd " & myPath & " && ./blender" -- tell terminal to open blender, in the current window, Voila!!!
end if


return myPath
on testterminal()
    tell application "System Events" to (name of processes) contains "Terminal"
end testterminal

This app also enables you to open up as many instances of blender as you want at the same time.

$\endgroup$
3
  • $\begingroup$ Please don't post duplicate answers, kindly link to the other post/answer and if you could tidy this up that would be great, thanks! $\endgroup$
    – iKlsR
    Jul 23, 2016 at 14:15
  • $\begingroup$ @iKIsR And how would that be done, do you think i should just delete all in edit and paste a link, i'm sorry about the duplicates, how should i go about tidying this up? and thanks for commenting. $\endgroup$ Jul 23, 2016 at 16:07
  • $\begingroup$ For one, if you are new to markdown, there is a styleguide help button to the top right of the editing area when you are creating the post. You have some comments about leaving an image once you have the rep (which you now do) so maybe add those. Basically look at any post from a high rep user and try to structure it like that. We prefer self contained answers so links to external sources can be added but it's helpful if that same content is summarized here. $\endgroup$
    – iKlsR
    Jul 23, 2016 at 16:12
9
$\begingroup$

If you're on macOS and in a terminal, locate blender.app, then do:

./blender.app/Contents/MacOS/blender

Blender's log messages will now appear in the terminal window from which you started it.

Note that open blender.app does not show you any log messages.

$\endgroup$
6
$\begingroup$

I'm currently using Blender 2.78 and I think that the easiest way to see your console logs is to go inside the blender container, exactly like in previous posts. Then go to MacOs folder and run blender script. enter image description here It will open terminal and blender application.

enter image description here

$\endgroup$
2
$\begingroup$

Updated with the second part to the solution.

When you click "run script," Python errors are reported in the console. Blender API errors, however, are not. If you have Blender API errors before the line where your call to print() happens, then your code never reaches that line. Blender and Python both quietly let this happen without error or warning.

Tick the "register" box in the text editor, save, and click "run script". Your script class should have a bl_label field set to a string. Go to the viewport, press spacebar, and type that string. When it shows up in the results, click it.

Now your Blender API errors will show themselves. Correct your code, and when your print() call is reached, it will print to the system console.

I'm shocked that this doesn't seem to appear anywhere on threads about this yet, especially since differences between types such as BMesh and Mesh render show-stopping API errors like this really easy to make.

$\endgroup$
2
$\begingroup$

If you're running Blender from Linux and didn't start it in the shell (which is fine to do), then you can usually tail this log file to see the stdout output from Blender (and any other GUI program)

tail -f ~/.xsession-errors
$\endgroup$
2
$\begingroup$

Easiest way in Ubuntu Linux is to create a Desktop launcher. Simply right click on desktop, popup menu offers create launcher option. Select. When creating the launcher, you get the option to run in Terminal. Select that option. Now every time you launch Blender from the desktop, it automatically runs the terminal.

$\endgroup$
1
  • 1
    $\begingroup$ Window manager is...? $\endgroup$
    – brockmann
    Feb 26, 2020 at 10:41
0
$\begingroup$

I often need to patch @TheMindVirus's print method, so here is my variant.

print_orig = print
def print(*data):
    if len(data) == 1: data = data[0]
    print_orig(data)
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == 'CONSOLE':
                override = {'window': window, 'screen': screen, 'area': area}
                for line in str(data).splitlines():
                    bpy.ops.console.scrollback_append(override, text=line.rstrip(), type="OUTPUT")
  • handles inputs of type other than string
  • handles multi-line string prints
  • prints to both terminal/console and blender console, for easy multi-use

I also advise combining this print function, with a top-level try catch, which lets the blender console print many runtime errors for devel which normally would only print to the terminal

if __name__ == "__main__":
    register()

    # test call
    try:
        bpy.ops.object.simple_operator()
    except Exception as err:
        print(err)
        raise
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .