def patch_spaceswitch_dpspine_objects():
    """
    This function is not clean, but it can be used to generate spaceswitch object for certain module and after look at
    all space switch target information to replace to space switch object target that could used a ctrl to the new space
    switch object that will not be removed after an unbuild
    """

    # Get the rig instance in the scene (Now support only the first one found
    rig_net = libSerialization.get_networks_from_class('Rig')[0]
    rig_instance = libSerialization.import_network(rig_net)

    # Get all the module that we could need to patch
    to_patch = [module for module in rig_instance.modules if isinstance(module, rigDpSpine.DpSpine)]

    for module in to_patch:
        module_to_patch = module
        if isinstance(module_to_patch, rigDpSpine.DpSpine):
            # Find all connection that have from the ctrl ik down (COG) and replace it
            connected_to_fk_dwn = module_to_patch.ctrl_fk_dwn._network.message.outputs(s=False, d=True, p=True)

            for connection in connected_to_fk_dwn:
                attr_name = connection.shortName()
                if attr_name.find('targets') >= 0:
                    log.info('Reconnecting {0} from {1} to space switch node {2}'
                             .format(connection, module_to_patch.ctrl_ik_dwn, module_to_patch.ctrl_ik_dwn._network))
                    connection.disconnect()
                    pymel.connectAttr(module_to_patch.ctrl_ik_dwn._network.message, connection, force=True)
    def test_export_network(self, epsilon=0.00001):
        pynode_a = pymel.createNode('transform')
        pynode_b = pymel.createNode('transform')

        old_instance = A()
        old_instance.ex_int = 42
        old_instance.ex_float = 3.14159
        old_instance.ex_str = 'Hello World'
        old_instance.ex_None = None
        old_instance.ex_list_pynode = [None, pynode_a, None, pynode_b]

        #
        # Ensure consistency when exporting to network
        #
        n = libSerialization.export_network(old_instance)
        network_ex_int = n.ex_int.get()
        network_ex_float = n.ex_float.get()
        network_ex_str = n.ex_str.get()
        self.assertTrue(network_ex_int == old_instance.ex_int)
        self.assertTrue(abs(network_ex_float- old_instance.ex_float) < epsilon)
        self.assertTrue(network_ex_str == old_instance.ex_str)

        # Note: libSerialization will NEVER export a None value since the type cannot be resolved.
        self.assertTrue(not n.hasAttr('ex_None'))

        #
        # Ensure consistency when importing from network
        #
        new_instance = libSerialization.import_network(n)
        self.assertTrue(network_ex_int == new_instance.ex_int)
        self.assertTrue(abs(network_ex_float- new_instance.ex_float) < epsilon)
        self.assertTrue(network_ex_str == new_instance.ex_str)
Esempio n. 3
0
def find():
    """
    :return: All the rigs embedded in the current maya scene.
    """
    #TODO: Find why when a scene is open for a long time, this function is slower
    networks = libSerialization.getNetworksByClass('Rig')
    return [libSerialization.import_network(network, module='omtk') for network in networks]
Esempio n. 4
0
    def test_export_network(self, epsilon=0.00001):
        pynode_a = pymel.createNode('transform')
        pynode_b = pymel.createNode('transform')

        old_instance = A()
        old_instance.ex_int = 42
        old_instance.ex_float = 3.14159
        old_instance.ex_str = 'Hello World'
        old_instance.ex_None = None
        old_instance.ex_list_pynode = [None, pynode_a, None, pynode_b]

        #
        # Ensure consistency when exporting to network
        #
        n = libSerialization.export_network(old_instance)
        network_ex_int = n.ex_int.get()
        network_ex_float = n.ex_float.get()
        network_ex_str = n.ex_str.get()
        self.assertTrue(network_ex_int == old_instance.ex_int)
        self.assertTrue(
            abs(network_ex_float - old_instance.ex_float) < epsilon)
        self.assertTrue(network_ex_str == old_instance.ex_str)

        # Note: libSerialization will NEVER export a None value since the type cannot be resolved.
        self.assertTrue(not n.hasAttr('ex_None'))

        #
        # Ensure consistency when importing from network
        #
        new_instance = libSerialization.import_network(n)
        self.assertTrue(network_ex_int == new_instance.ex_int)
        self.assertTrue(
            abs(network_ex_float - new_instance.ex_float) < epsilon)
        self.assertTrue(network_ex_str == new_instance.ex_str)
Esempio n. 5
0
def unbuild_all():
    networks = libSerialization.getNetworksByClass('Rig')
    for network in networks:
        rigroot = libSerialization.import_network(network)
        rigroot.unbuild()
        pymel.delete(network)
        # Write changes to scene
        network = libSerialization.export_network(rigroot)
        pymel.select(network)
Esempio n. 6
0
def find():
    """
    :return: All the rigs embedded in the current maya scene.
    """
    # TODO: Find why when a scene is open for a long time, this function is slower
    networks = libSerialization.get_networks_from_class('Rig')
    results = [libSerialization.import_network(network, module='omtk') for network in networks]
    results = filter(None, results)  # Prevent un-serializable networks from passing through.
    return results
Esempio n. 7
0
def build_all():
    """
    Build all the rigs embedded in the current maya scene.
    """
    networks = libSerialization.getNetworksByClass('Rig')
    for network in networks:
        rigroot = libSerialization.import_network(network)
        if rigroot.build():
            pymel.delete(network)
            libSerialization.export_network(rigroot)
Esempio n. 8
0
def unbuild_all(strict=False):
    networks = libSerialization.get_networks_from_class('Rig')
    for network in networks:
        rigroot = libSerialization.import_network(network)
        if not rigroot:
            log.warning("Error importing rig network {0}".format(network))
            continue
        rigroot.unbuild(strict=strict)
        pymel.delete(network)
        # Write changes to scene
        network = libSerialization.export_network(rigroot)
        pymel.select(network)
def patch_spaceswitch_data_into_network(sel=None):
    if sel is None:
        sel = pymel.selected()

    networks = libSerialization.get_connected_networks(sel, key=lambda
        net: libSerialization.is_network_from_class(net, 'BaseCtrl'))

    for net in networks:
        ctrl_instance = libSerialization.import_network(net)
        data = ctrl_instance.get_spaceswitch_enum_targets()

        if data:
            # Create missing attributes if needed
            if not net.hasAttr('targets_indexes'):
                log.info('targets_indexes attribute is missing on network {0}. It will be created'.format(net))
                pymel.addAttr(net, longName='targets_indexes', at='long', multi=True)
            if not net.hasAttr('targets'):
                log.info('targets attribute is missing on network {0}. It will be created'.format(net))
                pymel.addAttr(net, longName='targets', at='message', multi=True)
            if not net.hasAttr('local_index'):
                log.info('targets_indexes attribute is missing on network {0}. It will be created'.format(net))
                pymel.addAttr(net, longName='local_index', at='long')

            # Add data if needed
            for index, (name, obj) in data.items():
                target_data = net.targets.get()
                indexes_data = net.targets_indexes.get()
                if obj not in target_data:
                    if index not in indexes_data:
                        if obj is not None:
                            if isinstance(obj, pymel.nt.Joint):
                                log.info('Patching network {0} space switch data named {1} - Index {2} on object {3}'
                                     .format(net.name(), name, index, obj))
                                pymel.connectAttr(obj.message, net.targets[len(target_data)])
                            elif isinstance(obj, pymel.nt.Transform):
                                key = lambda network: libSerialization.is_network_from_class(network, 'Node')
                                ctrl_net = libSerialization.get_connected_networks(obj, key)
                                if ctrl_net > 0:
                                    log.info('Patching network {0} space switch data named {1} - '
                                             'Index {2} on object {3}'.format(net.name(), name, index, ctrl_net[0]))
                                    pymel.connectAttr(ctrl_net[0].message, net.targets[len(target_data)])
                            net.targets_indexes[len(target_data)].set(index)
                        elif name == 'Local':
                            log.info('Patching network {0} space switch data named {1} - Index {2} as local index'
                                     .format(net.name(), name, index))
                            net.local_index.set(index)
                    else:
                        if obj is not None:
                            log.warning('Object {0} have not been found in attribute targets but index {1} yes.'
                                        'This object will not be patched to prevent any index conflict. '
                                        'Look at your data to understand'.format(obj, index))
                else:
                    log.info('Space Switch object {0} is already included in the targets attributes'.format(obj))
Esempio n. 10
0
def unbuild_all(strict=False):
    networks = libSerialization.get_networks_from_class('Rig')
    for network in networks:
        rigroot = libSerialization.import_network(network)
        if not rigroot:
            log.warning("Error importing rig network {0}".format(network))
            continue
        rigroot.unbuild(strict=strict)
        pymel.delete(network)
        # Write changes to scene
        network = libSerialization.export_network(rigroot)
        pymel.select(network)
Esempio n. 11
0
def build_all(strict=False):
    """
    Build all the rigs embedded in the current maya scene.
    """
    networks = libSerialization.get_networks_from_class('Rig')
    for network in networks:
        rigroot = libSerialization.import_network(network)
        if not rigroot:
            log.warning("Error importing rig network {0}".format(network))
            continue
        if rigroot.build(strict=strict):
            pymel.delete(network)
            libSerialization.export_network(rigroot)
Esempio n. 12
0
def build_all(strict=False):
    """
    Build all the rigs embedded in the current maya scene.
    """
    networks = libSerialization.get_networks_from_class('Rig')
    for network in networks:
        rigroot = libSerialization.import_network(network)
        if not rigroot:
            log.warning("Error importing rig network {0}".format(network))
            continue
        if rigroot.build(strict=strict):
            pymel.delete(network)
            libSerialization.export_network(rigroot)
Esempio n. 13
0
def CallFnOnNetworkByClass(_sFn, _sCls):
    fnFilter = lambda x: libSerialization.isNetworkInstanceOfClass(x, _sCls)
    networks = libSerialization.getConnectedNetworks(pymel.selected(), key=fnFilter)
    for network in networks:
        rigPart = libSerialization.import_network(network)

        if not hasattr(rigPart, _sFn):
            logging.warning("Can't find attribute {0} in {1}".format(_sFn, network)); continue

        try:
            getattr(rigPart, _sFn)()
        except Exception as e:
            print(str(e))
Esempio n. 14
0
def find():
    """
    :return: All the rigs embedded in the current maya scene.
    """
    # TODO: Find why when a scene is open for a long time, this function is slower
    networks = libSerialization.get_networks_from_class('Rig')
    results = [
        libSerialization.import_network(network, module='omtk')
        for network in networks
    ]
    results = filter(
        None,
        results)  # Prevent un-serializable networks from passing through.
    return results
Esempio n. 15
0
def _get_modules_from_selection(sel=None):
    def get_rig_network_from_module(network):
        for plug in network.message.outputs(plugs=True):
            plug_node = plug.node()
            if not isinstance(plug_node, pymel.nodetypes.Network):
                continue
            if libSerialization.is_network_from_class(plug_node, 'Rig'):
                return plug_node
        return None

    def is_module_child_of_rig(network):
        """
        Allow us to recognize module directly connected to a 'Rig' network.
        This way we can ignore 'sub-modules' (ex: individual avars)
        :param network: The network to analyse.
        :return: True if the network is directly connected to a 'Rig' network.
        """
        return get_rig_network_from_module(network) is not None

    if sel is None:
        sel = pymel.selected()

    # Resolve the rig network from the selection
    module_networks = libSerialization.get_connected_networks(
        sel, key=is_module_child_of_rig)
    if not module_networks:
        pymel.warning("Found no module related to selection.")
        return None, None

    # Resolve rig
    rig_networks = set()
    for module in module_networks:
        rig_network = get_rig_network_from_module(module)
        rig_networks.add(rig_network)
    rig_network = next(iter(rig_networks), None)
    if not rig_network:
        pymel.warning("Found no rig related to selection.")
        return None, None

    # Deserialize the rig and find the associated networks
    rig = libSerialization.import_network(rig_network)
    modules = []
    for module in rig.modules:
        if module._network in module_networks:
            modules.append(module)

    return rig, modules
Esempio n. 16
0
def _get_module_networks_from_selection(module_name):
    """
    Search for specific module networks recursively, starting at selection.
    Note that to help with performances, we'll skip any Rig network we encounter.
    :param module_name: The name of the module to search for.
    :return: A list of pymel.nodetypes.Network.
    """
    def fn_skip(network):
        return libSerialization.is_network_from_class(network, 'Rig')
    def fn_key(network):
        return libSerialization.is_network_from_class(network, module_name)
    sel = pymel.selected()
    networks = libSerialization.get_connected_networks(sel, recursive=True, key=fn_key, key_skip=fn_skip)

    modules = [libSerialization.import_network(network, fn_skip=fn_skip) for network in networks]
    modules = filter(None, modules)  # Filter any invalid networks that libSerialization doesn't protect us from.
    return modules
Esempio n. 17
0
def _get_modules_from_selection(sel=None):
    def get_rig_network_from_module(network):
        for plug in network.message.outputs(plugs=True):
            plug_node = plug.node()
            if not isinstance(plug_node, pymel.nodetypes.Network):
                continue
            if libSerialization.is_network_from_class(plug_node, 'Rig'):
                return plug_node
        return None

    def is_module_child_of_rig(network):
        """
        Allow us to recognize module directly connected to a 'Rig' network.
        This way we can ignore 'sub-modules' (ex: individual avars)
        :param network: The network to analyse.
        :return: True if the network is directly connected to a 'Rig' network.
        """
        return get_rig_network_from_module(network) is not None

    if sel is None:
        sel = pymel.selected()

    # Resolve the rig network from the selection
    module_networks = libSerialization.get_connected_networks(sel, key=is_module_child_of_rig)
    if not module_networks:
        pymel.warning("Found no module related to selection.")
        return None, None

    # Resolve rig
    rig_networks = set()
    for module in module_networks:
        rig_network = get_rig_network_from_module(module)
        rig_networks.add(rig_network)
    rig_network = next(iter(rig_networks), None)
    if not rig_network:
        pymel.warning("Found no rig related to selection.")
        return None, None

    # Deserialize the rig and find the associated networks
    rig = libSerialization.import_network(rig_network)
    modules = []
    for module in rig.modules:
        if module._network in module_networks:
            modules.append(module)

    return rig, modules
Esempio n. 18
0
def patch_spaceswitch_dpspine_objects():
    """
    This function is not clean, but it can be used to generate spaceswitch object for certain module and after look at
    all space switch target information to replace to space switch object target that could used a ctrl to the new space
    switch object that will not be removed after an unbuild
    """

    # Get the rig instance in the scene (Now support only the first one found
    rig_net = libSerialization.get_networks_from_class('Rig')[0]
    rig_instance = libSerialization.import_network(rig_net)

    # Get all the module that we could need to patch
    to_patch = [
        module for module in rig_instance.modules
        if isinstance(module, rigDpSpine.DpSpine)
    ]

    for module in to_patch:
        module_to_patch = module
        if isinstance(module_to_patch, rigDpSpine.DpSpine):
            # Find all connection that have from the ctrl ik down (COG) and replace it
            connected_to_fk_dwn = module_to_patch.ctrl_fk_dwn._network.message.outputs(
                s=False, d=True, p=True)

            for connection in connected_to_fk_dwn:
                attr_name = connection.shortName()
                if attr_name.find('targets') >= 0:
                    log.info(
                        'Reconnecting {0} from {1} to space switch node {2}'.
                        format(connection, module_to_patch.ctrl_ik_dwn,
                               module_to_patch.ctrl_ik_dwn._network))
                    connection.disconnect()
                    pymel.connectAttr(
                        module_to_patch.ctrl_ik_dwn._network.message,
                        connection,
                        force=True)
Esempio n. 19
0
def patch_spaceswitch_data_into_network(sel=None):
    if sel is None:
        sel = pymel.selected()

    networks = libSerialization.get_connected_networks(
        sel,
        key=lambda net: libSerialization.is_network_from_class(
            net, 'BaseCtrl'))

    for net in networks:
        ctrl_instance = libSerialization.import_network(net)
        data = ctrl_instance.get_spaceswitch_enum_targets()

        if data:
            # Create missing attributes if needed
            if not net.hasAttr('targets_indexes'):
                log.info(
                    'targets_indexes attribute is missing on network {0}. It will be created'
                    .format(net))
                pymel.addAttr(net,
                              longName='targets_indexes',
                              at='long',
                              multi=True)
            if not net.hasAttr('targets'):
                log.info(
                    'targets attribute is missing on network {0}. It will be created'
                    .format(net))
                pymel.addAttr(net,
                              longName='targets',
                              at='message',
                              multi=True)
            if not net.hasAttr('local_index'):
                log.info(
                    'targets_indexes attribute is missing on network {0}. It will be created'
                    .format(net))
                pymel.addAttr(net, longName='local_index', at='long')

            # Add data if needed
            for index, (name, obj) in data.items():
                target_data = net.targets.get()
                indexes_data = net.targets_indexes.get()
                if obj not in target_data:
                    if index not in indexes_data:
                        if obj is not None:
                            if isinstance(obj, pymel.nt.Joint):
                                log.info(
                                    'Patching network {0} space switch data named {1} - Index {2} on object {3}'
                                    .format(net.name(), name, index, obj))
                                pymel.connectAttr(
                                    obj.message, net.targets[len(target_data)])
                            elif isinstance(obj, pymel.nt.Transform):
                                key = lambda network: libSerialization.is_network_from_class(
                                    network, 'Node')
                                ctrl_net = libSerialization.get_connected_networks(
                                    obj, key)
                                if ctrl_net > 0:
                                    log.info(
                                        'Patching network {0} space switch data named {1} - '
                                        'Index {2} on object {3}'.format(
                                            net.name(), name, index,
                                            ctrl_net[0]))
                                    pymel.connectAttr(
                                        ctrl_net[0].message,
                                        net.targets[len(target_data)])
                            net.targets_indexes[len(target_data)].set(index)
                        elif name == 'Local':
                            log.info(
                                'Patching network {0} space switch data named {1} - Index {2} as local index'
                                .format(net.name(), name, index))
                            net.local_index.set(index)
                    else:
                        if obj is not None:
                            log.warning(
                                'Object {0} have not been found in attribute targets but index {1} yes.'
                                'This object will not be patched to prevent any index conflict. '
                                'Look at your data to understand'.format(
                                    obj, index))
                else:
                    log.info(
                        'Space Switch object {0} is already included in the targets attributes'
                        .format(obj))