3
$\begingroup$

I have a script which enters a long loop and prints a bunch of things in the console.

Sometimes after a crash I forget to toggle it back on and I run the script and now I can't see what's it doing and the whole blender is not letting me click anything.

Is it possible to do something like this?

if console.visible == False:
    console.toggle()
$\endgroup$
3
  • $\begingroup$ Do you want to turn on the console when blender start? $\endgroup$
    – X Y
    Jul 16, 2022 at 9:42
  • 1
    $\begingroup$ there is no way to check if it is visible. blender.stackexchange.com/questions/3155/… $\endgroup$ Jul 16, 2022 at 9:53
  • $\begingroup$ you can actually, using win32gui ; please refer to my answer bellow $\endgroup$
    – taylor
    May 3, 2023 at 14:01

4 Answers 4

3
$\begingroup$

You could start Blender with the console on by default by adding -con to the shortcut properties:

\blender_path\blender-3.2.0-windows-x64\blender.exe -con
$\endgroup$
3
$\begingroup$

As @radoo has already pointed out you can run blender.exe with the argument -con (for "console on")

On Unix and Unix-Like systems that is easy since we can directly run it in the terminal and see the console outputs. If you are on Windows you can create a .bat file for convenience with the following contents

start "" "X:\blender_path\blender.exe" -con

Then you can directly run the .bat file and you will have the system console always available.

$\endgroup$
2
$\begingroup$

You can on Windows ; On Windows, it's possible to check using python code, whether the blender console window is already visible or not.

That Blender's console window being most probably the only window of the console class (ConsoleWindowClass) with the name ""

(see bellow to install win32gui)

def show_blender_system_console():
    import win32gui

    def enum_windows_callback(hwnd, results):
        class_name = win32gui.GetClassName(hwnd)
        if class_name == "ConsoleWindowClass":
            results.append((hwnd, win32gui.GetWindowText(hwnd)))

    windows = []
    win32gui.EnumWindows(enum_windows_callback, windows)

    for hwnd, title in windows:
        if title=="":
            break

    print(f"The Blender console window is {hwnd}")

    # Check if the window is visible or hidden
    is_visible = win32gui.IsWindowVisible(hwnd)

    if is_visible:
        print("The window is visible.")
        win32gui.SetForegroundWindow(hwnd)
    else:
        print("The window is hidden.")
        bpy.ops.wm.console_toggle()

win32gui is included in pywin32 ; to install a python package do in a cmd (win+R>cmd):

C:\[path to your blender app]\python\bin\python.exe -m pip install pywin32

e.g. (Change the path accordingly to your version):

C:\Program Files\Blender Foundation\Blender 3.5\3.5\python\python.exe -m pip install pywin32

you might want to do ensure pip is installed and updated before:

C:\[path to your blender app]\python\bin\python.exe -m ensurepip
C:\[path to your blender app]\python\bin\python.exe -m pip  install --upgrade pip
$\endgroup$
2
  • 1
    $\begingroup$ You need to include an explanation on how to install win32gui. It is not there by default. $\endgroup$ Apr 26, 2023 at 0:56
  • 2
    $\begingroup$ explanation added :) $\endgroup$
    – taylor
    Apr 26, 2023 at 14:11
0
$\begingroup$

improved solution:

import bpy
def show_blender_system_console():
    import win32gui

    def enum_windows_callback(hwnd, results):
        class_name = win32gui.GetClassName(hwnd)
        if class_name == "ConsoleWindowClass":
            results.append((hwnd, win32gui.GetWindowText(hwnd)))

    windows = []
    win32gui.EnumWindows(enum_windows_callback, windows)

    for hwnd, title in windows:
        if "blender.exe" in title:
            break

    print(f"The Blender console window is {hwnd}")

    # Check if the window is visible or hidden
    is_visible = win32gui.IsWindowVisible(hwnd)

    if is_visible:
        print("The window is visible.")
        win32gui.SetForegroundWindow(hwnd)
    else:
        print("The window is hidden.")
        bpy.ops.wm.console_toggle()
        
show_blender_system_console()
$\endgroup$

You must log in to answer this question.

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