<commit_before>import time
import os
import rman
import bpy
import sys
from .rman_constants import RFB_VIEWPORT_MAX_BUCKETS, RMAN_RENDERMAN_BLUE
from .rman_scene import RmanScene
from .rman_scene_sync import RmanSceneSync
from. import rman_spool
from. import chatserver
from .rfb_logger import rfb_log
import socketserver
import threading
import subprocess
import ctypes
import numpy

# for viewport buckets
import gpu
from gpu_extras.batch import batch_for_shader

# utils
from .rfb_utils.envconfig_utils import envconfig
from .rfb_utils import string_utils
from .rfb_utils import display_utils
from .rfb_utils import scene_utils
from .rfb_utils.prefs_utils import get_pref

# config
from .rman_config import __RFB_CONFIG_DICT__ as rfb_config

# roz stats
from .rman_stats import RfBStatsManager

__RMAN_RENDER__ = None
__RMAN_IT_PORT__ = -1
__BLENDER_DSPY_PLUGIN__ = None
__DRAW_THREAD__ = None
__RMAN_STATS_THREAD__ = None

def __turn_off_viewport__():
    '''
    Loop through all of the windows/areas and turn shading to SOLID
    for all view_3d areas.
    '''
    rfb_log().debug("Attempting to turn off viewport render")
    for window in bpy.context.window_manager.windows:
        for area in window.screen.areas:
            if area.type == 'VIEW_3D':
                for space in area.spaces:
                    if space.type == 'VIEW_3D':
                        space.shading.type = 'SOLID'    
            area.tag_redraw()     

def __update_areas__():
    for window in bpy.context.window_manager.windows:
        for area in window.screen.areas:
            area.tag_redraw()

class ItHandler(chatserver.ItBaseHandler):

    def dspyRender(self):
        global __RMAN_RENDER__
        if not __RMAN_RENDER__.is_running:                        
            bpy.ops.render.render(layer=bpy.context.view_layer.name)             

    def dspyIPR(self):
        global __RMAN_RENDER__
        if __RMAN_RENDER__.rman_interactive_running:
            crop = []
            for c in self.msg.getOpt('crop').split(' '):
                crop.append(float(c))
            if len(crop) == 4:
                __RMAN_RENDER__.rman_scene_sync.update_cropwindow(crop)

    def stopRender(self):
        global __RMAN_RENDER__
        rfb_log().debug("Stop Render Requested.")
        if __RMAN_RENDER__.rman_interactive_running:
            __turn_off_viewport__()
        __RMAN_RENDER__.del_bl_engine() 

    def selectObjectById(self):
        global __RMAN_RENDER__

        obj_id = int(self.msg.getOpt('id', '0'))
        if obj_id < 0 or not (obj_id in __RMAN_RENDER__.rman_scene.obj_hash):
            return
        name = __RMAN_RENDER__.rman_scene.obj_hash[obj_id]
        rfb_log().debug('ID: %d Obj Name: %s' % (obj_id, name))
        obj = bpy.context.scene.objects[name]
        if obj:
            if bpy.context.view_layer.objects.active:
                bpy.context.view_layer.objects.active.select_set(False)
            obj.select_set(True)
            bpy.context.view_layer.objects.active = obj

    def selectSurfaceById(self):
        self.selectObjectById()
        window = bpy.context.window_manager.windows[0]
        if window.screen:
            for a in window.screen.areas:
                if a.type == "PROPERTIES":
                    for s in a.spaces:
                        if s.type == "PROPERTIES":
                            try:
                                s.context = "MATERIAL"
                            except:
                                pass
                            return

def start_cmd_server():

    global __RMAN_IT_PORT__

    if __RMAN_IT_PORT__ != -1:
        return __RMAN_IT_PORT__

    # zero port makes the OS pick one
    host, port = "localhost", 0

    # install handler
    chatserver.protocols['it'] = ItHandler

    # Create the server, binding to localhost on some port
    server = socketserver.TCPServer((host, port),
                                    chatserver.CommandHandler)
    ip, port = server.server_address

    thread = threading.Thread(target=server.serve_forever)

    # Exit the server thread when the main thread terminates
    thread.daemon = True
    thread.start()

    __RMAN_IT_PORT__ = port

    return __RMAN_IT_PORT__        

def draw_threading_func(db):
    refresh_rate = get_pref('rman_viewport_refresh_rate', default=0.01)
    while db.rman_is_live_rendering:
        if not scene_utils.any_areas_shading():
            # if there are no 3d viewports, stop IPR
            db.del_bl_engine()
            break
        if db.rman_is_viewport_rendering:
            try:
                db.bl_engine.tag_redraw()
                time.sleep(refresh_rate)
            except ReferenceError as e:
                # calling tag_redraw has failed. This might mean
                # that there are no more view_3d areas that are shading. Try to
                # stop IPR.
                #rfb_log().debug("Error calling tag_redraw (%s). Aborting..." % str(e))
                db.del_bl_engine()
                return
        else:
            # rendering to "it" add a 1 second sleep
            time.sleep(1.0)

def call_stats_update_payloads(db):
    panel_region = None
    # find the region where the live stats panel is in
    for window in bpy.context.window_manager.windows:
        for area in window.screen.areas:
            if area.type == 'VIEW_3D':
                for region in area.regions:
                    if region.type == 'UI':
                        panel_region = region
                        break   

    wait_before_redraw = 0.0
    while db.rman_running:
        if not db.bl_engine:
            break
        db.stats_mgr.update_payloads()
        if wait_before_redraw >= 1.0:
            # wait around 1.0 seconds before telling the panel
            # to redraw. Telling the panel to redraw
            # any faster seems to cause issues.
            panel_region.tag_redraw()
            wait_before_redraw = 0.0
        time.sleep(0.1)
        wait_before_redraw += 0.1
<commit_msg>Increase the wait time on the stats panel redraw to 2 seconds.<commit_after>import time
import os
import rman
import bpy
import sys
from .rman_constants import RFB_VIEWPORT_MAX_BUCKETS, RMAN_RENDERMAN_BLUE
from .rman_scene import RmanScene
from .rman_scene_sync import RmanSceneSync
from. import rman_spool
from. import chatserver
from .rfb_logger import rfb_log
import socketserver
import threading
import subprocess
import ctypes
import numpy

# for viewport buckets
import gpu
from gpu_extras.batch import batch_for_shader

# utils
from .rfb_utils.envconfig_utils import envconfig
from .rfb_utils import string_utils
from .rfb_utils import display_utils
from .rfb_utils import scene_utils
from .rfb_utils.prefs_utils import get_pref

# config
from .rman_config import __RFB_CONFIG_DICT__ as rfb_config

# roz stats
from .rman_stats import RfBStatsManager

__RMAN_RENDER__ = None
__RMAN_IT_PORT__ = -1
__BLENDER_DSPY_PLUGIN__ = None
__DRAW_THREAD__ = None
__RMAN_STATS_THREAD__ = None

def __turn_off_viewport__():
    '''
    Loop through all of the windows/areas and turn shading to SOLID
    for all view_3d areas.
    '''
    rfb_log().debug("Attempting to turn off viewport render")
    for window in bpy.context.window_manager.windows:
        for area in window.screen.areas:
            if area.type == 'VIEW_3D':
                for space in area.spaces:
                    if space.type == 'VIEW_3D':
                        space.shading.type = 'SOLID'    
            area.tag_redraw()     

def __update_areas__():
    for window in bpy.context.window_manager.windows:
        for area in window.screen.areas:
            area.tag_redraw()

