<commit_before>import functools
import inspect
import os
import logging
from collections import defaultdict

import core

from maya import cmds
import pymel.core as pymel
from maya import OpenMaya
from omtk.core import api
from omtk.core import classModule
from omtk.libs import libPython
from omtk.libs import libSkeleton
from omtk.ui import main_window
from omtk import ui_shared

from omtk.vendor import libSerialization
from omtk.vendor.Qt import QtCore, QtGui, QtWidgets

log = logging.getLogger('omtk')


class AutoRig(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(AutoRig, self).__init__()

        # Internal data
        self.root = None
        self.roots = []

        self.ui = main_window.Ui_OpenRiggingToolkit()
        self.ui.setupUi(self)

        version = api.get_version()
        self.setWindowTitle('Open Rigging Toolkit {}'.format(version))

        #
        # First update
        #

        self.import_networks()

        # Connect events
        self.ui.actionBuildAll.triggered.connect(self.on_build_all)
        self.ui.actionRebuildAll.triggered.connect(self.on_rebuild_all)
        self.ui.actionUnbuildAll.triggered.connect(self.on_unbuild_all)
        self.ui.actionImport.triggered.connect(self.on_import)
        self.ui.actionExport.triggered.connect(self.on_export)
        self.ui.actionUpdate.triggered.connect(self.on_update)
        self.ui.actionCreateModule.triggered.connect(self.on_btn_add_pressed)
        self.ui.actionMirrorJntsLToR.triggered.connect(self.on_mirror_influences_l_to_r)
        self.ui.actionMirrorJntsRToL.triggered.connect(self.on_mirror_influences_r_to_l)
        self.ui.actionMirrorSelection.triggered.connect(self.on_mirror_selection)
        self.ui.actionAddSelectedInfluencesToModule.triggered.connect(self.on_add_selected_influences_to_module)
        self.ui.actionAddSelectedMeshesToModule.triggered.connect(self.on_add_selected_meshes_to_module)
        self.ui.actionRemoveNodeFromModule.triggered.connect(self.on_removeFromModule)
        self.ui.actionShowPluginManager.triggered.connect(self.on_show_pluginmanager)
        self.ui.actionShowPreferences.triggered.connect(self.on_show_preferences)

        # Connect widget signals
        self.ui.widget_modules.needImportNetwork.connect(self.import_networks)
        self.ui.widget_modules.needExportNetwork.connect(self.export_networks)
        self.ui.widget_modules.deletedRig.connect(self.on_rig_deleted)
        self.ui.widget_jnts.onRightClick.connect(self.on_btn_add_pressed)

        self.callbacks_events = []
        self.callbacks_scene = []
        self.callbacks_nodes = None

        self.create_callbacks()

        # from omtk.core import plugin_manager
        # pm = plugin_manager.plugin_manager
        # failed_plugins = pm.get_failed_plugins()
        # if failed_plugins:
        #     log.warning("The following plugins failed to load: {0}".format(', '.join(str(p) for p in failed_plugins)))

        # Build the template menu
        available_templates = self._get_available_templates()
        if available_templates:
            menu_template = QtWidgets.QMenu(self.ui.menubar)
            menu_template.setTitle("Templates")

            for template in available_templates:
                template_name = os.path.basename(template)
                action = QtWidgets.QAction(template_name, self)
                action.triggered.connect(functools.partial(self.action_import_template, template))
                menu_template.addAction(action)

            self.ui.menubar.addAction(menu_template.menuAction())

    def action_import_template(self, path):
        log.info('Importing template {0}'.format(path))
        cmds.file(path, i=True)
        self.on_update()

    def _get_available_templates(self):
        results = set()

        for plugin_dir in os.environ.get('OMTK_PLUGINS', []).split(os.pathsep):
            if not os.path.exists(plugin_dir):
                continue

            template_dir = os.path.join(plugin_dir, 'templates')
            if not os.path.exists(template_dir):
                continue

            for filename in os.listdir(template_dir):
                if filename.endswith('.ma') or filename.endswith('.mb'):<commit_msg>Remove all existing rigs when importing a template<commit_after>import functools
import inspect
import os
import logging
from collections import defaultdict

import core

from maya import cmds
import pymel.core as pymel
from maya import OpenMaya
from omtk.core import api
from omtk.core import classModule
from omtk.libs import libPython
from omtk.libs import libSkeleton
from omtk.ui import main_window
from omtk import ui_shared

from omtk.vendor import libSerialization
from omtk.vendor.Qt import QtCore, QtGui, QtWidgets

log = logging.getLogger('omtk')


class AutoRig(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(AutoRig, self).__init__()

        # Internal data
        self.root = None
        self.roots = []

        self.ui = main_window.Ui_OpenRiggingToolkit()
        self.ui.setupUi(self)

        version = api.get_version()
        self.setWindowTitle('Open Rigging Toolkit {}'.format(version))

        #
        # First update
        #

        self.import_networks()

        # Connect events
        self.ui.actionBuildAll.triggered.connect(self.on_build_all)
        self.ui.actionRebuildAll.triggered.connect(self.on_rebuild_all)
        self.ui.actionUnbuildAll.triggered.connect(self.on_unbuild_all)
        self.ui.actionImport.triggered.connect(self.on_import)
        self.ui.actionExport.triggered.connect(self.on_export)
        self.ui.actionUpdate.triggered.connect(self.on_update)
        self.ui.actionCreateModule.triggered.connect(self.on_btn_add_pressed)
        self.ui.actionMirrorJntsLToR.triggered.connect(self.on_mirror_influences_l_to_r)
        self.ui.actionMirrorJntsRToL.triggered.connect(self.on_mirror_influences_r_to_l)
        self.ui.actionMirrorSelection.triggered.connect(self.on_mirror_selection)
        self.ui.actionAddSelectedInfluencesToModule.triggered.connect(self.on_add_selected_influences_to_module)
        self.ui.actionAddSelectedMeshesToModule.triggered.connect(self.on_add_selected_meshes_to_module)
        self.ui.actionRemoveNodeFromModule.triggered.connect(self.on_removeFromModule)
        self.ui.actionShowPluginManager.triggered.connect(self.on_show_pluginmanager)
        self.ui.actionShowPreferences.triggered.connect(self.on_show_preferences)

        # Connect widget signals
        self.ui.widget_modules.needImportNetwork.connect(self.import_networks)
        self.ui.widget_modules.needExportNetwork.connect(self.export_networks)
        self.ui.widget_modules.deletedRig.connect(self.on_rig_deleted)
        self.ui.widget_jnts.onRightClick.connect(self.on_btn_add_pressed)

        self.callbacks_events = []
        self.callbacks_scene = []
        self.callbacks_nodes = None

        self.create_callbacks()

        # from omtk.core import plugin_manager
        # pm = plugin_manager.plugin_manager
        # failed_plugins = pm.get_failed_plugins()
        # if failed_plugins:
        #     log.warning("The following plugins failed to load: {0}".format(', '.join(str(p) for p in failed_plugins)))

        # Build the template menu
        available_templates = self._get_available_templates()
        if available_templates:
            menu_template = QtWidgets.QMenu(self.ui.menubar)
            menu_template.setTitle("Templates")

            for template in available_templates:
                template_name = os.path.basename(template)
                action = QtWidgets.QAction(template_name, self)
                action.triggered.connect(functools.partial(self.action_import_template, template))
                menu_template.addAction(action)

            self.ui.menubar.addAction(menu_template.menuAction())

    def action_import_template(self, path):
        for root in self.roots:
            pymel.delete(root._network)
            self.on_rig_deleted(root)
        log.info('Importing template {0}'.format(path))
        
        cmds.file(path, i=True)
        self.on_update()

    def _get_available_templates(self):
        results = set()

        for plugin_dir in os.environ.get('OMTK_PLUGINS', []).split(os.pathsep):
            if not os.path.exists(plugin_dir):
                continue

            template_dir = os.path.join(plugin_dir, 'templates')
            if not os.path.exists(template_dir):
                continue

            for filename in os.listdir(template_dir):
                if filename.endswith('.ma') or filename.endswith('.mb'):
{'fraction_non_alphanumeric': 0.08777393310265283, 'fraction_numerical': 0.000461361014994233, 'mean_word_length': 3.0575573233504914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3919903', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2596, 'n_words': 408}