Untitled

 avatar
unknown
plain_text
3 years ago
6.3 kB
8
Indexable
import rigging_widgets.rig_builder as rig_builder
import maya.cmds as mc
import site
import inspect
import logging
import gc

MODULE_ROOT = 'iRig'
USER = os.getenv('USERNAME')
__file__ = 'D:/Pipeline/{}/dev/git_repo/iRig/extra\iRigLoader.py'.format(USER)
SHOW_NAME = os.getenv('TT_PROJCODE')

logger = logging.getLogger('irig_loader')


def reset_by_path(module_path=None):
    if module_path is None:
        module_path = os.path.dirname('__file__')
        print 'module_path:',module_path
    module_path = os.path.abspath(module_path).lower()  # cleaner comparison later
    print 'module_path:',module_path
    sys_key_to_delete = list()
    sys_module_to_be_deleted = list()
    for key, module in sys.modules.iteritems():  # Iterate through currently loaded modules
        # try:
        if module and module.__name__ not in sys.builtin_module_names:
            try:
                f = inspect.getfile(module)
            except Exception as e:
                logger.critical('Unable to inspect module: %s' % module.__name__)
                continue
            module_file_path = os.path.abspath(f.lower())
            if module_file_path == __file__.lower():  # Removing this script will break the process.
                continue
            if getattr(module, "__dnr__", False):
                logger.debug("Skipping %s, found __dnr__" % key)
                continue

            if module_file_path.startswith(module_path):
                logger.info("Removing %s" % key)
                sys_key_to_delete.append(key)
                sys_module_to_be_deleted.append(module)


    # Remove modules in to_delete from current session globals() if they're referenced in some way.
    global_key_to_delete = list()
    for key, module in globals().iteritems():
        if module in sys_module_to_be_deleted:
            global_key_to_delete.append(key)

    for global_key in global_key_to_delete:
        logger.info('Deleting from globals(): {}'.format(global_key))
        del (globals()[global_key])

    # If we'd deleted the module in the loop above, it would have changed the size of the dictionary and
    # testing the loop. So now we go over the list we made and delete all the modules
    for module in sys_key_to_delete:
        logger.info('Removing from sys: {}'.format(module))
        del (sys.modules[module])


def add_dir_pth_files_to_site(path='', index=None):
    pre_add_length = len(sys.path)
    if path and os.path.exists(path):
        dirs_to_source = [
            path + '/src/',
            path + '/src/iRig/',
            path + '/src/iRig/iRig_maya/framework/'
        ]
        for source_dir in dirs_to_source:
            sys.path.insert(0, source_dir)

        if isinstance(index, int) and index <= pre_add_length:
            added_path_list = sys.path[pre_add_length:]
            for pos, sys_path in enumerate(added_path_list):
                target_index = index + pos
                logger.debug('Inserting at index: [{}] - {}'.format(target_index, sys_path))
                sys.path.insert(target_index, sys_path)
            logger.debug('Removing duplicates')
            site.removeduppaths()
            
def teardown():
    if 'MAYA_CALLBACKS' in globals():
        global MAYA_CALLBACKS
        if MAYA_CALLBACKS:
            import maya.OpenMaya as om
            for callback in MAYA_CALLBACKS:
                logging.critical('Removing MMessage callback: %s' % callback)
                om.MMessage.removeCallback(callback)
    if 'ALL_SIGNALS' in globals():
        global ALL_SIGNALS
        if ALL_SIGNALS:
            for s in ALL_SIGNALS:
                logging.getLogger('rig_build').info('Blocking signal: %s' % s)
                s._block = True
                s._slots = []
    if 'ALL_CLASS_SIGNALS' in globals():
        global ALL_CLASS_SIGNALS
        if ALL_CLASS_SIGNALS:
            for s in ALL_CLASS_SIGNALS:
                logging.getLogger('rig_build').info('Blocking class signals: %s' % s)
                s._map = {}
                        
def irig_reload(reopen_toolbox=False): 
    selected_iRig = 'D:/Pipeline/{}/dev/git_repo/iRig'.format(USER)
    os.environ['PIPE_DEV_MODE'] = 'True'

    teardown()
    
    # close iRig toolbox
    if mc.workspaceControl('iRig_Toolbox_Dock_Control', q=True, exists=True):
        mc.workspaceControl('iRig_Toolbox_Dock_Control', e=True, close=True)


    # Close framework GUI
    widget_name = ['Rig Builder', 'Face Builder']
    for workspace_control in mc.lsUI(type='workspaceControl'):
        if 'WorkspaceControl' in workspace_control:
            for name in widget_name:
                if mc.workspaceControl(workspace_control, q=True, label=True) == name:
                    mc.workspaceControl(workspace_control, e=True, close=True)
    
    
    existing_index = None
    module_search = os.path.normpath('/{}/'.format(MODULE_ROOT))
    print 'module_search:',module_search
    for i, p in enumerate(sys.path):
        p_norm = os.path.normpath(p)
        if module_search.lower() in p_norm.lower():
            if existing_index is None:
                existing_index = i
            sys.path.remove(p)
            reset_by_path(module_path=p)

    add_dir_pth_files_to_site(selected_iRig, index = existing_index)

    logger.info('{} was loaded from: {}'.format(MODULE_ROOT, selected_iRig))

    gc.collect()
    
    
    
    if reopen_toolbox:
        from iRig.iRig_maya.toolbox import toolboxUI
        reload(toolboxUI)
        toolboxUI.launch()
    else:
        current_show_dev_mode = os.getenv('SHOW_DEV_MODE', 'False')
        
        if current_show_dev_mode == 'True':
            SHOWS_DIR = "D:/Pipeline/{}/dev/git_repo/{}".format(USER, SHOW_NAME)
        else:
            SHOWS_DIR = 'G:/Rigging/Shows/{}'.format(SHOW_NAME)

        # add show to sys.path
        while SHOWS_DIR in sys.path:
            sys.path.remove(SHOWS_DIR)
        sys.path.insert(0, SHOWS_DIR)
    
        import widget_launchers.launch_widgets as lw
        reload(lw)
        lw.launch_rig_widget()



irig_reload(reopen_toolbox=False)

Editor is loading...