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))
def _fill_widget_influences(self, qt_parent, data): obj = pymel.PyNode(data.val) if data.val else None #obj, children_data = data if obj: obj_name = obj.stripNamespace() fnFilter = lambda x: libSerialization.is_network_from_class(x, 'Module') networks = libSerialization.get_connected_networks(obj, key=fnFilter, recursive=False) textBrush = QtGui.QBrush(QtCore.Qt.white) if self._is_influence(obj): # todo: listen to the Rig class qItem = QtGui.QTreeWidgetItem(0) qItem.obj = obj qItem.networks = networks qItem.setText(0, obj_name) qItem.setForeground(0, textBrush) ui_shared._set_icon_from_type(obj, qItem) qItem.setCheckState(0, QtCore.Qt.Checked if networks else QtCore.Qt.Unchecked) if qItem.flags() & QtCore.Qt.ItemIsUserCheckable: qItem.setFlags(qItem.flags() ^ QtCore.Qt.ItemIsUserCheckable) qt_parent.addChild(qItem) qt_parent = qItem for child_data in data.children: self._fill_widget_influences(qt_parent, child_data)
def _fill_widget_influences(self, qt_parent, data): obj = pymel.PyNode(data.val) if data.val else None #obj, children_data = data if obj: obj_name = obj.stripNamespace() fnFilter = lambda x: libSerialization.is_network_from_class( x, 'Module') networks = libSerialization.get_connected_networks(obj, key=fnFilter, recursive=False) textBrush = QtGui.QBrush(QtCore.Qt.white) if self._is_influence(obj): # todo: listen to the Rig class qItem = QtGui.QTreeWidgetItem(0) qItem.obj = obj qItem.networks = networks qItem.setText(0, obj_name) qItem.setForeground(0, textBrush) ui_shared._set_icon_from_type(obj, qItem) qItem.setCheckState( 0, QtCore.Qt.Checked if networks else QtCore.Qt.Unchecked) if qItem.flags() & QtCore.Qt.ItemIsUserCheckable: qItem.setFlags(qItem.flags() ^ QtCore.Qt.ItemIsUserCheckable) qt_parent.addChild(qItem) qt_parent = qItem for child_data in data.children: self._fill_widget_influences(qt_parent, child_data)
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
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
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
def get_obj_mirror_def(obj): network_is_ctrl = lambda x: libSerialization.is_network_from_class(x, classCtrl.BaseCtrl.__name__.split('.')[-1]) networks = libSerialization.get_connected_networks([obj], key=network_is_ctrl, recursive=False) network = next(iter(networks), None) if network: # HACK: We read the attributes directly try: return ( network.attr('mirror_x').get(), network.attr('mirror_y').get(), network.attr('mirror_z').get(), network.attr('mirror_flip_pos_x').get(), network.attr('mirror_flip_pos_y').get(), network.attr('mirror_flip_pos_z').get(), network.attr('mirror_flip_rot_x').get(), network.attr('mirror_flip_rot_y').get(), network.attr('mirror_flip_rot_z').get() ) except pymel.MayaAttributeError as e: print(str(e)) # If we cannot resolve the ctrl data, take a guess? pymel.warning("Can't resolve mirror data for {0}".format(obj))
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))