Results 1 to 5 of 5
  1. #1
    Member Atom's Avatar
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    11,220

    A Console Based Progress Bar

    Hi All,

    Nothing amazing here, but I was wondering how Yafaray and LUX get green colored text in the console window. Does anyone know how to make colorized text appear in the console in Blender 2.5?

    As I was searching for this answer, I ran across this code that works in Blender.
    Code:
    import sys
    import time
    
    def progress(n):
       for i in range(n+1):
           sys.stdout.write('\r%3s%% [%s>%s]' % (i, '='*i, ' '*(n-i)))
           sys.stdout.flush()
           time.sleep(0.5)
    
    progress(60)
    Run this short code and then immediately bring up the console window. You will see that you can have a text based progress bar. This may be a nice way to show a function in progress if they take a long time.
    Last edited by Atom; 28-Mar-11 at 13:58.
    OSX Mac Mini i7 @ 2.6ghz 16Gb Ram (no GPU)
    Windows 7 8Gb, GeForce GTX 660 2Gb Ram AMD Hexcore @ 2.7Ghz.
    Atom's Links Page



  2. #2
    Member Crouch's Avatar
    Join Date
    Oct 2004
    Location
    the Netherlands
    Posts
    1,412
    Use report()
    Example of a typical line within your operator execute() or invoke() code:
    Code:
    self.report("INFO", "This text shows up in green")



  3. #3
    Member Atom's Avatar
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    11,220
    Thanks, I did not know about that method of an operator.

    A lot of my code is still currently running "freeform" and not as an operator so I have no self

    I did manage to come up with a way to colorize when code is running in a "freeform" state.
    Code:
    import platform
    
    import logging
    logging.basicConfig(level=logging.DEBUG,format='(%(threadName)-10s) %(message)s',)
    
    ############################################################################
    # Global variables here.
    ############################################################################
    PREFIX = "  "
    CONSOLE_PREFIX = ""
    CONSOLE_HANDLE = None
    CONSOLE_COLOR_CLEAR = None
    DEBUG = True
    LOGGING = True
    
    def toConsole(passedItem):
        global DEBUG, LOGGING, CONSOLE_HANDLE, CONSOLE_PREFIX
            
        if DEBUG == True:
            if platform.system() == "Windows":
                # Turn on colorization.
                ctypes.windll.kernel32.SetConsoleTextAttribute(CONSOLE_HANDLE, FOREGROUND_RED)
                    
            if LOGGING == True:
                if platform.system() == "Windows":
                    logging.debug(CONSOLE_PREFIX + "->" + passedItem)
                else:
                    #Linux or OSX.
                    logging.debug(bcolors.WARNING + CONSOLE_PREFIX + "->" + passedItem)
                    
            
            if platform.system() == "Windows":
                # Turn off colorization.
                ctypes.windll.kernel32.SetConsoleTextAttribute(CONSOLE_HANDLE, CONSOLE_COLOR_CLEAR)
        
    
    if (platform.system() == "Linux") or (platform.system() == "Darwin"):
        # For Linux or OSX, we only need to use ansii escape codes.
        class bcolors:
            HEADER = '\033[95m'
            OKBLUE = '\033[94m'
            OKGREEN = '\033[92m'
            WARNING = '\033[93m'
            FAIL = '\033[91m'
            ENDC = '\033[0m'
        
            def disable(self):
                self.HEADER = ''
                self.OKBLUE = ''
                self.OKGREEN = ''
                self.WARNING = ''
                self.FAIL = ''
                self.ENDC = ''
    
    if platform.system() == "Windows":
        # For windows we need to use ctypes win32.dll
        import ctypes
        
        # Constants from the Windows API
        STD_OUTPUT_HANDLE = -11
        FOREGROUND_BLUE_DRK    = 0x0001 # text color contains dark blue.
        FOREGROUND_GREEN_DRK    = 0x0002 # text color contains green.
        FOREGROUND_CYAN_DRK    = 0x0003 # text color contains cyan.
        FOREGROUND_RED_DRK    = 0x0004 # text color contains red.
        FOREGROUND_PLUM = 0x0005 # text color contains purple.
        FOREGROUND_GOLD    = 0x0006 # text color contains gold.
        FOREGROUND_WHITE    = 0x0007 # text color contains white.
        FOREGROUND_GREY    = 0x0008 # text color contains grey.
        FOREGROUND_BLUE    = 0x0009 # text color contains blue.
        FOREGROUND_IVORY    = 0x000f # text color contains ivory.
        FOREGROUND_YELLOW    = 0x000e # text color contains yellow.
        FOREGROUND_PINK    = 0x000d # text color contains pink.
        FOREGROUND_RED    = 0x000c # text color contains red.
        FOREGROUND_CYAN    = 0x000b # text color contains cyan.
        FOREGROUND_GREEN    = 0x000a # text color contains green.
        
        
        def get_csbi_attributes(handle):
            # Based on IPython's winconsole.py, written by Alexander Belchenko
            import struct
            csbi = ctypes.create_string_buffer(22)
            res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(handle, csbi)
            assert res
        
            (bufx, bufy, curx, cury, wattr,
            left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
            return wattr
    
        CONSOLE_HANDLE = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
        try:
            CONSOLE_COLOR_CLEAR = get_csbi_attributes(CONSOLE_HANDLE)
        except:
            pass
        
    toConsole("My string and my #" + str(10) +".")
    print("here")
    I have not tested the Linux or OSX portion, but from what I read ANSII support is automatic in the consoles for those OS's. So a simple escape sequence can turn color on and off.
    OSX Mac Mini i7 @ 2.6ghz 16Gb Ram (no GPU)
    Windows 7 8Gb, GeForce GTX 660 2Gb Ram AMD Hexcore @ 2.7Ghz.
    Atom's Links Page



  4. #4
    Originally Posted by Atom View Post
    I have not tested the Linux or OSX portion, but from what I read ANSII support is automatic in the consoles for those OS's. So a simple escape sequence can turn color on and off.
    Tested in Linux (ubuntu 12.04) with blender 2.68 ... works!

    0a Screenshot - 07272013 - 02:13:07 PM.png



  5. #5
    Member
    Join Date
    Aug 2010
    Location
    Moon
    Posts
    181
    Wow! That's nice research!



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •