<commit_before>import pymel.core as pymel
import logging
from classNameMap import NameMap
from classRigElement import RigElement
from classRigCtrl import RigCtrl
from omtk.libs import libPymel, libAttr

'''
This is the baseclass for anything that can be Build/Unbuild


A RigPart serialized respect the maya architecture and can be vulgarised to a node.
This allow us to expend the autorigger to software supporting compounds (xsi) or digital assets (houdini).

To build a RigPart, use the build and unbuild function.
To manage a rigpart, use the build() and unbuild() function.
Also, a rigpart have usefull properties like @inputs and @outputs.

'''

def _rget_by_type(val, type):
    for key, val in val.__dict__.iteritems():
        if isinstance(val, type):
            yield val
        elif hasattr(val, '__getItem__'):
            for subval in val:
                if isinstance(subval, type):
                    yield subval

class RigPart(RigElement):

    # RigElement overrides
    def isBuilt(self):
        return self.grp_anm is not None or self.grp_rig is not None


    @property
    def outputs(self):
        return self.__dict__['_outputs']


    # todo: since args is never used, maybe use to instead of _input?
    def __init__(self, _input=[], *args, **kwargs):
        super(RigPart, self).__init__(*args, **kwargs)
        self.iCtrlIndex = 2
        self.grp_anm = None
        self.grp_rig = None
        self.canPinTo = True # If raised, the network can be used as a space-switch pin-point
        self._pNameMapAnm = None
        self._pNameMapRig = None

        #  since we're using hook on inputs, assign it last!
        self.input = _input

    def __repr__(self):
        # TODO: Never crash on __repr__
        assert(hasattr(self, '_pNameMapAnm'))
        return '{0} ({1})'.format(str(self._pNameMapAnm), self.__class__.__name__ )

    def __setattr__(self, key, val):
        self.__dict__[key] = val
        # todo: find a faster way? (properties don't work since we need access via libSerialization)
        if key == 'input':
            self._post_setattr_inputs()
            self._chain = libPymel.PyNodeChain(self.input) # todo: approve PyNodeChain class


    # Even when nothing is build, it's usefull to access properties like namemaps.
    # This method is called automaticly when self.inputs is changed.
    def _post_setattr_inputs(self):
        oRef = next(iter(self.input), None)
        if oRef is not None:
            self._pNameMapAnm = NameMap(oRef, _sType='anm')
            self._pNameMapRig = NameMap(oRef, _sType='rig')
            self._oParent = oRef.getParent() if oRef is not None else None

    def build(self, _bCreateGrpAnm=True, _bCreateGrpRig=True, *args, **kwargs):
        '''
        if len(self.input) == 0:
            raise Exception("No inputs defined for {0}".format(self))
        assert(hasattr(self, '_pNameMapAnm'))
        assert(self._pNameMapAnm is not None)
        assert(hasattr(self, '_pNameMapRig'))
        assert(self._pNameMapRig is not None)
        '''
        if self._pNameMapAnm is None:
            self._pNameMapAnm = NameMap('untitled')

        if self._pNameMapRig is None:
            self._pNameMapRig = NameMap('untitled')

        logging.info('Building {0}'.format(self._pNameMapRig.Serialize()))

        '''
        if len(self.input) == 0:
            logging.error("[RigPart:Build] Can't build, inputs is empty"); return False
        '''

        if _bCreateGrpAnm:
            self.grp_anm = pymel.createNode('transform', name=self._pNameMapAnm.Serialize(self.__class__.__name__.lower(), _sType='anms'))
        if _bCreateGrpRig:
            self.grp_rig = pymel.createNode('transform', name=self._pNameMapRig.Serialize(self.__class__.__name__.lower(), _sType='rigs'))

    def unbuild(self):
        # Ensure that there's no more connections in the input chain
        for obj in self.input:
            if isinstance(obj, pymel.nodetypes.Transform):
                libAttr.disconnectAttr(obj.tx)
                libAttr.disconnectAttr(obj.ty)
                libAttr.disconnectAttr(obj.tz)<commit_msg>fix grouping bug related to nomenclature<commit_after>import pymel.core as pymel