class ItHandler(chatserver.ItBaseHandler):

    def dspyRender(self):
        global __RMAN_RENDER__
        if not __RMAN_RENDER__.is_running:                        
            bpy.ops.render.render(layer=bpy.context.view_layer.name)             

    def dspyIPR(self):
        global __RMAN_RENDER__
        if __RMAN_RENDER__.rman_interactive_running:
            crop = []
            for c in self.msg.getOpt('crop').split(' '):
                crop.append(float(c))
            if len(crop) == 4:
                __RMAN_RENDER__.rman_scene_sync.update_cropwindow(crop)

    def stopRender(self):
        global __RMAN_RENDER__
        rfb_log().debug("Stop Render Requested.")
        if __RMAN_RENDER__.rman_interactive_running:
            __turn_off_viewport__()
        __RMAN_RENDER__.del_bl_engine() 

    def selectObjectById(self):
        global __RMAN_RENDER__

        obj_id = int(self.msg.getOpt('id', '0'))
        if obj_id < 0 or not (obj_id in __RMAN_RENDER__.rman_scene.obj_hash):
            return
        name = __RMAN_RENDER__.rman_scene.obj_hash[obj_id]
        rfb_log().debug('ID: %d Obj Name: %s' % (obj_id, name))
        obj = bpy.context.scene.objects[name]
        if obj:
            if bpy.context.view_layer.objects.active:
                bpy.context.view_layer.objects.active.select_set(False)
            obj.select_set(True)
            bpy.context.view_layer.objects.active = obj

    def selectSurfaceById(self):
        self.selectObjectById()
        window = bpy.context.window_manager.windows[0]
        if window.screen:
            for a in window.screen.areas:
                if a.type == "PROPERTIES":
                    for s in a.spaces:
                        if s.type == "PROPERTIES":
                            try:
                                s.context = "MATERIAL"
                            except:
                                pass
                            return

def start_cmd_server():

    global __RMAN_IT_PORT__

    if __RMAN_IT_PORT__ != -1:
        return __RMAN_IT_PORT__

    # zero port makes the OS pick one
    host, port = "localhost", 0

    # install handler
    chatserver.protocols['it'] = ItHandler

    # Create the server, binding to localhost on some port
    server = socketserver.TCPServer((host, port),
                                    chatserver.CommandHandler)
    ip, port = server.server_address

    thread = threading.Thread(target=server.serve_forever)

    # Exit the server thread when the main thread terminates
    thread.daemon = True
    thread.start()

    __RMAN_IT_PORT__ = port

    return __RMAN_IT_PORT__        

def draw_threading_func(db):
    refresh_rate = get_pref('rman_viewport_refresh_rate', default=0.01)
    while db.rman_is_live_rendering:
        if not scene_utils.any_areas_shading():
            # if there are no 3d viewports, stop IPR
            db.del_bl_engine()
            break
        if db.rman_is_viewport_rendering:
            try:
                db.bl_engine.tag_redraw()
                time.sleep(refresh_rate)
            except ReferenceError as e:
                # calling tag_redraw has failed. This might mean
                # that there are no more view_3d areas that are shading. Try to
                # stop IPR.
                #rfb_log().debug("Error calling tag_redraw (%s). Aborting..." % str(e))
                db.del_bl_engine()
                return
        else:
            # rendering to "it" add a 1 second sleep
            time.sleep(1.0)

def call_stats_update_payloads(db):
    panel_region = None
    # find the region where the live stats panel is in
    for window in bpy.context.window_manager.windows:
        for area in window.screen.areas:
            if area.type == 'VIEW_3D':
                for region in area.regions:
                    if region.type == 'UI':
                        panel_region = region
                        break   

    wait_before_redraw = 0.0
    while db.rman_running:
        if not db.bl_engine:
            break
        db.stats_mgr.update_payloads()
        if wait_before_redraw >= 2.0:
            # wait around 2.0 seconds before telling the panel
            # to redraw. Telling the panel to redraw
            # too quickly seems to cause issues.
            panel_region.tag_redraw()
            wait_before_redraw = 0.0
        time.sleep(0.1)
        wait_before_redraw += 0.1

{'fraction_non_alphanumeric': 0.07258464079273327, 'fraction_numerical': 0.005202312138728324, 'mean_word_length': 2.019446522064323, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6292988', 'n_tokens_mistral': 3788, 'n_tokens_neox': 3679, 'n_words': 911}