9
$\begingroup$

Can we know, with python, if the system console of blender is open? I know that we can open or close it with this function:

bpy.ops.wm.console_toggle()

But it doesn't return the 'state' of the system console like open or closed (True,False).

I want to be sure that the console is open when i execute one of my script.

$\endgroup$
3
  • $\begingroup$ I see there is a toggle() command but what about explicitly open() or close()?. If you have those you should be able to do a Boolean evaluation. what parameter can you pass to toggle? $\endgroup$
    – user320
    Commented Oct 3, 2013 at 15:16
  • $\begingroup$ This function doesn't accept any parameters and return just a {'FINISHED'}. It just open the console if it's close and vice versa. $\endgroup$
    – lucblender
    Commented Oct 3, 2013 at 17:02
  • $\begingroup$ You can on Windows ; On Windows, it's possible to check using python code, even if the blender console window has been toggled visible or not. See my answer here (linked to not duplicate answers in many places): blender.stackexchange.com/a/291507/24534 $\endgroup$
    – taylor
    Commented Apr 26, 2023 at 0:07

3 Answers 3

7
$\begingroup$

Short Answer: You cannot

First, bpy.ops.wm.console_toggle() is available only on Windows. And I admit, it is a work around. Other OSs run without a console. If you want a console, you must start blender from a terminal on Linux and Mac.

The I/O steam redirect blender.exe > out.txt should work. (but I haven't test it)

If you realy want to communicate with user, you should use a dialog box: http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Interface

error message or save it in a log file. Those methods should work on any system.

P.S.

On C code side, the function returns the current status, and you can query the status.

$\endgroup$
1
  • 3
    $\begingroup$ I can confirm streaming blender in linux works. $\endgroup$
    – Kirbinator
    Commented Oct 4, 2013 at 20:30
1
$\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
  • $\begingroup$ This is a great starting point, though to my understanding it gives you first open console window. Could be a console not related to Blender, or could be a console of another Blender instance. You could send something to the console and find the console with that message in it (I don't know how hard it would be to read it from a console), or you could somehow check for ownership (again, not proficient with winapi, but maybe can be done) $\endgroup$ Commented May 2, 2023 at 14:42
  • $\begingroup$ So far my blender console is the only one with "" as a title ; As it's (on my side) only for debugging reasons and not to be deployed. If another console window with no title was to be used, you could also try to select the blender console considering it's size or the fact the top right close cross is greyed $\endgroup$
    – taylor
    Commented May 3, 2023 at 14:12
0
$\begingroup$
import bpy
from bpy.app.handlers import persistent

bl_info = {
  "name": "Startup",
  "author": "",
  "version": (1, 2, 4),
  "blender": (3, 6, 0),
  "location": "",
  "description": "startup scripts",
  "warning": "",
  "doc_url": "",
  "category": "startup",
  "addon_id": "startup"
}

console_opened = False
def ensure_console():
  global console_opened
  if console_opened: return
  bpy.ops.wm.console_toggle()
  console_opened = True

@persistent
def load_handler(a, b):
  ensure_console()
  
def register():
  bpy.app.handlers.load_post.append(load_handler)

if __name__ == "__main__":
  register()

This will execute the console open just once.
So no need for external stuff.

$\endgroup$

You must log in to answer this question.

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