import logging
from classNameMap import NameMap
from classRigElement import RigElement
from classRigCtrl import RigCtrl
from omtk.libs import libPymel, libAttr

'''
This is the baseclass for anything that can be Build/Unbuild


A RigPart serialized respect the maya architecture and can be vulgarised to a node.
This allow us to expend the autorigger to software supporting compounds (xsi) or digital assets (houdini).

To build a RigPart, use the build and unbuild function.
To manage a rigpart, use the build() and unbuild() function.
Also, a rigpart have usefull properties like @inputs and @outputs.

'''

def _rget_by_type(val, type):
    for key, val in val.__dict__.iteritems():
        if isinstance(val, type):
            yield val
        elif hasattr(val, '__getItem__'):
            for subval in val:
                if isinstance(subval, type):
                    yield subval

class RigPart(RigElement):

    # RigElement overrides
    def isBuilt(self):
        return self.grp_anm is not None or self.grp_rig is not None


    @property
    def outputs(self):
        return self.__dict__['_outputs']


    # todo: since args is never used, maybe use to instead of _input?
    def __init__(self, _input=[], *args, **kwargs):
        super(RigPart, self).__init__(*args, **kwargs)
        self.iCtrlIndex = 2
        self.grp_anm = None
        self.grp_rig = None
        self.canPinTo = True # If raised, the network can be used as a space-switch pin-point
        self._pNameMapAnm = None
        self._pNameMapRig = None

        #  since we're using hook on inputs, assign it last!
        self.input = _input

    def __repr__(self):
        # TODO: Never crash on __repr__
        assert(hasattr(self, '_pNameMapAnm'))
        return '{0} ({1})'.format(str(self._pNameMapAnm), self.__class__.__name__ )

    def __setattr__(self, key, val):
        self.__dict__[key] = val
        # todo: find a faster way? (properties don't work since we need access via libSerialization)
        if key == 'input':
            self._post_setattr_inputs()
            self._chain = libPymel.PyNodeChain(self.input) # todo: approve PyNodeChain class


    # Even when nothing is build, it's usefull to access properties like namemaps.
    # This method is called automaticly when self.inputs is changed.
    def _post_setattr_inputs(self):
        oRef = next(iter(self.input), None)
        if oRef is not None:
            self._pNameMapAnm = NameMap(oRef, _sType='anm')
            self._pNameMapRig = NameMap(oRef, _sType='rig')
            self._oParent = oRef.getParent() if oRef is not None else None

    def build(self, _bCreateGrpAnm=True, _bCreateGrpRig=True, *args, **kwargs):
        '''
        if len(self.input) == 0:
            raise Exception("No inputs defined for {0}".format(self))
        assert(hasattr(self, '_pNameMapAnm'))
        assert(self._pNameMapAnm is not None)
        assert(hasattr(self, '_pNameMapRig'))
        assert(self._pNameMapRig is not None)
        '''
        if self._pNameMapAnm is None:
            self._pNameMapAnm = NameMap('untitled')

        if self._pNameMapRig is None:
            self._pNameMapRig = NameMap('untitled')

        logging.info('Building {0}'.format(self._pNameMapRig.Serialize()))

        '''
        if len(self.input) == 0:
            logging.error("[RigPart:Build] Can't build, inputs is empty"); return False
        '''

        if _bCreateGrpAnm:
            self.grp_anm = pymel.createNode('transform', name=self._pNameMapAnm.Serialize(self.__class__.__name__.lower(), _sType='anm'))
        if _bCreateGrpRig:
            self.grp_rig = pymel.createNode('transform', name=self._pNameMapRig.Serialize(self.__class__.__name__.lower(), _sType='rig'))

    def unbuild(self):
        # Ensure that there's no more connections in the input chain
        for obj in self.input:
            if isinstance(obj, pymel.nodetypes.Transform):
                libAttr.disconnectAttr(obj.tx)
                libAttr.disconnectAttr(obj.ty)
                libAttr.disconnectAttr(obj.tz)
{'fraction_non_alphanumeric': 0.09623987425946076, 'fraction_numerical': 0.0016926611050658929, 'mean_word_length': 2.692857142857143, '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': '349342', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2494, 'n_words': 740}